iowo/src/syntax/check.rs

103 lines
2.5 KiB
Rust
Raw Normal View History

2023-11-15 21:13:04 +01:00
use super::{
error::{FileId, SyntaxError},
2023-11-18 18:34:03 +01:00
parse_syntax, PipelineElement, PipelineElementKind,
2023-11-15 21:13:04 +01:00
};
pub fn check(
syntax: &Vec<PipelineElement>,
) -> Result<(), Vec<SyntaxError>> {
2023-11-15 21:13:04 +01:00
let mut errs = Vec::new();
if let Err(e_span) = check_missing_streamer(&syntax) {
2023-11-18 18:34:03 +01:00
errs.push(SyntaxError::MissingStreamer(vec![e_span]));
2023-11-15 21:13:04 +01:00
}
2023-11-18 18:34:03 +01:00
if let Err(err_locs) = check_missing_filters(&syntax) {
errs.push(SyntaxError::MissingFilter(err_locs))
}
if let Err(e_span) = check_missing_sink(&syntax) {
2023-11-18 18:34:03 +01:00
errs.push(SyntaxError::MissingSink(vec![e_span]));
}
2023-11-15 21:13:04 +01:00
if errs.is_empty() {
Ok(())
2023-11-15 21:13:04 +01:00
} else {
Err(errs)
}
}
fn check_missing_streamer(syntax: &Vec<PipelineElement>) -> Result<(), logos::Span> {
if let Some(&PipelineElement {
kind: PipelineElementKind::Pipe,
ref span,
}) = syntax.first()
{
Err(span.clone())
} else {
Ok(())
}
}
2023-11-18 18:34:03 +01:00
#[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>> {
let mut missing_filter_locs = Vec::new();
for i in 0..syntax.len() {
if let (
Some(&PipelineElement {
kind: PipelineElementKind::Pipe,
ref span,
}),
Some(&PipelineElement {
kind: PipelineElementKind::Pipe,
span: ref span1,
}),
) = (syntax.get(i), syntax.get(i + 1))
{
missing_filter_locs.push(span.start..span1.end)
}
}
if missing_filter_locs.is_empty() {
Ok(())
} else {
Err(missing_filter_locs)
}
}
2023-11-18 18:34:03 +01:00
#[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> {
if let Some(&PipelineElement {
kind: PipelineElementKind::Pipe,
ref span,
}) = syntax.last()
{
Err(span.clone())
} else {
Ok(())
}
}
2023-11-18 18:34:03 +01:00
#[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))
}