pub struct FilterRegistry { /* private fields */ }Expand description
A named set of filters.
TemplateOptions::default carries the
built in filters. Registering a filter with the same name replaces the built
in one for that set of options.
§Examples
use docx_templating::{FilterError, FilterRegistry};
use serde_json::Value;
let mut registry = FilterRegistry::default();
registry.insert("shout", |value, _| {
let text = value
.as_str()
.ok_or_else(|| FilterError::new("value is not a string"))?;
Ok(Value::String(format!("{}!", text.to_uppercase())))
});
assert!(registry.contains("shout"));
assert!(registry.contains("upper"));Implementations§
Source§impl FilterRegistry
impl FilterRegistry
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a registry with the built in filters.
The built in filters are the text filters upper, lower, trim,
default, len and join, the number filters round, fixed,
thousands and percent, and, when the dates feature is on, the
date filters date and timezone.
Sourcepub fn insert<F>(&mut self, name: impl Into<String>, filter: F)
pub fn insert<F>(&mut self, name: impl Into<String>, filter: F)
Register a filter, replacing any filter with the same name.
§Examples
use docx_templating::FilterRegistry;
use serde_json::Value;
let mut registry = FilterRegistry::default();
registry.insert("wrap", |value, argument| {
let open = argument.unwrap_or("[");
Ok(Value::String(format!("{open}{value}")))
});Sourcepub fn get(
&self,
name: &str,
) -> Option<&(dyn Fn(&Value, Option<&str>) -> Result<Value, FilterError> + Send + Sync)>
pub fn get( &self, name: &str, ) -> Option<&(dyn Fn(&Value, Option<&str>) -> Result<Value, FilterError> + Send + Sync)>
Get a filter by name.
Trait Implementations§
Source§impl Clone for FilterRegistry
impl Clone for FilterRegistry
Source§impl Debug for FilterRegistry
impl Debug for FilterRegistry
Auto Trait Implementations§
impl !RefUnwindSafe for FilterRegistry
impl !UnwindSafe for FilterRegistry
impl Forget for FilterRegistry
impl Freeze for FilterRegistry
impl Move for FilterRegistry
impl Send for FilterRegistry
impl Sync for FilterRegistry
impl Unpin for FilterRegistry
impl UnsafeUnpin for FilterRegistrywhere
*const u8 is !null: UnsafeUnpin,
PhantomData<(String, Arc<dyn Fn(&Value, Option<&str>) -> Result<Value, FilterError> + Sync + Send>)>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more