path_value/
lib.rs

1#![allow(dead_code)]
2#![allow(unused_variables)]
3#![allow(unknown_lints)]
4
5#[macro_use]
6extern crate pest_derive;
7#[macro_use]
8extern crate serde;
9
10pub use error::Error;
11pub use value::to_value;
12pub use value::Value;
13
14mod error;
15mod path;
16mod value;
17
18#[cfg(test)]
19mod tests {
20    use crate::{to_value, Value};
21
22    #[test]
23    fn simple_test() {
24        let mut value_origin = Value::default();
25
26        assert!(matches!(value_origin.set("/test/bool", true), Ok(_)));
27        assert!(matches!(
28            value_origin.set("/test/str", "i am string"),
29            Ok(_)
30        ));
31
32        assert!(matches!(value_origin.get("/test/bool"), Ok(Some(true))));
33        assert!(
34            matches!(value_origin.get::<String, _, _>("/test/str"), Ok(Some(str)) if str == "i am string")
35        );
36
37        let mut value_new = Value::default();
38
39        assert!(matches!(value_new.set("/test/bool", false), Ok(_)));
40        assert!(matches!(value_new.set("/test/i32", 1000_i32), Ok(_)));
41
42        assert!(matches!(value_origin.merge(value_new), Ok(_)));
43
44        assert!(matches!(value_origin.get("/test/bool"), Ok(Some(false))));
45        assert!(
46            matches!(value_origin.get::<String, _, _>("/test/str"), Ok(Some(str)) if str == "i am string")
47        );
48        assert!(matches!(value_origin.get("/test/i32"), Ok(Some(1000_i32))));
49    }
50
51    #[test]
52    fn simple_to_value_test() {
53        let bool_value = true;
54        assert!(
55            matches!(to_value(bool_value), Ok(v) if matches!(v.get::<bool, _, _>("/fake_path"), Ok(None)))
56        );
57        let i32_value = 1000_i32;
58        assert!(
59            matches!(to_value(i32_value), Ok(v) if matches!(v.get::<i32, _, _>("/fake_path"), Ok(None)))
60        );
61        let str_value = "i am string";
62        assert!(
63            matches!(to_value(str_value), Ok(v) if matches!(v.get::<String, _, _>("/fake_path"), Ok(None)))
64        );
65
66        let value = to_value(str_value);
67        assert!(matches!(to_value(str_value), Ok(_)));
68
69        let mut value = value.unwrap();
70
71        // should override origin value inside the value
72        assert!(matches!(value.set("/test/bool", false), Ok(_)));
73        assert!(matches!(value.get("/test/bool"), Ok(Some(false))));
74    }
75}