pub struct ValueMap { /* private fields */ }Expand description
A CEL map with heterogeneous keys.
Uses a BTreeMap with a custom key type for deterministic iteration order.
Implementations§
Source§impl ValueMap
impl ValueMap
Sourcepub fn from_entries(entries: impl IntoIterator<Item = (MapKey, Value)>) -> Self
pub fn from_entries(entries: impl IntoIterator<Item = (MapKey, Value)>) -> Self
Create a map from an iterator of key-value pairs.
Sourcepub fn contains_key(&self, key: &MapKey) -> bool
pub fn contains_key(&self, key: &MapKey) -> bool
Check if a key exists.
Sourcepub fn get_with_numeric_coercion(&self, key: &MapKey) -> Option<&Value>
pub fn get_with_numeric_coercion(&self, key: &MapKey) -> Option<&Value>
Get a value by key with cross-type numeric coercion. Tries exact match first, then Int↔UInt coercion for in-range values.
Sourcepub fn contains_key_with_numeric_coercion(&self, key: &MapKey) -> bool
pub fn contains_key_with_numeric_coercion(&self, key: &MapKey) -> bool
Check if a key exists with cross-type numeric coercion.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of entries.
Examples found in repository?
examples/extract_values.rs (line 63)
7fn main() {
8 let env = Env::with_standard_library()
9 .with_variable("count", CelType::Int)
10 .with_variable("items", CelType::list(CelType::Int))
11 .with_variable("config", CelType::map(CelType::String, CelType::String));
12
13 let mut activation = MapActivation::new();
14 activation.insert("count", 42); // i32 automatically widens to i64
15 activation.insert("items", Value::list([1, 2, 3]));
16 activation.insert(
17 "config",
18 Value::map([("host", "localhost"), ("port", "8080")]),
19 );
20
21 // Extract i64
22 let ast = env.compile("count * 2").unwrap();
23 let program = env.program(&ast).unwrap();
24 let result = program.eval(&activation);
25
26 let value: i64 = (&result).try_into().expect("expected int");
27 println!("i64: {}", value);
28
29 // Extract bool
30 let ast = env.compile("count > 10").unwrap();
31 let program = env.program(&ast).unwrap();
32 let result = program.eval(&activation);
33
34 let value: bool = (&result).try_into().expect("expected bool");
35 println!("bool: {}", value);
36
37 // Extract &str
38 let ast = env.compile("config.host").unwrap();
39 let program = env.program(&ast).unwrap();
40 let result = program.eval(&activation);
41
42 let value: &str = (&result).try_into().expect("expected string");
43 println!("&str: {}", value);
44
45 // Extract &[Value] (list)
46 let ast = env.compile("items.filter(x, x > 1)").unwrap();
47 let program = env.program(&ast).unwrap();
48 let result = program.eval(&activation);
49
50 let list: &[Value] = (&result).try_into().expect("expected list");
51 println!("list length: {}", list.len());
52 for (i, v) in list.iter().enumerate() {
53 let n: i64 = v.try_into().expect("expected int");
54 println!(" [{}] = {}", i, n);
55 }
56
57 // Extract &ValueMap
58 let ast = env.compile("config").unwrap();
59 let program = env.program(&ast).unwrap();
60 let result = program.eval(&activation);
61
62 let map: &ValueMap = (&result).try_into().expect("expected map");
63 println!("map size: {}", map.len());
64 for (k, v) in map.iter() {
65 println!(" {:?} = {}", k, v);
66 }
67
68 // Handle errors gracefully
69 let result = Value::from("not an int");
70 let attempt: Result<i64, _> = (&result).try_into();
71 match attempt {
72 Ok(v) => println!("got: {}", v),
73 Err(e) => println!("conversion error: {}", e),
74 }
75
76 // Extract &OptionalValue
77 let opt_result = Value::optional_some(Value::from(99));
78 let opt: &OptionalValue = (&opt_result).try_into().expect("expected optional");
79 if opt.is_present() {
80 let inner: i64 = opt.as_value().unwrap().try_into().expect("expected int");
81 println!("optional value: {}", inner);
82 }
83}Sourcepub fn iter(&self) -> impl Iterator<Item = (&MapKey, &Value)>
pub fn iter(&self) -> impl Iterator<Item = (&MapKey, &Value)>
Iterate over entries.
Examples found in repository?
examples/extract_values.rs (line 64)
7fn main() {
8 let env = Env::with_standard_library()
9 .with_variable("count", CelType::Int)
10 .with_variable("items", CelType::list(CelType::Int))
11 .with_variable("config", CelType::map(CelType::String, CelType::String));
12
13 let mut activation = MapActivation::new();
14 activation.insert("count", 42); // i32 automatically widens to i64
15 activation.insert("items", Value::list([1, 2, 3]));
16 activation.insert(
17 "config",
18 Value::map([("host", "localhost"), ("port", "8080")]),
19 );
20
21 // Extract i64
22 let ast = env.compile("count * 2").unwrap();
23 let program = env.program(&ast).unwrap();
24 let result = program.eval(&activation);
25
26 let value: i64 = (&result).try_into().expect("expected int");
27 println!("i64: {}", value);
28
29 // Extract bool
30 let ast = env.compile("count > 10").unwrap();
31 let program = env.program(&ast).unwrap();
32 let result = program.eval(&activation);
33
34 let value: bool = (&result).try_into().expect("expected bool");
35 println!("bool: {}", value);
36
37 // Extract &str
38 let ast = env.compile("config.host").unwrap();
39 let program = env.program(&ast).unwrap();
40 let result = program.eval(&activation);
41
42 let value: &str = (&result).try_into().expect("expected string");
43 println!("&str: {}", value);
44
45 // Extract &[Value] (list)
46 let ast = env.compile("items.filter(x, x > 1)").unwrap();
47 let program = env.program(&ast).unwrap();
48 let result = program.eval(&activation);
49
50 let list: &[Value] = (&result).try_into().expect("expected list");
51 println!("list length: {}", list.len());
52 for (i, v) in list.iter().enumerate() {
53 let n: i64 = v.try_into().expect("expected int");
54 println!(" [{}] = {}", i, n);
55 }
56
57 // Extract &ValueMap
58 let ast = env.compile("config").unwrap();
59 let program = env.program(&ast).unwrap();
60 let result = program.eval(&activation);
61
62 let map: &ValueMap = (&result).try_into().expect("expected map");
63 println!("map size: {}", map.len());
64 for (k, v) in map.iter() {
65 println!(" {:?} = {}", k, v);
66 }
67
68 // Handle errors gracefully
69 let result = Value::from("not an int");
70 let attempt: Result<i64, _> = (&result).try_into();
71 match attempt {
72 Ok(v) => println!("got: {}", v),
73 Err(e) => println!("conversion error: {}", e),
74 }
75
76 // Extract &OptionalValue
77 let opt_result = Value::optional_some(Value::from(99));
78 let opt: &OptionalValue = (&opt_result).try_into().expect("expected optional");
79 if opt.is_present() {
80 let inner: i64 = opt.as_value().unwrap().try_into().expect("expected int");
81 println!("optional value: {}", inner);
82 }
83}Trait Implementations§
Auto Trait Implementations§
impl Freeze for ValueMap
impl !RefUnwindSafe for ValueMap
impl Send for ValueMap
impl Sync for ValueMap
impl Unpin for ValueMap
impl UnsafeUnpin for ValueMap
impl !UnwindSafe for ValueMap
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