diff --git a/src/assignment_resolver.rs b/src/assignment_resolver.rs index b1fcd6e..777035e 100644 --- a/src/assignment_resolver.rs +++ b/src/assignment_resolver.rs @@ -45,10 +45,7 @@ impl<'src: 'run, 'run> AssignmentResolver<'src, 'run> { length: 0, kind: TokenKind::Unspecified, }; - return Err(CompileError { - kind: Internal { message }, - token, - }); + return Err(CompileError::new(token, Internal { message })); } self.stack.pop(); diff --git a/src/compile_error.rs b/src/compile_error.rs index f36a393..0756878 100644 --- a/src/compile_error.rs +++ b/src/compile_error.rs @@ -3,20 +3,27 @@ use super::*; #[derive(Debug, PartialEq)] pub(crate) struct CompileError<'src> { pub(crate) token: Token<'src>, - pub(crate) kind: CompileErrorKind<'src>, + pub(crate) kind: Box>, } impl<'src> CompileError<'src> { pub(crate) fn context(&self) -> Token<'src> { self.token } + + pub(crate) fn new(token: Token<'src>, kind: CompileErrorKind<'src>) -> CompileError<'src> { + Self { + token, + kind: Box::new(kind), + } + } } impl Display for CompileError<'_> { fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { use CompileErrorKind::*; - match &self.kind { + match &*self.kind { AliasShadowsRecipe { alias, recipe_line } => { write!( f, diff --git a/src/justfile.rs b/src/justfile.rs index ccad90a..26f06ea 100644 --- a/src/justfile.rs +++ b/src/justfile.rs @@ -257,7 +257,7 @@ impl<'src> Justfile<'src> { let mut ran = BTreeSet::new(); for (recipe, arguments) in grouped { - self.run_recipe(&context, recipe, arguments, &dotenv, search, &mut ran)?; + Self::run_recipe(&context, recipe, arguments, &dotenv, search, &mut ran)?; } Ok(()) @@ -276,7 +276,6 @@ impl<'src> Justfile<'src> { } fn run_recipe( - &self, context: &RecipeContext<'src, '_>, recipe: &Recipe<'src>, arguments: &[&str], @@ -314,7 +313,7 @@ impl<'src> Justfile<'src> { .map(|argument| evaluator.evaluate_expression(argument)) .collect::>>()?; - self.run_recipe( + Self::run_recipe( context, recipe, &arguments.iter().map(String::as_ref).collect::>(), @@ -336,7 +335,7 @@ impl<'src> Justfile<'src> { evaluated.push(evaluator.evaluate_expression(argument)?); } - self.run_recipe( + Self::run_recipe( context, recipe, &evaluated.iter().map(String::as_ref).collect::>(), diff --git a/src/lexer.rs b/src/lexer.rs index 536eea7..06268eb 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -209,12 +209,12 @@ impl<'src> Lexer<'src> { length: 0, kind: Unspecified, }; - CompileError { - kind: CompileErrorKind::Internal { + CompileError::new( + token, + CompileErrorKind::Internal { message: message.into(), }, - token, - } + ) } /// Create a compilation error with `kind` @@ -245,14 +245,11 @@ impl<'src> Lexer<'src> { length, }; - CompileError { token, kind } + CompileError::new(token, kind) } fn unterminated_interpolation_error(interpolation_start: Token<'src>) -> CompileError<'src> { - CompileError { - token: interpolation_start, - kind: UnterminatedInterpolation, - } + CompileError::new(interpolation_start, UnterminatedInterpolation) } /// True if `text` could be an identifier @@ -796,7 +793,7 @@ impl<'src> Lexer<'src> { let mut escape = false; loop { - if self.next == None { + if self.next.is_none() { return Err(self.error(kind.unterminated_error_kind())); } else if kind.processes_escape_sequences() && self.next_is('\\') && !escape { escape = true; @@ -993,7 +990,7 @@ mod tests { column, length, }, - kind, + kind: Box::new(kind), }; assert_eq!(have, want); } @@ -2276,20 +2273,19 @@ mod tests { fn presume_error() { let compile_error = Lexer::new("!").presume('-').unwrap_err(); assert_matches!( - compile_error, - CompileError { - token: Token { - offset: 0, - line: 0, - column: 0, - length: 0, - src: "!", - kind: Unspecified, - }, - kind: Internal { - ref message, - }, - } if message == "Lexer presumed character `-`" + compile_error.token, + Token { + offset: 0, + line: 0, + column: 0, + length: 0, + src: "!", + kind: Unspecified, + } + ); + assert_matches!(&*compile_error.kind, + Internal { ref message } + if message == "Lexer presumed character `-`" ); assert_eq!( diff --git a/src/load_dotenv.rs b/src/load_dotenv.rs index b738e61..a83da15 100644 --- a/src/load_dotenv.rs +++ b/src/load_dotenv.rs @@ -39,7 +39,7 @@ fn load_from_file(path: &Path) -> RunResult<'static, BTreeMap> { // https://github.com/dotenv-rs/dotenv/issues/13 #![allow(deprecated)] - let iter = dotenv::from_path_iter(&path)?; + let iter = dotenv::from_path_iter(path)?; let mut dotenv = BTreeMap::new(); for result in iter { let (key, value) = result?; diff --git a/src/parser.rs b/src/parser.rs index effb9ca..cb197e0 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -396,10 +396,11 @@ impl<'tokens, 'src> Parser<'tokens, 'src> { /// Parse an expression, e.g. `1 + 2` fn parse_expression(&mut self) -> CompileResult<'src, Expression<'src>> { if self.depth == if cfg!(windows) { 48 } else { 256 } { - return Err(CompileError { - token: self.next()?, - kind: CompileErrorKind::ParsingRecursionDepthExceeded, - }); + let token = self.next()?; + return Err(CompileError::new( + token, + CompileErrorKind::ParsingRecursionDepthExceeded, + )); } self.depth += 1; @@ -904,7 +905,7 @@ mod tests { column, length, }, - kind, + kind: Box::new(kind), }; assert_eq!(have, want); } diff --git a/src/platform.rs b/src/platform.rs index e84dcce..965fac2 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -21,14 +21,14 @@ impl PlatformInterface for Platform { use std::os::unix::fs::PermissionsExt; // get current permissions - let mut permissions = fs::metadata(&path)?.permissions(); + let mut permissions = fs::metadata(path)?.permissions(); // set the execute bit let current_mode = permissions.mode(); permissions.set_mode(current_mode | 0o100); // set the new permissions - fs::set_permissions(&path, permissions) + fs::set_permissions(path, permissions) } fn signal_from_exit_status(exit_status: process::ExitStatus) -> Option { diff --git a/src/string_kind.rs b/src/string_kind.rs index edfbe46..06b530f 100644 --- a/src/string_kind.rs +++ b/src/string_kind.rs @@ -84,12 +84,9 @@ impl StringKind { } pub(crate) fn from_token_start(token_start: &str) -> Option { - for &kind in Self::ALL { - if token_start.starts_with(kind.delimiter()) { - return Some(kind); - } - } - - None + Self::ALL + .iter() + .find(|&&kind| token_start.starts_with(kind.delimiter())) + .copied() } } diff --git a/src/testing.rs b/src/testing.rs index 89dfd46..5988e48 100644 --- a/src/testing.rs +++ b/src/testing.rs @@ -79,7 +79,7 @@ pub(crate) fn analysis_error( column, length, }, - kind, + kind: Box::new(kind), }; assert_eq!(have, want); } diff --git a/src/token.rs b/src/token.rs index d86cef6..c360f32 100644 --- a/src/token.rs +++ b/src/token.rs @@ -16,7 +16,7 @@ impl<'src> Token<'src> { } pub(crate) fn error(&self, kind: CompileErrorKind<'src>) -> CompileError<'src> { - CompileError { token: *self, kind } + CompileError::new(*self, kind) } }