mod fill;
mod params;
use crate::metadata::{InputCardinality, NodeCategory, NodeMetadata};
use crate::processor::{BatchFile, BatchInput, NodeInput, NodeOutput, NodeProcessor, OutputFile};
use crate::progress::ProgressReporter;
use crate::{BntoError, ProcessContext};
use params::{EditFieldsParams, build_edit_fields_parameters};
pub struct EditFields;
impl EditFields {
pub fn new() -> Self {
Self
}
}
impl Default for EditFields {
fn default() -> Self {
Self::new()
}
}
impl NodeProcessor for EditFields {
fn name(&self) -> &str {
"edit-fields"
}
fn metadata(&self) -> NodeMetadata {
NodeMetadata {
node_type: "edit-fields".to_string(),
name: "Edit Fields".to_string(),
description: "Edit fields on each item, setting them from static values or templates \
over its other fields, such as {{item.first}} {{item.last}}; files pass \
through unchanged."
.to_string(),
category: NodeCategory::Data,
accepts: vec![],
platforms: vec![
"browser".to_string(),
"cli".to_string(),
"desktop".to_string(),
],
parameters: build_edit_fields_parameters(),
input_cardinality: InputCardinality::Batch,
requires: vec![],
}
}
fn validate(&self, params: &serde_json::Map<String, serde_json::Value>) -> Vec<String> {
EditFieldsParams::read(params).err().into_iter().collect()
}
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 resolves_items(&self) -> bool {
true
}
fn process_batch(
&self,
input: BatchInput,
_progress: &ProgressReporter,
_ctx: &dyn ProcessContext,
) -> Result<NodeOutput, BntoError> {
let params = EditFieldsParams::read(&input.params).map_err(BntoError::InvalidInput)?;
let files = input
.files
.into_iter()
.map(|f| edit_file(¶ms, f))
.collect::<Result<_, _>>()?;
Ok(NodeOutput {
files,
metadata: serde_json::Map::new(),
})
}
}
fn edit_file(params: &EditFieldsParams, f: BatchFile) -> Result<OutputFile, BntoError> {
let set = fill::fill_values(¶ms.values, &f.metadata)
.map_err(|why| BntoError::InvalidInput(format!("{}: {why}", f.filename)))?;
let mut metadata = if params.keep_only_set {
serde_json::Map::new()
} else {
f.metadata
};
metadata.extend(set);
Ok(OutputFile {
data: f.data,
filename: f.filename,
mime_type: f
.mime_type
.unwrap_or_else(|| "application/octet-stream".to_string()),
metadata,
})
}
#[cfg(test)]
mod tests;