minijinja::value

Struct Kwargs

Source
pub struct Kwargs { /* private fields */ }
Expand description

Utility to accept keyword arguments.

Keyword arguments are represented as regular values as the last argument in an argument list. This can be quite complex to use manually so this type is added as a utility. You can use get to fetch a single keyword argument and then use assert_all_used to make sure extra arguments create an error.

Here an example of a function modifying values in different ways.

use minijinja::value::{Value, Kwargs};
use minijinja::Error;

fn modify(mut values: Vec<Value>, options: Kwargs) -> Result<Vec<Value>, Error> {
    // get pulls a parameter of any type.  Same as from_args.  For optional
    // boolean values the type inference is particularly convenient.
    if let Some(true) = options.get("reverse")? {
        values.reverse();
    }
    if let Some(limit) = options.get("limit")? {
        values.truncate(limit);
    }
    options.assert_all_used()?;
    Ok(values)
}

If for whatever reason you need a value again you can use Into to convert it back into a Value. This is particularly useful when performing calls into values. To create a Kwargs object from scratch you can use FromIterator:

use minijinja::value::{Value, Kwargs};
let kwargs = Kwargs::from_iter([
    ("foo", Value::from(true)),
    ("bar", Value::from(42)),
]);
let value = Value::from(kwargs);
assert!(value.is_kwargs());

When working with Rest you can use from_args to split all arguments into positional arguments and keyword arguments:

fn my_func(args: Rest<Value>) -> Result<Value, Error> {
    let (args, kwargs) = from_args::<(&[Value], Kwargs)>(&args)?;
    // do something with args and kwargs
}

Implementations§

Source§

impl Kwargs

Source

pub fn peek<'a, T>(&'a self, key: &'a str) -> Result<T, Error>
where T: ArgType<'a, Output = T>,

Get a single argument from the kwargs but don’t mark it as used.

Source

pub fn get<'a, T>(&'a self, key: &'a str) -> Result<T, Error>
where T: ArgType<'a, Output = T>,

Gets a single argument from the kwargs and marks it as used.

This method works pretty much like from_args and marks any parameter used internally. For optional arguments you would typically use Option<T> and for non optional ones directly T.

Examples:

// f(int=42) -> Some(42)
// f() -> None
let optional_int: Option<u32> = kwargs.get("int")?;
// f(int=42) -> 42
// f() -> Error
let required_int: u32 = kwargs.get("int")?;

If you don’t want to mark it as used, us peek instead.

Source

pub fn has(&self, key: &str) -> bool

Checks if a keyword argument exists.

Source

pub fn args(&self) -> impl Iterator<Item = &str>

Iterates over all passed keyword arguments.

Source

pub fn assert_all_used(&self) -> Result<(), Error>

Asserts that all kwargs were used.

Trait Implementations§

Source§

impl<'a> ArgType<'a> for Kwargs

Source§

type Output = Kwargs

The output type of this argument.
Source§

impl Clone for Kwargs

Source§

fn clone(&self) -> Kwargs

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Kwargs

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Kwargs> for Value

Source§

fn from(value: Kwargs) -> Self

Converts to this type from the input type.
Source§

impl<'a> FromIterator<(&'a str, Value)> for Kwargs

Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = (&'a str, Value)>,

Creates a value from an iterator. Read more
Source§

impl FromIterator<(String, Value)> for Kwargs

Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = (String, Value)>,

Creates a value from an iterator. Read more
Source§

impl TryFrom<Value> for Kwargs

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: Value) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl !Freeze for Kwargs

§

impl !RefUnwindSafe for Kwargs

§

impl Send for Kwargs

§

impl !Sync for Kwargs

§

impl Unpin for Kwargs

§

impl !UnwindSafe for Kwargs

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<I> FunctionResult for I
where I: Into<Value>,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.