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;
#[derive(Parser, Debug)]

View file

@ -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;

View file

@ -1,22 +1,20 @@
use super::{
error::{FileId, SyntaxError},
parse_syntax, PipelineElement, PipelineElementKind,
};
#[cfg(test)]
mod test;
pub fn check(
syntax: &Vec<PipelineElement>,
) -> Result<(), Vec<SyntaxError>> {
use super::{error::SyntaxError, PipelineElement, PipelineElementKind};
pub fn check(syntax: &[PipelineElement]) -> Result<(), Vec<SyntaxError>> {
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<PipelineElement>) -> 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<PipelineElement>) -> 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<PipelineElement>) -> Result<(), Vec<logos::Span>> {
fn check_missing_filters(syntax: &[PipelineElement]) -> Result<(), Vec<logos::Span>> {
let mut missing_filter_locs = Vec::new();
for i in 0..syntax.len() {
@ -73,15 +63,7 @@ fn check_missing_filters(syntax: &Vec<PipelineElement>) -> Result<(), Vec<logos:
}
}
#[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]))
}
fn check_missing_sink(syntax: &Vec<PipelineElement>) -> 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<PipelineElement>) -> 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))
}

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};
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(),
),