Skip to main content

Value

Enum Value 

Source
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

Source

pub fn is_truthy(&self) -> bool

Returns true if the value is considered “truthy”.

Source

pub fn type_name(&self) -> &'static str

Returns the type name as a static string.

Source

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.

Source

pub fn is_str(&self) -> bool

Returns true if this is a Str variant.

Source

pub fn is_int(&self) -> bool

Returns true if this is an Int variant.

Source

pub fn is_float(&self) -> bool

Returns true if this is a Float variant.

Source

pub fn is_bool(&self) -> bool

Returns true if this is a Bool variant.

Source

pub fn is_list(&self) -> bool

Returns true if this is a List variant.

Source

pub fn is_struct(&self) -> bool

Returns true if this is a Struct variant.

Source

pub fn as_str(&self) -> Option<&str>

Returns the inner &str if this is a Str variant.

Source

pub fn as_int(&self) -> Option<i64>

Returns the inner i64 if this is an Int variant.

Source

pub fn as_float(&self) -> Option<f64>

Returns the inner f64 if this is a Float variant.

Source

pub fn as_bool(&self) -> Option<bool>

Returns the inner bool if this is a Bool variant.

Source

pub fn as_list(&self) -> Option<&[Value]>

Returns a slice of the inner list if this is a List variant.

Source

pub fn as_struct(&self) -> Option<&HashMap<String, Value>>

Returns a reference to the inner map if this is a Struct variant.

Source

pub fn as_tmpl(&self) -> Option<&Arc<Template>>

Returns a reference to the inner template if this is a Tmpl variant.

Source

pub fn new_struct<I, K, V>(pairs: I) -> Value
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<Value>,

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");
Source

pub fn list<I, V>(items: I) -> Value
where I: IntoIterator<Item = V>, V: Into<Value>,

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

Source

pub fn from_serialize<T>(value: &T) -> Result<Value, SerError>
where T: Serialize,

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"));
Source

pub fn deserialize_into<'de, T>(&'de self) -> Result<T, DeError>
where T: Deserialize<'de>,

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).

Source

pub fn from_flexbuffers(data: &[u8]) -> Result<Value, 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 Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

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

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

impl<'de> Deserialize<'de> for Value

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Value, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Value

Source§

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

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

impl Eq for Value

Source§

impl From<&Template> for Value

Source§

fn from(t: &Template) -> Value

Converts to this type from the input type.
Source§

impl From<&str> for Value

Source§

fn from(s: &str) -> Value

Converts to this type from the input type.
Source§

impl From<Arc<Template>> for Value

Source§

fn from(t: Arc<Template>) -> Value

Converts to this type from the input type.
Source§

impl From<HashMap<String, Value>> for Value

Source§

fn from(m: HashMap<String, Value>) -> Value

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(s: String) -> Value

Converts to this type from the input type.
Source§

impl From<Template> for Value

Source§

fn from(t: Template) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<Value>> for Value

Source§

fn from(v: Vec<Value>) -> Value

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(b: bool) -> Value

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(f: f32) -> Value

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(f: f64) -> Value

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(i: i32) -> Value

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(i: i64) -> Value

Converts to this type from the input type.
Source§

impl From<u32> for Value

Source§

fn from(i: u32) -> Value

Converts to this type from the input type.
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Value) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl TryFrom<Value> for Vec<Value>

Source§

type Error = ValueTypeError

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

fn try_from( v: Value, ) -> Result<Vec<Value>, <Vec<Value> as TryFrom<Value>>::Error>

Performs the conversion.
Source§

impl TryFrom<u64> for Value

Source§

type Error = TryFromIntError

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

fn try_from(i: u64) -> Result<Value, <Value as TryFrom<u64>>::Error>

Performs the conversion.
Source§

impl TryFrom<usize> for Value

Source§

type Error = TryFromIntError

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

fn try_from(i: usize) -> Result<Value, <Value as TryFrom<usize>>::Error>

Performs the conversion.

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> 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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

Source§

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

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.