copperlace/render/processor.rs
1use std::collections::HashMap;
2use std::sync::Arc;
3
4/// String transformer used in template processor pipelines.
5///
6/// Processors receive the rendered output of a rule or binding expression and
7/// return the transformed value. Returning `Err` stops rendering and surfaces a
8/// [`crate::render::RenderError::ProcessorError`].
9pub trait Processor: Send + Sync {
10 /// Transforms one rendered value.
11 fn process(&self, value: &str) -> Result<String, String>;
12}
13
14impl<F> Processor for F
15where
16 F: Fn(&str) -> Result<String, String> + Send + Sync,
17{
18 fn process(&self, value: &str) -> Result<String, String> {
19 self(value)
20 }
21}
22
23/// Registry mapping processor names to processor implementations.
24///
25/// Custom processors registered with [`crate::render::RuleSet::from_config_with_processors`]
26/// extend the builtin registry. If a custom processor uses the same name as a
27/// builtin, the custom implementation takes precedence.
28pub type ProcessorRegistry = HashMap<String, Arc<dyn Processor>>;
29
30/// Wraps a processor implementation for insertion into a [`ProcessorRegistry`].
31pub fn processor<F>(processor: F) -> Arc<dyn Processor>
32where
33 F: Processor + 'static,
34{
35 Arc::new(processor)
36}