Skip to main content

Context

Struct Context 

Source
pub struct Context { /* private fields */ }
Expand description

Template rendering context — holds all variables available during rendering.

§Examples

From an iterator of tuples:

use md_tmpl_core::{Context, Value};

let ctx: Context = vec![
    ("name", Value::from("Alice")),
    ("count", Value::from(3_i64)),
]
.into_iter()
.collect();

assert!(ctx.get("name").is_some());

Implementations§

Source§

impl Context

Source

pub fn new() -> Context

Create an empty context.

Source

pub fn with_capacity(capacity: usize) -> Context

Create an empty context pre-allocated for capacity variables.

Source

pub fn len(&self) -> usize

Returns the number of variables in this context.

Source

pub fn is_empty(&self) -> bool

Returns true if this context contains no variables.

Source

pub fn contains_key(&self, key: &str) -> bool

Returns true if a variable with the given key exists.

Source

pub fn set(&mut self, key: impl Into<String>, value: impl Into<Value>)

Insert a value into the context.

This is the dynamic API — the key is a plain string and type mismatches are caught at render_ctx() time, not here. For compile-time type safety, prefer one of:

  • include_template! (from md-tmpl-macros) — generates a strongly-typed parameter struct from your template.
  • Template::render (feature serde) — renders directly from any Serialize struct.
§Panics

Panics if key is the reserved internal key __kind__ — this key is used internally for enum variant tagging and must not be set directly. Use enum types in the template frontmatter instead.

Source

pub fn var(self, key: impl Into<String>, value: impl Into<Value>) -> Context

Builder-style insert — returns self for chaining.

Source

pub fn get(&self, key: &str) -> Option<&Value>

Look up a top-level variable.

Source

pub fn into_inner(self) -> HashMap<String, Value>

Consume this context and return the inner variable map.

Useful for converting a context into a Value::Struct.

Source§

impl Context

Source

pub fn from_value(val: Value) -> Result<Context, TemplateError>

Build a Context directly from a Value::Struct.

§Errors

Returns TemplateError::Syntax if the value is not a dict/map.

Source

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

Build a Context from any Serialize type that serializes as a map/struct.

§Errors

Returns TemplateError::Syntax if the serialized value is not a dict/map.

Source§

impl Context

CBOR support — behind the cbor feature, which implies serde.

Available in no_std — ciborium uses its own Read trait with a blanket impl for &[u8].

Source

pub fn from_cbor(data: &[u8]) -> Result<Context, TemplateError>

Build a Context from a CBOR binary buffer.

§Errors

Returns TemplateError::Syntax if the buffer is invalid or not a dict/map.

Source§

impl Context

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

Build a Context from a FlexBuffers binary buffer.

§Errors

Returns TemplateError::Syntax if the buffer is invalid or not a dict/map.

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

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 Context

Source§

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

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

impl Default for Context

Source§

fn default() -> Context

Returns the “default value” for a type. Read more
Source§

impl<K, V> FromIterator<(K, V)> for Context
where K: Into<String>, V: Into<Value>,

Collect (&str, Value) tuples into a Context.

Source§

fn from_iter<I>(iter: I) -> Context
where I: IntoIterator<Item = (K, V)>,

Creates a value from an iterator. Read more

Auto Trait Implementations§

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