pub enum Value {
Null,
Bool(bool),
Number(i64),
String(String),
List(Vec<Value>),
Map(HashMap<String, Value, BuildHasherDefault<FnvHasher>>),
}Expand description
A polymorphic context value.
Templates can substitute Null, Bool, Number, and String variants
directly via Display. List and Map are addressable through
dot-notation paths (e.g. items.0 or user.name) and are typically
consumed by control-flow blocks rather than direct substitution.
§Examples
use staticweaver_core::context::Value;
let v = Value::Number(42);
assert_eq!(v.to_string(), "42");
let nested = Value::Map({
let mut m = std::collections::HashMap::new();
let _ = m.insert("name".to_string(), Value::String("Alice".to_string()));
m.into_iter().collect()
});
assert!(matches!(nested.get_path("name"), Some(Value::String(_))));Variants§
Null
Absent / null. Renders as the empty string.
Bool(bool)
Boolean. Renders as "true" or "false".
Number(i64)
64-bit signed integer. Renders via Display.
String(String)
UTF-8 string. Renders verbatim (the engine handles HTML escaping).
List(Vec<Value>)
Ordered sequence of values. Addressable by 0, 1, …
Map(HashMap<String, Value, BuildHasherDefault<FnvHasher>>)
Nested map of named values. Addressable by key.
Implementations§
Source§impl Value
impl Value
Sourcepub fn get_path(&self, path: &str) -> Option<&Value>
pub fn get_path(&self, path: &str) -> Option<&Value>
Walks a dot-separated path through nested Map and List
variants, returning the final value if the entire path resolves.
List segments are matched as decimal indices (items.0.name).
Returns None for any missing segment, type mismatch, or invalid
index.
§Examples
use staticweaver_core::context::Value;
use fnv::FnvHashMap;
let mut inner = FnvHashMap::default();
let _ = inner.insert("name".to_string(), Value::String("Alice".to_string()));
let mut outer = FnvHashMap::default();
let _ = outer.insert("user".to_string(), Value::Map(inner));
let v = Value::Map(outer);
assert!(matches!(v.get_path("user.name"), Some(Value::String(_))));
assert!(v.get_path("user.missing").is_none());Sourcepub fn is_truthy(&self) -> bool
pub fn is_truthy(&self) -> bool
Returns true if the value is “truthy” — used by control-flow
blocks ({{#if key}}).
Null and Bool(false) are falsy; Number(0) is falsy; empty
String, List, and Map are falsy. Everything else is truthy.
§Examples
use staticweaver_core::context::Value;
assert!(!Value::Null.is_truthy());
assert!(!Value::Bool(false).is_truthy());
assert!(!Value::Number(0).is_truthy());
assert!(!Value::String(String::new()).is_truthy());
assert!(Value::String("x".to_string()).is_truthy());
assert!(Value::Number(1).is_truthy());Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Returns the inner string for Value::String, otherwise None.
Convenience for the back-compat Context::get shape.
§Examples
use staticweaver_core::context::Value;
assert_eq!(Value::String("hi".to_string()).as_str(), Some("hi"));
assert_eq!(Value::Number(42).as_str(), None);