Skip to main content

plural_selectors/
plural_selectors.rs

1use std::collections::BTreeMap;
2
3use this_me::kernel::{Kernel, ParsedPath, Value};
4
5fn object(entries: impl IntoIterator<Item = (&'static str, Value)>) -> Value {
6    Value::Object(
7        entries
8            .into_iter()
9            .map(|(key, value)| (key.to_string(), value))
10            .collect::<BTreeMap<_, _>>(),
11    )
12}
13
14fn run() -> Result<(), Box<dyn std::error::Error>> {
15    let mut me = Kernel::new();
16
17    me.postulate("apps.demo.notes[alpha].title", "Alpha")?;
18    me.postulate("apps.demo.notes[beta].title", "Beta")?;
19    me.postulate("apps.demo.notes[beta].tags.primary", "work")?;
20
21    assert_eq!(
22        me.children("apps.demo.notes[]")?,
23        vec!["alpha".to_string(), "beta".to_string()]
24    );
25
26    assert_eq!(
27        me.read_public_subtree("apps.demo.notes[]")?,
28        Some(object([
29            ("alpha", object([("title", Value::from("Alpha"))])),
30            (
31                "beta",
32                object([
33                    ("tags", object([("primary", Value::from("work"))])),
34                    ("title", Value::from("Beta")),
35                ]),
36            ),
37        ]))
38    );
39
40    let parsed = ParsedPath::parse("apps.demo.notes[priority >= 2]")?;
41    assert_eq!(
42        parsed.normalized(),
43        vec![
44            "apps".to_string(),
45            "demo".to_string(),
46            "notes".to_string(),
47            "priority >= 2".to_string(),
48        ]
49    );
50
51    println!("notes[] members = {:?}", me.children("apps.demo.notes[]")?);
52    Ok(())
53}
54
55fn main() -> Result<(), Box<dyn std::error::Error>> {
56    run()
57}
58
59#[test]
60fn example_runs() {
61    run().unwrap();
62}