use miniconf::{json, Deserialize, Leaf, Metadata, Serialize, Tree, TreeKey}; #[test] fn generic_type() { #[derive(Tree, Default)] struct Settings { pub data: Leaf, } let mut settings = Settings::::default(); json::set(&mut settings, "/data", b"3.0").unwrap(); assert_eq!(*settings.data, 3.0); // Test metadata let metadata = Settings::::traverse_all::().unwrap(); assert_eq!(metadata.max_depth, 1); assert_eq!(metadata.max_length, "data".len()); assert_eq!(metadata.count.get(), 1); } #[test] fn generic_array() { #[derive(Tree, Default)] struct Settings { pub data: [Leaf; 2], } let mut settings = Settings::::default(); json::set(&mut settings, "/data/0", b"3.0").unwrap(); assert_eq!(*settings.data[0], 3.0); // Test metadata let metadata = Settings::::traverse_all::().unwrap(); assert_eq!(metadata.max_depth, 2); assert_eq!(metadata.max_length("/"), "/data/0".len()); assert_eq!(metadata.count.get(), 2); } #[test] fn generic_struct() { #[derive(Tree, Default)] struct Settings { pub inner: Leaf, } #[derive(Serialize, Deserialize, Default)] struct Inner { pub data: f32, } let mut settings = Settings::::default(); json::set(&mut settings, "/inner", b"{\"data\": 3.0}").unwrap(); assert_eq!(settings.inner.data, 3.0); // Test metadata let metadata = Settings::::traverse_all::().unwrap(); assert_eq!(metadata.max_depth, 1); assert_eq!(metadata.max_length("/"), "/inner".len()); assert_eq!(metadata.count.get(), 1); } #[test] fn generic_atomic() { #[derive(Tree, Default)] struct Settings { atomic: Leaf>, opt: [[Leaf>; 1]; 1], opt1: [[Option>; 1]; 1], } #[derive(Deserialize, Serialize, Default)] struct Inner { inner: [T; 5], } let mut settings = Settings::::default(); json::set(&mut settings, "/atomic", b"{\"inner\": [3.0, 0, 0, 0, 0]}").unwrap(); assert_eq!(settings.atomic.inner[0], 3.0); // Test metadata let metadata = Settings::::traverse_all::().unwrap(); assert_eq!(metadata.max_depth, 3); assert_eq!(metadata.max_length("/"), "/opt1/0/0".len()); } #[test] fn test_depth() { #[derive(Tree)] struct S(Option>); // This works as array implements TreeKey S::<[Leaf; 1]>::traverse_all::().unwrap(); // This does not compile as u32 does not implement TreeKey // S::::traverse_all::(); }