Skip to main content

raw_values/
raw_values.rs

1/// Working with raw PklValue without serde deserialization.
2///
3/// Useful when you need dynamic access to PKL data or want to
4/// inspect the structure before deserializing.
5use pklrust::{EvaluatorManager, EvaluatorOptions, ModuleSource, PklValue};
6
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let mut manager = EvaluatorManager::new()?;
9    let evaluator = manager.new_evaluator(EvaluatorOptions::preconfigured())?;
10
11    let source = ModuleSource::text(
12        r#"
13        name = "example"
14        port = 8080
15        debug = true
16        tags = new Listing { "web"; "api" }
17        "#,
18    );
19
20    let value = manager.evaluate_module(&evaluator, source)?;
21
22    // Access properties via the convenience method
23    if let Some(props) = value.as_properties() {
24        for (key, val) in &props {
25            match val {
26                PklValue::String(s) => println!("{key} = \"{s}\""),
27                PklValue::Int(n) => println!("{key} = {n}"),
28                PklValue::Bool(b) => println!("{key} = {b}"),
29                PklValue::List(items) => {
30                    let strs: Vec<_> = items.iter().filter_map(|v| v.as_str()).collect();
31                    println!("{key} = {strs:?}");
32                }
33                other => println!("{key} = {other:?}"),
34            }
35        }
36    }
37
38    // Direct value accessors
39    let name = value
40        .as_properties()
41        .and_then(|p| p.get("name").copied())
42        .and_then(|v| v.as_str());
43    println!("\nDirect access — name: {name:?}");
44
45    manager.close_evaluator(&evaluator)?;
46    Ok(())
47}