macro_rules! function {
($(#[$outer:meta])* $ty:ident ( $($arg:ident),* ) $body:block) => { ... };
(!count $x:tt $($xs:tt)*) => { ... };
(!count) => { ... };
}Expand description
Defines a function type with the given name and argument names, implementing
crate::Function with the provided body.
The body may refer to the arguments by name, and must return a
std::borrow::Cow<FilterValue>. The body is evaluated each time the function is called, and may
borrow from the arguments, but must not retain references to them beyond the
lifetime of the returned value.
ยงExample
use filt_rs::{function, FilterValue};
use std::borrow::Cow;
function!{
/// Calculates the length of a string argument, returning a number.
len(s) {
match s.as_ref() {
FilterValue::String(s) => Cow::Owned(FilterValue::Number(s.chars().count() as f64)),
_ => Cow::Owned(FilterValue::Null),
}
}
}