2023-11-18 19:56:08 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test;
|
2023-11-15 21:13:04 +01:00
|
|
|
|
2023-11-18 19:56:08 +01:00
|
|
|
use super::{error::SyntaxError, PipelineElement, PipelineElementKind};
|
|
|
|
|
|
|
|
pub fn check(syntax: &[PipelineElement]) -> Result<(), Vec<SyntaxError>> {
|
2023-11-15 21:13:04 +01:00
|
|
|
let mut errs = Vec::new();
|
|
|
|
|
2023-11-18 19:56:08 +01:00
|
|
|
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 19:56:08 +01:00
|
|
|
if let Err(err_locs) = check_missing_filters(syntax) {
|
2023-11-18 18:34:03 +01:00
|
|
|
errs.push(SyntaxError::MissingFilter(err_locs))
|
2023-11-16 08:58:39 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 19:56:08 +01:00
|
|
|
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-16 19:22:26 +01:00
|
|
|
}
|
|
|
|
|
2023-11-15 21:13:04 +01:00
|
|
|
if errs.is_empty() {
|
2023-11-18 19:46:41 +01:00
|
|
|
Ok(())
|
2023-11-15 21:13:04 +01:00
|
|
|
} else {
|
|
|
|
Err(errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 19:56:08 +01:00
|
|
|
fn check_missing_streamer(syntax: &[PipelineElement]) -> Result<(), logos::Span> {
|
2023-11-15 21:13:04 +01:00
|
|
|
if let Some(&PipelineElement {
|
|
|
|
kind: PipelineElementKind::Pipe,
|
|
|
|
ref span,
|
|
|
|
}) = syntax.first()
|
|
|
|
{
|
|
|
|
Err(span.clone())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2023-11-16 08:58:39 +01:00
|
|
|
|
2023-11-18 19:56:08 +01:00
|
|
|
fn check_missing_filters(syntax: &[PipelineElement]) -> Result<(), Vec<logos::Span>> {
|
2023-11-16 08:58:39 +01:00
|
|
|
let mut missing_filter_locs = Vec::new();
|
|
|
|
|
|
|
|
for i in 0..syntax.len() {
|
|
|
|
if let (
|
2023-11-16 19:22:26 +01:00
|
|
|
Some(&PipelineElement {
|
2023-11-16 08:58:39 +01:00
|
|
|
kind: PipelineElementKind::Pipe,
|
|
|
|
ref span,
|
|
|
|
}),
|
2023-11-16 19:22:26 +01:00
|
|
|
Some(&PipelineElement {
|
2023-11-16 08:58:39 +01:00
|
|
|
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-16 19:22:26 +01:00
|
|
|
|
2023-11-18 19:56:08 +01:00
|
|
|
fn check_missing_sink(syntax: &[PipelineElement]) -> Result<(), logos::Span> {
|
2023-11-16 19:22:26 +01:00
|
|
|
if let Some(&PipelineElement {
|
|
|
|
kind: PipelineElementKind::Pipe,
|
|
|
|
ref span,
|
|
|
|
}) = syntax.last()
|
|
|
|
{
|
|
|
|
Err(span.clone())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|