mod lines;
mod params;
use crate::metadata::{InputCardinality, NodeCategory, NodeMetadata};
use crate::processor::{BatchFile, BatchInput, NodeInput, NodeOutput, NodeProcessor};
use crate::progress::ProgressReporter;
use crate::{BntoError, ProcessContext};
use super::pass_through::pass_through;
use params::{ReportParams, build_report_parameters};
pub struct Report;
impl Report {
pub fn new() -> Self {
Self
}
}
impl Default for Report {
fn default() -> Self {
Self::new()
}
}
impl NodeProcessor for Report {
fn name(&self) -> &str {
"report"
}
fn metadata(&self) -> NodeMetadata {
NodeMetadata {
node_type: "report".to_string(),
name: "Report".to_string(),
description: "Report a templated line in the run's output: once, once per file, \
or once per entry of a list; files pass through unchanged."
.to_string(),
category: NodeCategory::Control,
accepts: vec![],
platforms: vec![
"browser".to_string(),
"cli".to_string(),
"desktop".to_string(),
],
parameters: build_report_parameters(),
input_cardinality: InputCardinality::Batch,
requires: vec![],
}
}
fn validate(&self, params: &serde_json::Map<String, serde_json::Value>) -> Vec<String> {
ReportParams::read(params).err().into_iter().collect()
}
fn resolves_items(&self) -> bool {
true
}
fn process(
&self,
input: NodeInput,
progress: &ProgressReporter,
ctx: &dyn ProcessContext,
) -> Result<NodeOutput, BntoError> {
let file = BatchFile {
data: input.data,
filename: input.filename,
mime_type: input.mime_type,
metadata: serde_json::Map::new(),
};
let batch = BatchInput {
files: vec![file],
params: input.params,
};
self.process_batch(batch, progress, ctx)
}
fn process_batch(
&self,
input: BatchInput,
progress: &ProgressReporter,
ctx: &dyn ProcessContext,
) -> Result<NodeOutput, BntoError> {
let params = ReportParams::read(&input.params).map_err(BntoError::InvalidInput)?;
if ctx.dry_run() {
progress.report_output(&format!("Would report: {}", params.message));
return Ok(pass_through(input.files));
}
let lines = lines::lines(¶ms, &input.files).map_err(BntoError::InvalidInput)?;
for line in &lines {
progress.report_output(line);
}
Ok(pass_through(input.files))
}
}
#[cfg(test)]
mod tests;