Skip to content

Commit

Permalink
fix clippy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Pistonight committed Mar 11, 2024
1 parent 0a9804c commit 3a0e5b8
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 107 deletions.
6 changes: 4 additions & 2 deletions compiler-base/src/env/env_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ impl<T> From<Vec<T>> for RefCounted<[T]> {
}
}

impl<T> From<Box<T>> for RefCounted<T>
where T: ?Sized {
impl<T> From<Box<T>> for RefCounted<T>
where
T: ?Sized,
{
#[inline]
fn from(v: Box<T>) -> Self {
Self {
Expand Down
7 changes: 3 additions & 4 deletions compiler-base/src/env/env_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ impl<T> From<Vec<T>> for RefCounted<[T]> {
}

impl<T> From<Box<T>> for RefCounted<T>
where T: ?Sized
where
T: ?Sized,
{
#[inline]
fn from(v: Box<T>) -> Self {
Self {
inner: Rc::from(v),
}
Self { inner: Rc::from(v) }
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler-base/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::cell::RefCell;
use std::fmt::Display;
use std::ops::Deref;

use crate::res::Loader;
use crate::macros::late_global;
use crate::res::Loader;

#[cfg(feature = "wasm")]
pub mod env_wasm;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub mod site {
None => match origin.strip_prefix("http://") {
Some(domain) => RefCounted::from(domain),
None => origin,
}
},
}
}
}
Expand Down
1 change: 0 additions & 1 deletion compiler-base/src/util/data_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub enum DataUrlParseError {
NoData,
#[error("Error decoding base64 from data url: {0}")]
InvalidBase64(#[from] base64::DecodeError),

}

/// Decode data url to bytes. Supports base64 and URL encoding.
Expand Down
91 changes: 49 additions & 42 deletions compiler-core/src/comp/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ impl<'p> CompDoc<'p> {

/// Iterate over each CompLine of the CompDoc
pub fn lines_mut(&mut self) -> impl Iterator<Item = &mut CompLine> {
self.route.iter_mut().flat_map(|section| section.lines.iter_mut())
self.route
.iter_mut()
.flat_map(|section| section.lines.iter_mut())
}

/// Iterate over each DocRichText of the CompDoc.
Expand Down Expand Up @@ -72,10 +74,10 @@ pub struct LineRichTextIterMut<'c> {

impl<'c, 'p> RichTextIter<'c, 'p> {
pub fn new(doc: &'c CompDoc<'p>) -> Self {
Self {
doc,
Self {
doc,
with_preface: false,
with_counter: false
with_counter: false,
}
}
pub fn with_preface(mut self) -> Self {
Expand All @@ -89,8 +91,8 @@ impl<'c, 'p> RichTextIter<'c, 'p> {
}
impl<'c, 'p> RichTextIterMut<'c, 'p> {
pub fn new(doc: &'c mut CompDoc<'p>) -> Self {
Self {
doc,
Self {
doc,
with_preface: false,
with_counter: false,
}
Expand All @@ -106,9 +108,9 @@ impl<'c, 'p> RichTextIterMut<'c, 'p> {
}
impl<'c> LineRichTextIter<'c> {
pub fn new(line: &'c CompLine) -> Self {
Self {
line,
with_counter: false
Self {
line,
with_counter: false,
}
}
pub fn with_counter(mut self) -> Self {
Expand All @@ -118,9 +120,9 @@ impl<'c> LineRichTextIter<'c> {
}
impl<'c> LineRichTextIterMut<'c> {
pub fn new(line: &'c mut CompLine) -> Self {
Self {
line,
with_counter: false
Self {
line,
with_counter: false,
}
}
pub fn with_counter(mut self) -> Self {
Expand All @@ -129,15 +131,15 @@ impl<'c> LineRichTextIterMut<'c> {
}
}

impl<'c, 'p> IntoIterator for RichTextIter<'c, 'p>
{
impl<'c, 'p> IntoIterator for RichTextIter<'c, 'p> {
type Item = &'c DocRichTextBlock;
type IntoIter = DynIter<'c, Self::Item>;
fn into_iter(self) -> Self::IntoIter {
let iter = self.doc.route.iter().flat_map(move |section| {
section.lines.iter().flat_map(move |line| {
rich_text_iter(line, self.with_counter)
})
section
.lines
.iter()
.flat_map(move |line| rich_text_iter(line, self.with_counter))
});
if self.with_preface {
let preface_iter = self.doc.preface.iter().flat_map(|text| text.iter());
Expand All @@ -146,15 +148,15 @@ impl<'c, 'p> IntoIterator for RichTextIter<'c, 'p>
DynIter::new(iter)
}
}
impl<'c, 'p> IntoIterator for RichTextIterMut<'c, 'p>
{
impl<'c, 'p> IntoIterator for RichTextIterMut<'c, 'p> {
type Item = &'c mut DocRichTextBlock;
type IntoIter = DynIter<'c, Self::Item>;
fn into_iter(self) -> Self::IntoIter {
let iter = self.doc.route.iter_mut().flat_map(move |section| {
section.lines.iter_mut().flat_map(move |line| {
rich_text_iter_mut(line, self.with_counter)
})
section
.lines
.iter_mut()
.flat_map(move |line| rich_text_iter_mut(line, self.with_counter))
});
if self.with_preface {
let preface_iter = self.doc.preface.iter_mut().flat_map(|text| text.iter_mut());
Expand All @@ -163,17 +165,15 @@ impl<'c, 'p> IntoIterator for RichTextIterMut<'c, 'p>
DynIter::new(iter)
}
}
impl<'c> IntoIterator for LineRichTextIter<'c>
{
impl<'c> IntoIterator for LineRichTextIter<'c> {
type Item = &'c DocRichTextBlock;
type IntoIter = DynIter<'c, Self::Item>;
fn into_iter(self) -> Self::IntoIter {
let iter = rich_text_iter(self.line, self.with_counter);
DynIter::new(iter)
}
}
impl<'c> IntoIterator for LineRichTextIterMut<'c>
{
impl<'c> IntoIterator for LineRichTextIterMut<'c> {
type Item = &'c mut DocRichTextBlock;
type IntoIter = DynIter<'c, Self::Item>;
fn into_iter(self) -> Self::IntoIter {
Expand All @@ -182,14 +182,14 @@ impl<'c> IntoIterator for LineRichTextIterMut<'c>
}
}

fn rich_text_iter<'c>(line: &'c CompLine, with_counter: bool) -> impl Iterator<Item = &'c DocRichTextBlock> {
let iter = line.text.iter()
fn rich_text_iter(line: &CompLine, with_counter: bool) -> impl Iterator<Item = &DocRichTextBlock> {
let iter = line
.text
.iter()
.chain(line.secondary_text.iter())
.chain(line.notes.iter().flat_map(|note| {
match note {
DocNote::Text { content } => DynIter::new(content.iter()),
_ => DynIter::new(std::iter::empty())
}
.chain(line.notes.iter().flat_map(|note| match note {
DocNote::Text { content } => DynIter::new(content.iter()),
_ => DynIter::new(std::iter::empty()),
}))
.chain({
if !with_counter {
Expand All @@ -211,14 +211,17 @@ fn rich_text_iter<'c>(line: &'c CompLine, with_counter: bool) -> impl Iterator<I
iter
}

fn rich_text_iter_mut<'c>(line: &'c mut CompLine, with_counter: bool) -> impl Iterator<Item = &'c mut DocRichTextBlock> {
let iter = line.text.iter_mut()
fn rich_text_iter_mut(
line: &mut CompLine,
with_counter: bool,
) -> impl Iterator<Item = &mut DocRichTextBlock> {
let iter = line
.text
.iter_mut()
.chain(line.secondary_text.iter_mut())
.chain(line.notes.iter_mut().flat_map(|note| {
match note {
DocNote::Text { content } => DynIter::new(content.iter_mut()),
_ => DynIter::new(std::iter::empty())
}
.chain(line.notes.iter_mut().flat_map(|note| match note {
DocNote::Text { content } => DynIter::new(content.iter_mut()),
_ => DynIter::new(std::iter::empty()),
}))
.chain({
if !with_counter {
Expand All @@ -240,11 +243,15 @@ fn rich_text_iter_mut<'c>(line: &'c mut CompLine, with_counter: bool) -> impl It
iter
}

pub struct DynIter<'c, T>(Box<dyn Iterator<Item = T> + 'c + Send + Sync>) where T: 'c;
pub struct DynIter<'c, T>(Box<dyn Iterator<Item = T> + 'c + Send + Sync>)
where
T: 'c;

impl<'c, T> DynIter<'c, T> {
fn new<I>(iter: I) -> Self
where I: Iterator<Item = T> + 'c + Send + Sync {
where
I: Iterator<Item = T> + 'c + Send + Sync,
{
Self(Box::new(iter))
}
}
Expand Down
17 changes: 11 additions & 6 deletions compiler-core/src/expo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ impl<'p> CompDoc<'p> {

for plugin in &mut plugins {
if req.plugin_id == plugin.get_id() {
let result = match plugin.on_export_comp_doc(&req.export_id, &req.payload, self).await {
let result = match plugin
.on_export_comp_doc(&req.export_id, &req.payload, self)
.await
{
Ok(None) => None,
Ok(Some(expo_doc)) => Some(expo_doc),
Err(e) => Some(ExpoDoc::Error(e.to_string())),
Expand All @@ -162,11 +165,13 @@ impl<'p> ExecContext<'p> {

for plugin in &mut plugins {
if req.plugin_id == plugin.get_id() {
let result =
match plugin.on_export_exec_doc(&req.export_id, req.payload, &self.exec_doc).await {
Ok(expo_doc) => expo_doc,
Err(e) => ExpoDoc::Error(e.to_string()),
};
let result = match plugin
.on_export_exec_doc(&req.export_id, req.payload, &self.exec_doc)
.await
{
Ok(expo_doc) => expo_doc,
Err(e) => ExpoDoc::Error(e.to_string()),
};
return result;
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/plugin/builtin/botw_unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::borrow::Cow;
use serde_json::Value;

use crate::comp::{CompDoc, CompLine, CompMovement};
use crate::env::yield_budget;
use crate::json::Coerce;
use crate::lang::{DocDiagnostic, DocRichText, DocRichTextBlock};
use crate::plugin::{PluginResult, PluginRuntime};
use crate::env::yield_budget;
use crate::macros::async_trait;
use crate::plugin::{PluginResult, PluginRuntime};

const FURY: &str = "fury";
const GALE: &str = "gale";
Expand Down
42 changes: 26 additions & 16 deletions compiler-core/src/plugin/builtin/export_livesplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::comp::{CompDoc, CompLine, CompSection};
use crate::env::{self, yield_budget, RefCounted};
use crate::expo::{ExpoBlob, ExpoDoc, ExportIcon, ExportMetadata};
use crate::export_error;
use crate::json::Coerce;
use crate::plugin::{PluginResult, PluginRuntime};
use crate::macros::async_trait;
use crate::export_error;
use crate::env::{self, RefCounted, yield_budget};
use crate::plugin::{PluginResult, PluginRuntime};
use crate::res::ResPath;

pub struct ExportLiveSplitPlugin;
Expand Down Expand Up @@ -86,7 +86,7 @@ impl PluginRuntime for ExportLiveSplitPlugin {
// build lines to split
let mut split_sections = Vec::with_capacity(doc.route.len());
for section in &doc.route {
let mut split_lines= vec![];
let mut split_lines = vec![];
for line in &section.lines {
yield_budget(64).await;
if should_split_on(line, &split_types) {
Expand Down Expand Up @@ -162,7 +162,7 @@ fn should_split_on(line: &CompLine, split_types: &BTreeSet<String>) -> bool {
}

async fn build_icon_cache(
doc: &CompDoc<'_>,
doc: &CompDoc<'_>,
split_sections: &[(&CompSection, Vec<&CompLine>)],
webp_compat: WebpCompat,
) -> Result<BTreeMap<String, Vec<u8>>, String> {
Expand All @@ -179,7 +179,11 @@ async fn build_icon_cache(
continue;
}
if let Some(icon_url) = doc.config.icons.get(icon_id) {
icon_futures.push(load_icon(icon_id.to_string(), icon_url.to_string(), webp_compat))
icon_futures.push(load_icon(
icon_id.to_string(),
icon_url.to_string(),
webp_compat,
))
}
}
}
Expand All @@ -189,18 +193,16 @@ async fn build_icon_cache(
match result {
Ok(Ok((id, bytes))) => {
cache.insert(id, bytes.to_vec());
},
Ok(Err(e)) | Err(e) => {
return Err(e)
},
}
Ok(Err(e)) | Err(e) => return Err(e),
}
}
Ok(cache)
}

fn create_segment(
line: &CompLine,
name: &str,
line: &CompLine,
name: &str,
include_icon: bool,
icon_cache: &BTreeMap<String, Vec<u8>>,
) -> livesplit_core::Segment {
Expand All @@ -223,12 +225,20 @@ enum WebpCompat {
/// Emit error. This is the default
Error,
/// Skip the icon
Skip
Skip,
}

async fn load_icon(icon_id: String, icon_url: String, webp_compat: WebpCompat) -> Result<(String, RefCounted<[u8]>), String> {
async fn load_icon(
icon_id: String,
icon_url: String,
webp_compat: WebpCompat,
) -> Result<(String, RefCounted<[u8]>), String> {
let loader = match env::global_loader::get() {
None => return Err("No global loader available to load the icons for split export!".to_string()),
None => {
return Err(
"No global loader available to load the icons for split export!".to_string(),
)
}
Some(loader) => loader,
};

Expand All @@ -240,7 +250,7 @@ async fn load_icon(icon_id: String, icon_url: String, webp_compat: WebpCompat) -

if data.starts_with(b"RIFF") {
if let WebpCompat::Error = webp_compat {
return Err(format!("Failed to load icon `{icon_url}`: RIFF (webp) icons are not supported by LiveSplit. Set the option `webp-compat: skip` to skip invalid webp icons."))
return Err(format!("Failed to load icon `{icon_url}`: RIFF (webp) icons are not supported by LiveSplit. Set the option `webp-compat: skip` to skip invalid webp icons."));
}
}

Expand Down
Loading

0 comments on commit 3a0e5b8

Please sign in to comment.