refactoring and implementing clippy suggestions

This commit is contained in:
Schrottkatze 2023-11-18 19:56:08 +01:00
parent 3952091018
commit 2db2ef2ea1
5 changed files with 45 additions and 51 deletions

View file

@ -1,5 +1,3 @@
use std::path::PathBuf;
use clap::Parser; use clap::Parser;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]

View file

@ -1,17 +1,13 @@
use args::Args; use args::Args;
use clap::Parser; use clap::Parser;
use codespan_reporting::{ use codespan_reporting::{
files::{SimpleFile, SimpleFiles}, files::SimpleFiles,
term::{ term::{
self, self,
termcolor::{ColorChoice, StandardStream}, termcolor::{ColorChoice, StandardStream},
}, },
}; };
use syntax::{ use syntax::{check::check, error::SyntaxError, parse_syntax};
check::{self, check},
error::SyntaxError,
parse_syntax, PipelineElement,
};
mod args; mod args;
mod builtins; mod builtins;

View file

@ -1,22 +1,20 @@
use super::{ #[cfg(test)]
error::{FileId, SyntaxError}, mod test;
parse_syntax, PipelineElement, PipelineElementKind,
};
pub fn check( use super::{error::SyntaxError, PipelineElement, PipelineElementKind};
syntax: &Vec<PipelineElement>,
) -> Result<(), Vec<SyntaxError>> { pub fn check(syntax: &[PipelineElement]) -> Result<(), Vec<SyntaxError>> {
let mut errs = Vec::new(); 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])); 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)) 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])); errs.push(SyntaxError::MissingSink(vec![e_span]));
} }
@ -27,7 +25,7 @@ pub fn check(
} }
} }
fn check_missing_streamer(syntax: &Vec<PipelineElement>) -> Result<(), logos::Span> { fn check_missing_streamer(syntax: &[PipelineElement]) -> Result<(), logos::Span> {
if let Some(&PipelineElement { if let Some(&PipelineElement {
kind: PipelineElementKind::Pipe, kind: PipelineElementKind::Pipe,
ref span, ref span,
@ -39,15 +37,7 @@ fn check_missing_streamer(syntax: &Vec<PipelineElement>) -> Result<(), logos::Sp
} }
} }
#[test] fn check_missing_filters(syntax: &[PipelineElement]) -> Result<(), Vec<logos::Span>> {
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<PipelineElement>) -> Result<(), Vec<logos::Span>> {
let mut missing_filter_locs = Vec::new(); let mut missing_filter_locs = Vec::new();
for i in 0..syntax.len() { for i in 0..syntax.len() {
@ -73,15 +63,7 @@ fn check_missing_filters(syntax: &Vec<PipelineElement>) -> Result<(), Vec<logos:
} }
} }
#[test] fn check_missing_sink(syntax: &[PipelineElement]) -> Result<(), logos::Span> {
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]))
}
fn check_missing_sink(syntax: &Vec<PipelineElement>) -> Result<(), logos::Span> {
if let Some(&PipelineElement { if let Some(&PipelineElement {
kind: PipelineElementKind::Pipe, kind: PipelineElementKind::Pipe,
ref span, ref span,
@ -92,11 +74,3 @@ fn check_missing_sink(syntax: &Vec<PipelineElement>) -> Result<(), logos::Span>
Ok(()) 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))
}

28
src/syntax/check/test.rs Normal file
View file

@ -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))
}

View file

@ -1,7 +1,5 @@
use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::diagnostic::{Diagnostic, Label};
pub type FileId = usize;
/// The enum representing a syntax error, used for error reporting /// The enum representing a syntax error, used for error reporting
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum SyntaxError { pub enum SyntaxError {
@ -27,7 +25,7 @@ impl SyntaxError {
Self::InvalidToken(errs) => Diagnostic::error() Self::InvalidToken(errs) => Diagnostic::error()
.with_message("failed to parse invalid tokens") .with_message("failed to parse invalid tokens")
.with_labels( .with_labels(
errs.into_iter() errs.iter()
.map(|span| { .map(|span| {
Label::primary(file_id, span.clone()).with_message("invalid token") Label::primary(file_id, span.clone()).with_message("invalid token")
}) })
@ -36,7 +34,7 @@ impl SyntaxError {
Self::MissingStreamer(locs) => Diagnostic::error() Self::MissingStreamer(locs) => Diagnostic::error()
.with_message("pipelines must always start with a streamer") .with_message("pipelines must always start with a streamer")
.with_labels( .with_labels(
locs.into_iter() locs.iter()
.map(|span| { .map(|span| {
Label::primary(file_id, span.clone()).with_message("missing streamer") Label::primary(file_id, span.clone()).with_message("missing streamer")
}) })
@ -45,7 +43,7 @@ impl SyntaxError {
Self::MissingFilter(locs) => Diagnostic::error() Self::MissingFilter(locs) => Diagnostic::error()
.with_message("missing filters in pipelines") .with_message("missing filters in pipelines")
.with_labels( .with_labels(
locs.into_iter() locs.iter()
.map(|span| { .map(|span| {
Label::primary(file_id, span.clone()).with_message("no filter here") Label::primary(file_id, span.clone()).with_message("no filter here")
}) })
@ -54,7 +52,7 @@ impl SyntaxError {
Self::MissingSink(locs) => Diagnostic::error() Self::MissingSink(locs) => Diagnostic::error()
.with_message("pipelines cannot end on a pipe") .with_message("pipelines cannot end on a pipe")
.with_labels( .with_labels(
locs.into_iter() locs.iter()
.map(|span| Label::primary(file_id, span.clone()).with_message("no sink")) .map(|span| Label::primary(file_id, span.clone()).with_message("no sink"))
.collect(), .collect(),
), ),