From 2db2ef2ea14d671bd4c846785e6b08f43397664a Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Sat, 18 Nov 2023 19:56:08 +0100 Subject: [PATCH] refactoring and implementing clippy suggestions --- src/args.rs | 2 -- src/main.rs | 8 ++----- src/syntax/check.rs | 48 +++++++++------------------------------- src/syntax/check/test.rs | 28 +++++++++++++++++++++++ src/syntax/error.rs | 10 ++++----- 5 files changed, 45 insertions(+), 51 deletions(-) create mode 100644 src/syntax/check/test.rs diff --git a/src/args.rs b/src/args.rs index 336be67..1ac3ad2 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,5 +1,3 @@ -use std::path::PathBuf; - use clap::Parser; #[derive(Parser, Debug)] diff --git a/src/main.rs b/src/main.rs index a8abacf..3be6cd6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,13 @@ use args::Args; use clap::Parser; use codespan_reporting::{ - files::{SimpleFile, SimpleFiles}, + files::SimpleFiles, term::{ self, termcolor::{ColorChoice, StandardStream}, }, }; -use syntax::{ - check::{self, check}, - error::SyntaxError, - parse_syntax, PipelineElement, -}; +use syntax::{check::check, error::SyntaxError, parse_syntax}; mod args; mod builtins; diff --git a/src/syntax/check.rs b/src/syntax/check.rs index c69f950..dd074cb 100644 --- a/src/syntax/check.rs +++ b/src/syntax/check.rs @@ -1,22 +1,20 @@ -use super::{ - error::{FileId, SyntaxError}, - parse_syntax, PipelineElement, PipelineElementKind, -}; +#[cfg(test)] +mod test; -pub fn check( - syntax: &Vec, -) -> Result<(), Vec> { +use super::{error::SyntaxError, PipelineElement, PipelineElementKind}; + +pub fn check(syntax: &[PipelineElement]) -> Result<(), Vec> { let mut errs = Vec::new(); - if let Err(e_span) = check_missing_streamer(&syntax) { + if let Err(e_span) = check_missing_streamer(syntax) { errs.push(SyntaxError::MissingStreamer(vec![e_span])); } - if let Err(err_locs) = check_missing_filters(&syntax) { + if let Err(err_locs) = check_missing_filters(syntax) { errs.push(SyntaxError::MissingFilter(err_locs)) } - if let Err(e_span) = check_missing_sink(&syntax) { + if let Err(e_span) = check_missing_sink(syntax) { errs.push(SyntaxError::MissingSink(vec![e_span])); } @@ -27,7 +25,7 @@ pub fn check( } } -fn check_missing_streamer(syntax: &Vec) -> Result<(), logos::Span> { +fn check_missing_streamer(syntax: &[PipelineElement]) -> Result<(), logos::Span> { if let Some(&PipelineElement { kind: PipelineElementKind::Pipe, ref span, @@ -39,15 +37,7 @@ fn check_missing_streamer(syntax: &Vec) -> Result<(), logos::Sp } } -#[test] -fn test_check_missing_streamer() { - let test_data = "| invert | save \"./image_processed.jpg\""; - let syntax = parse_syntax(test_data).unwrap(); - - assert_eq!(check_missing_streamer(&syntax), Err(0..1)) -} - -fn check_missing_filters(syntax: &Vec) -> Result<(), Vec> { +fn check_missing_filters(syntax: &[PipelineElement]) -> Result<(), Vec> { let mut missing_filter_locs = Vec::new(); for i in 0..syntax.len() { @@ -73,15 +63,7 @@ fn check_missing_filters(syntax: &Vec) -> Result<(), Vec) -> Result<(), logos::Span> { +fn check_missing_sink(syntax: &[PipelineElement]) -> Result<(), logos::Span> { if let Some(&PipelineElement { kind: PipelineElementKind::Pipe, ref span, @@ -92,11 +74,3 @@ fn check_missing_sink(syntax: &Vec) -> Result<(), logos::Span> Ok(()) } } - -#[test] -fn test_check_missing_sink() { - let test_data = "meow | invert | "; - let syntax = parse_syntax(test_data).unwrap(); - - assert_eq!(check_missing_sink(&syntax), Err(14..15)) -} diff --git a/src/syntax/check/test.rs b/src/syntax/check/test.rs new file mode 100644 index 0000000..c885303 --- /dev/null +++ b/src/syntax/check/test.rs @@ -0,0 +1,28 @@ +use crate::syntax::{ + check::{check_missing_filters, check_missing_sink, check_missing_streamer}, + parse_syntax, +}; + +#[test] +fn test_check_missing_streamer() { + let test_data = "| invert | save \"./image_processed.jpg\""; + let syntax = parse_syntax(test_data).unwrap(); + + assert_eq!(check_missing_streamer(&syntax), Err(0..1)) +} + +#[test] +fn test_check_missing_filters() { + let test_data = "meow | | test | awa | | nya"; + let syntax = parse_syntax(test_data).unwrap(); + + assert_eq!(check_missing_filters(&syntax), Err(vec![5..8, 20..25])) +} + +#[test] +fn test_check_missing_sink() { + let test_data = "meow | invert | "; + let syntax = parse_syntax(test_data).unwrap(); + + assert_eq!(check_missing_sink(&syntax), Err(14..15)) +} diff --git a/src/syntax/error.rs b/src/syntax/error.rs index cf5a02e..e4cdaff 100644 --- a/src/syntax/error.rs +++ b/src/syntax/error.rs @@ -1,7 +1,5 @@ use codespan_reporting::diagnostic::{Diagnostic, Label}; -pub type FileId = usize; - /// The enum representing a syntax error, used for error reporting #[derive(Debug, Clone)] pub enum SyntaxError { @@ -27,7 +25,7 @@ impl SyntaxError { Self::InvalidToken(errs) => Diagnostic::error() .with_message("failed to parse invalid tokens") .with_labels( - errs.into_iter() + errs.iter() .map(|span| { Label::primary(file_id, span.clone()).with_message("invalid token") }) @@ -36,7 +34,7 @@ impl SyntaxError { Self::MissingStreamer(locs) => Diagnostic::error() .with_message("pipelines must always start with a streamer") .with_labels( - locs.into_iter() + locs.iter() .map(|span| { Label::primary(file_id, span.clone()).with_message("missing streamer") }) @@ -45,7 +43,7 @@ impl SyntaxError { Self::MissingFilter(locs) => Diagnostic::error() .with_message("missing filters in pipelines") .with_labels( - locs.into_iter() + locs.iter() .map(|span| { Label::primary(file_id, span.clone()).with_message("no filter here") }) @@ -54,7 +52,7 @@ impl SyntaxError { Self::MissingSink(locs) => Diagnostic::error() .with_message("pipelines cannot end on a pipe") .with_labels( - locs.into_iter() + locs.iter() .map(|span| Label::primary(file_id, span.clone()).with_message("no sink")) .collect(), ),