Struct minijinja::value::Value

source ·
pub struct Value(_);
Expand description

Represents a dynamically typed value in the template engine.

Implementations

The undefined value

Creates a value from something that can be serialized.

This is the method that MiniJinja will generally use whenever a serializable object is passed to one of the APIs that internally want to create a value. For instance this is what context! and render will use.

During serialization of the value, serializing_for_value will return true which makes it possible to customize serialization for MiniJinja. For more information see serializing_for_value.

let val = Value::from_serializable(&vec![1, 2, 3]);

Creates a value from a safe string.

A safe string is one that will bypass auto escaping. For instance if you want to have the template engine render some HTML without the user having to supply the |safe filter, you can use a value of this type instead.

let val = Value::from_safe_string("<em>note</em>".into());

Creates a value from a dynamic object.

For more information see Object.

use std::fmt;

#[derive(Debug)]
struct Thing {
    id: usize,
}

impl fmt::Display for Thing {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl Object for Thing {}

let val = Value::from_object(Thing { id: 42 });

Objects are internally reference counted. If you want to hold on to the Arc you can directly create the value from an arc’ed object:

use std::sync::Arc;
let val = Value::from(Arc::new(Thing { id: 42 }));

Creates a callable value from a function.

let pow = Value::from_function(|a: u32| a * a);

Returns the kind of the value.

This can be used to determine what’s in the value before trying to perform operations on it.

Returns true if the map represents keyword arguments.

Is this value true?

Returns true if this value is safe.

Returns true if this value is undefined.

Returns true if this value is none.

If the value is a string, return it.

Returns the bytes of this value if they exist.

If the value is a sequence it’s returned as slice.

let seq = Value::from(vec![1u32, 2, 3, 4]);
let slice = seq.as_slice().unwrap();
assert_eq!(slice.len(), 4);

Returns the length of the contained value.

Values without a length will return None.

let seq = Value::from(vec![1, 2, 3, 4]);
assert_eq!(seq.len(), Some(4));

Looks up an attribute by attribute name.

This this returns UNDEFINED when an invalid key is resolved. An error is returned when if the value does not contain an object that has attributes.

let ctx = minijinja::context! {
    foo => "Foo"
};
let value = ctx.get_attr("foo")?;
assert_eq!(value.to_string(), "Foo");

Looks up an index of the value.

This is a shortcut for get_item.

let seq = Value::from(vec![0u32, 1, 2]);
let value = seq.get_item_by_index(1).unwrap();
assert_eq!(value.try_into().ok(), Some(1));

Looks up an item (or attribute) by key.

This is similar to get_attr but instead of using a string key this can be any key. For instance this can be used to index into sequences. Like get_attr this returns UNDEFINED when an invalid key is looked up.

let ctx = minijinja::context! {
    foo => "Foo",
};
let value = ctx.get_item(&Value::from("foo")).unwrap();
assert_eq!(value.to_string(), "Foo");

Iterates over the value.

Depending on the kind of the value the iterator has a different behavior.

let value = Value::from({
    let mut m = std::collections::BTreeMap::new();
    m.insert("foo", 42);
    m.insert("bar", 23);
    m
});
for key in value.try_iter()? {
    let value = value.get_item(&key)?;
    println!("{} = {}", key, value);
}

Returns some reference to the boxed object if it is of type T, or None if it isn’t.

This is basically the “reverse” of from_object.

Example
use std::fmt;

#[derive(Debug)]
struct Thing {
    id: usize,
}

impl fmt::Display for Thing {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl Object for Thing {}

let x_value = Value::from_object(Thing { id: 42 });
let thing = x_value.downcast_object_ref::<Thing>().unwrap();
assert_eq!(thing.id, 42);

Trait Implementations

The output type of this argument.
The output type of this argument.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Serialize this value into the given Serde serializer. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.