Skip to main content

extend_store_scalars/
extend_store_scalars.rs

1//! Defining scalar variables in a Store.
2//!
3//! The Store is the data backing a Pipeline. Operations read from and write to
4//! it. This example shows how to define scalar variables and read them back,
5//! using the ergonomic From/Into conversions that let you pass Rust primitives
6//! directly — no `.into()` calls required.
7
8use panopticon_core::extend::{Store, Type, Value};
9
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    let mut store = Store::new();
12
13    // define_var accepts any V: Into<Value>.
14    // From impls exist for &str, String, bool, i64, f64, so you can pass
15    // these directly without calling .into().
16    store.define_var("name", "Alice")?;
17    store.define_var("age", 30i64)?;
18    store.define_var("active", true)?;
19    store.define_var("score", 9.5f64)?;
20    store.define_var("empty", Value::Null)?;
21
22    // Reading back: Store::get returns &StoreEntry.
23    // get_value() extracts the &Value from a Var entry.
24    let name = store.get("name")?.get_value()?;
25    println!("name = {name:?}");
26
27    // as_var() returns (&Value, &Type) — useful when you need to inspect both.
28    let (val, ty) = store.get("age")?.as_var()?;
29    println!("age = {val:?} (type: {ty:?})");
30
31    // Type is inferred from the value at define time.
32    assert_eq!(store.get("active")?.as_var()?.1, &Type::Boolean);
33    assert_eq!(store.get("score")?.as_var()?.1, &Type::Float);
34    assert_eq!(store.get("empty")?.as_var()?.1, &Type::Null);
35
36    // Duplicate names are rejected with StoreError::EntryAlreadyExists.
37    let err = store.define_var("name", "Bob");
38    println!("duplicate define_var: {err:?}");
39    assert!(err.is_err());
40
41    println!("store_scalars: ok");
42    Ok(())
43}