Struct minijinja::value::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

§

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

§

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> 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,

§

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>,

§

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>,

§

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.