pub struct Value(_);
Expand description
Represents a dynamically typed value in the template engine.
Implementations
sourceimpl Value
impl Value
sourcepub fn from_serializable<T: Serialize>(value: &T) -> Value
pub fn from_serializable<T: Serialize>(value: &T) -> Value
Creates a value from something that can be serialized.
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
.
sourcepub fn from_safe_string(value: String) -> Value
pub fn from_safe_string(value: String) -> Value
Creates a value from a safe string.
sourcepub fn from_object<T: Object + 'static>(value: T) -> Value
pub fn from_object<T: Object + 'static>(value: T) -> Value
Creates a value from a dynamic object.
sourcepub fn downcast_object_ref<T: Object>(&self) -> Option<&T>
pub fn downcast_object_ref<T: Object>(&self) -> Option<&T>
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);
sourcepub fn is_undefined(&self) -> bool
pub fn is_undefined(&self) -> bool
Returns true
if this value is undefined.
sourcepub fn get_attr(&self, key: &str) -> Result<Value, Error>
pub fn get_attr(&self, key: &str) -> Result<Value, Error>
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.
sourcepub fn get_item_by_index(&self, idx: usize) -> Result<Value, Error>
pub fn get_item_by_index(&self, idx: usize) -> Result<Value, Error>
Looks up an index of the value.
This is a shortcut for get_item
.
Trait Implementations
sourceimpl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
sourceimpl PartialOrd<Value> for Value
impl PartialOrd<Value> for Value
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more