2024-04-15 22:35:27 +02:00
|
|
|
use std::str::from_utf8;
|
|
|
|
|
|
|
|
use macroconf::config;
|
2024-07-25 12:54:33 +02:00
|
|
|
use miniconf::{
|
|
|
|
Error::Traversal,
|
|
|
|
IntoKeys, JsonCoreSlash, Path,
|
|
|
|
Traversal::{Absent, TooLong},
|
|
|
|
Tree, TreeKey,
|
|
|
|
};
|
2024-05-13 22:01:08 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-04-15 22:35:27 +02:00
|
|
|
|
|
|
|
#[config]
|
2024-05-13 22:01:08 +02:00
|
|
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Tree)]
|
|
|
|
struct SubConfig {
|
2024-04-15 22:35:27 +02:00
|
|
|
skipped: i32,
|
|
|
|
#[min]
|
|
|
|
min: i32,
|
|
|
|
#[max]
|
|
|
|
max: i32,
|
|
|
|
#[default = 0]
|
|
|
|
default: i32,
|
|
|
|
/// This is a description
|
|
|
|
description: i32,
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:01:08 +02:00
|
|
|
#[config]
|
|
|
|
#[derive(Debug, Clone, Copy, Tree)]
|
|
|
|
struct Config {
|
2024-07-25 12:54:33 +02:00
|
|
|
#[tree(depth = 2)]
|
2024-05-13 22:01:08 +02:00
|
|
|
sub_config: SubConfig,
|
|
|
|
}
|
|
|
|
|
2024-04-15 22:35:27 +02:00
|
|
|
#[test]
|
|
|
|
fn keys() {
|
|
|
|
for (id, field) in ["skipped", "min", "max", "default", "description"]
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
{
|
|
|
|
assert_eq!(
|
2024-07-25 12:54:33 +02:00
|
|
|
SubConfig::traverse_by_key([field].into_keys(), |index, name, _len| {
|
|
|
|
assert_eq!((id, Some(field)), (index, name));
|
2024-04-15 22:35:27 +02:00
|
|
|
Ok::<_, ()>(())
|
|
|
|
}),
|
|
|
|
Ok(1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sub_keys() {
|
|
|
|
assert_eq!(
|
2024-07-25 12:54:33 +02:00
|
|
|
SubConfig::traverse_by_key(["skipped", "value"].into_keys(), |_, _, _| Ok::<_, ()>(())),
|
|
|
|
Err(Traversal(TooLong(1)))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
SubConfig::traverse_by_key(["skipped"].into_keys(), |_, _, _| Ok::<_, ()>(())),
|
2024-04-15 22:35:27 +02:00
|
|
|
Ok(1)
|
|
|
|
);
|
|
|
|
for field in ["min", "max", "default", "description"] {
|
|
|
|
assert_eq!(
|
2024-07-25 12:54:33 +02:00
|
|
|
SubConfig::traverse_by_key([field, "value"].into_keys(), |_, _, _| Ok::<_, ()>(())),
|
2024-04-15 22:35:27 +02:00
|
|
|
Ok(2)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-07-25 12:54:33 +02:00
|
|
|
SubConfig::traverse_by_key([field, field].into_keys(), |_, _, _| Ok::<_, ()>(())),
|
2024-04-15 22:35:27 +02:00
|
|
|
Ok(2)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize() {
|
|
|
|
let mut buffer = [0u8; 32];
|
2024-05-13 22:01:08 +02:00
|
|
|
let config = SubConfig {
|
2024-04-15 22:35:27 +02:00
|
|
|
skipped: 1,
|
2024-05-13 22:01:08 +02:00
|
|
|
min: __SubConfigMin::new(2),
|
|
|
|
max: __SubConfigMax::new(3),
|
|
|
|
default: __SubConfigDefault::new(4),
|
|
|
|
description: __SubConfigDescription::new(5),
|
2024-04-15 22:35:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for (input, output) in [
|
|
|
|
("/skipped", "1"),
|
|
|
|
("/min", "2"),
|
|
|
|
("/min/value", "2"),
|
|
|
|
("/min/min", "-2147483648"),
|
|
|
|
("/max", "3"),
|
|
|
|
("/max/value", "3"),
|
|
|
|
("/max/max", "2147483647"),
|
|
|
|
("/default", "4"),
|
|
|
|
("/default/value", "4"),
|
|
|
|
("/default/default", "0"),
|
|
|
|
("/description", "5"),
|
|
|
|
("/description/value", "5"),
|
|
|
|
("/description/description", "\"This is a description\""),
|
|
|
|
] {
|
|
|
|
let res = config.get_json(input, &mut buffer);
|
|
|
|
assert_eq!(res, Ok(output.len()));
|
|
|
|
assert_eq!(from_utf8(&buffer[..output.len()]), Ok(output));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize() {
|
2024-05-13 22:01:08 +02:00
|
|
|
let mut config = SubConfig {
|
2024-04-15 22:35:27 +02:00
|
|
|
skipped: 0,
|
2024-05-13 22:01:08 +02:00
|
|
|
min: __SubConfigMin::new(0),
|
|
|
|
max: __SubConfigMax::new(0),
|
|
|
|
default: __SubConfigDefault::new(0),
|
|
|
|
description: __SubConfigDescription::new(0),
|
2024-04-15 22:35:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for input in [
|
|
|
|
"/skipped",
|
|
|
|
"/min",
|
|
|
|
"/min/value",
|
|
|
|
"/max",
|
|
|
|
"/max/value",
|
|
|
|
"/default",
|
|
|
|
"/default/value",
|
|
|
|
"/description",
|
|
|
|
"/description/value",
|
|
|
|
] {
|
|
|
|
let res = config.set_json(input, b"10");
|
|
|
|
assert_eq!(res, Ok(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
for input in [
|
|
|
|
"/min/min",
|
|
|
|
"/max/max",
|
|
|
|
"/default/default",
|
|
|
|
"/description/description",
|
|
|
|
] {
|
|
|
|
let res = config.set_json(input, b"10");
|
2024-07-25 12:54:33 +02:00
|
|
|
assert_eq!(res, Err(Traversal(Absent(1))));
|
2024-04-15 22:35:27 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-13 22:01:08 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subconfig() {
|
|
|
|
let control = vec![
|
|
|
|
"/sub_config/skipped".to_owned(),
|
|
|
|
"/sub_config/min/value".to_owned(),
|
|
|
|
"/sub_config/min/min".to_owned(),
|
|
|
|
"/sub_config/max/value".to_owned(),
|
|
|
|
"/sub_config/max/max".to_owned(),
|
|
|
|
"/sub_config/default/value".to_owned(),
|
|
|
|
"/sub_config/default/default".to_owned(),
|
|
|
|
"/sub_config/description/value".to_owned(),
|
|
|
|
"/sub_config/description/description".to_owned(),
|
|
|
|
];
|
2024-07-25 12:54:33 +02:00
|
|
|
let paths: Vec<String> = Config::nodes::<Path<String, '/'>>()
|
|
|
|
.filter_map(|path| path.ok().map(|(n, _)| n.0))
|
2024-05-13 22:01:08 +02:00
|
|
|
.collect();
|
|
|
|
assert_eq!(paths, control);
|
|
|
|
}
|