#[non_exhaustive]pub struct DataContext { /* private fields */ }Implementations§
Source§impl DataContext
impl DataContext
Sourcepub fn set<V: Into<DataValue>>(&mut self, key: &str, value: V)
pub fn set<V: Into<DataValue>>(&mut self, key: &str, value: V)
Set a value. Accepts various types via Into<DataValue>.
Sourcepub fn lang(&self) -> &str
pub fn lang(&self) -> &str
The active language locale code (e.g. "en" / "zh"), read from the
reserved __lang key. Callers set it with ctx.set("__lang", "zh")
before rendering; it selects the variant for @t("en", "中文") text.
Defaults to "en" when unset.
Sourcepub fn is_truthy(&self, key: &str) -> bool
pub fn is_truthy(&self, key: &str) -> bool
Check if variable exists and is truthy (for visible field evaluation).
Returns true if:
- The value is
Bool(true) - The value is a non-zero
Int - The value exists and is any other type
Returns
falseif the key is absent or the value isBool(false)/Int(0).
Sourcepub fn resolve(&self, template: &str) -> String
pub fn resolve(&self, template: &str) -> String
Resolve a template string by substituting {var}, {var:0W}, and {var%N} patterns.
§Examples
let mut ctx = DataContext::new();
ctx.set("name", "LEAFKIT");
assert_eq!(ctx.resolve("{name}"), "LEAFKIT");
ctx.set("num", 1i64);
assert_eq!(ctx.resolve("\u{2116}\u{2022}{num:03}"), "\u{2116}\u{2022}001");
ctx.set("weight", 150i64);
assert_eq!(ctx.resolve("{weight%10}"), "0");Trait Implementations§
Source§impl Clone for DataContext
impl Clone for DataContext
Source§fn clone(&self) -> DataContext
fn clone(&self) -> DataContext
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for DataContext
impl RefUnwindSafe for DataContext
impl Send for DataContext
impl Sync for DataContext
impl Unpin for DataContext
impl UnsafeUnpin for DataContext
impl UnwindSafe for DataContext
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
Mutably borrows from an owned value. Read more
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,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.