fn_as_modifier

Macro fn_as_modifier 

Source
macro_rules! fn_as_modifier {
    (fn $modifier_name: ident ($first_name:ident: $first_t: ty $($(,$name: ident: $t: ty $(= $default: expr)?)+)?) -> $return: ty => $func: path) => { ... };
    (default_value $arg_name: ident) => { ... };
    (default_value $arg_name: ident $default: tt) => { ... };
    (try_into $value: ident: $type: ty) => { ... };
}
Expand description

Creates a new modifier based on a method. This macro is usually used to create new template modifiers. The method header of the resulting method will look different from the given header.

Possible parameter types are:

&str, String, bool, f64, isize, i32, usize, u32

§Example

use mini_template::value::Value;
use mini_template::fn_as_modifier;

fn repeat_n_times(s: &str, n: usize) -> String {
    let mut result = String::new();
    for _ in 0..n {
        result.push_str(s)
    }
    result
}

fn_as_modifier!(
    fn repeat_n_times_modifier(s: &str, n: usize) -> String => repeat_n_times
);

assert_eq!(
    repeat_n_times_modifier(&Value::String("17".to_owned()), vec![&Value::Number(2.)]),
    Ok(Value::String("1717".to_owned()))
);

§Warning

The variants default and try_into are for internal use only and are subject to change without further notice.