pub enum Value {
Str(String),
Bool(bool),
Int(i64),
Float(f64),
List(Arc<Vec<Value>>),
Struct(Arc<HashMap<String, Value>>),
Tmpl(Arc<Template>),
None,
}Expand description
A value that can be inserted into a template.
Variants§
Str(String)
A plain string.
Bool(bool)
A boolean.
Int(i64)
A 64-bit integer.
Float(f64)
A 64-bit float.
List(Arc<Vec<Value>>)
An ordered list of values.
Struct(Arc<HashMap<String, Value>>)
A string-keyed map of values.
Tmpl(Arc<Template>)
A pre-compiled template.
None
An absent/null value — transparent representation of Option::None.
Implementations§
Source§impl Value
impl Value
Sourcepub fn get_field(&self, key: &str) -> Option<&Value>
pub fn get_field(&self, key: &str) -> Option<&Value>
Access a field on a Struct value.
The internal enum tag key (ENUM_TAG_KEY)
is hidden — use str(value) to extract the variant name instead.
Sourcepub fn as_list(&self) -> Option<&[Value]>
pub fn as_list(&self) -> Option<&[Value]>
Returns a slice of the inner list if this is a List variant.
Sourcepub fn as_struct(&self) -> Option<&HashMap<String, Value>>
pub fn as_struct(&self) -> Option<&HashMap<String, Value>>
Returns a reference to the inner map if this is a Struct variant.
Sourcepub fn as_tmpl(&self) -> Option<&Arc<Template>>
pub fn as_tmpl(&self) -> Option<&Arc<Template>>
Returns a reference to the inner template if this is a Tmpl variant.
Sourcepub fn new_struct<I, K, V>(pairs: I) -> Self
pub fn new_struct<I, K, V>(pairs: I) -> Self
Create a Struct from an iterator of key-value pairs.
Accepts arrays, slices, vecs — anything iterable.
§Examples
use md_tmpl_core::Value;
let v = Value::new_struct([("name", "Alice"), ("role", "admin")]);
assert_eq!(v.get_field("name").unwrap().to_string(), "Alice");Sourcepub fn list<I, V>(items: I) -> Self
pub fn list<I, V>(items: I) -> Self
Create a List from an iterator of values.
Accepts arrays, slices, vecs — anything iterable.
§Examples
use md_tmpl_core::Value;
let v = Value::list([
Value::new_struct([("label", "alpha")]),
Value::new_struct([("label", "beta")]),
]);
assert_eq!(v.type_name(), "list");Source§impl Value
impl Value
Sourcepub fn from_serialize<T: Serialize>(value: &T) -> Result<Self, SerError>
pub fn from_serialize<T: Serialize>(value: &T) -> Result<Self, SerError>
Create a Value from any Serialize type.
This is the same as to_value but available as a
method on Value for convenience.
§Errors
Returns an error if serialization fails.
§Examples
use md_tmpl_core::Value;
use serde::Serialize;
#[derive(Serialize)]
struct Agent {
name: String,
}
let val = Value::from_serialize(&Agent {
name: "Alice".into(),
})
.unwrap();
assert_eq!(val.get_field("name").unwrap().as_str(), Some("Alice"));Sourcepub fn deserialize_into<'de, T: Deserialize<'de>>(
&'de self,
) -> Result<T, DeError>
pub fn deserialize_into<'de, T: Deserialize<'de>>( &'de self, ) -> Result<T, DeError>
Deserialize this Value into a Rust type.
This is the same as from_value but available as
a method on Value for convenience.
§Errors
Returns an error if the value shape doesn’t match T.
§Examples
use md_tmpl_core::Value;
use serde::Deserialize;
#[derive(Deserialize, Debug, PartialEq)]
struct Agent {
name: String,
}
let val = Value::new_struct([("name", Value::Str("Alice".into()))]);
let agent: Agent = val.deserialize_into().unwrap();
assert_eq!(
agent,
Agent {
name: "Alice".into()
}
);Source§impl Value
FlexBuffers support — behind the flexbuffers feature, which implies
std and serde (the flexbuffers crate does not support no_std).
impl Value
FlexBuffers support — behind the flexbuffers feature, which implies
std and serde (the flexbuffers crate does not support no_std).
Sourcepub fn from_flexbuffers(data: &[u8]) -> Result<Self, TemplateError>
pub fn from_flexbuffers(data: &[u8]) -> Result<Self, TemplateError>
Create a Value from a FlexBuffers binary buffer.
§Errors
Returns an error if the buffer is invalid or deserialization fails.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
Source§fn 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>,
impl Eq for Value
Auto Trait Implementations§
impl Freeze for Value
impl RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnsafeUnpin for Value
impl UnwindSafe for Value
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.