use crate::filter::*;
#[derive(Default)]
pub struct PipelineOptions {
pub display_errors: bool,
pub display_messages: bool,
}
pub struct Stage {
filter: Box<dyn Filter>,
inspect: bool
}
impl Stage {
pub fn new(filter: Box<dyn Filter>, inspect: bool) -> Self {
Self {
filter,
inspect
}
}
}
pub struct Pipeline {
stages: Vec<Stage>,
options: PipelineOptions
}
pub struct PipelineOutput {
pub output: EncodingData,
pub inspections: Vec<EncodingData>,
}
impl Pipeline {
pub fn new(stages: Vec<Stage>, options: PipelineOptions) -> Pipeline {
Self {
stages,
options,
}
}
pub fn run(&self, data: EncodingData) -> std::result::Result<PipelineOutput, String> {
let mut stage_data = Ok(data);
let mut inspections: Vec::<EncodingData> = Vec::new();
for stage in &self.stages {
match stage_data {
Ok(data) => {
self.message(&format!("running {}", stage.filter.name()));
stage_data = stage.filter.map(data);
if stage.inspect && stage_data.is_ok() {
inspections.push(stage_data.clone().unwrap());
}
},
Err(e) => {
self.error(&e);
return Err(e);
},
}
}
stage_data.map(|output| PipelineOutput {
output,
inspections,
})
}
fn message(&self, text: &str) {
if self.options.display_messages {
println!("{}", text);
}
}
fn error(&self, text: &str) {
if self.options.display_errors {
println!("error: {}", text);
}
}
}