This commit is contained in:
Max Känner 2024-05-13 22:01:08 +02:00
parent f5935b673b
commit 35afc0f484

View File

@ -2,10 +2,11 @@ use std::str::from_utf8;
use macroconf::config; use macroconf::config;
use miniconf::{Error::Absent, JsonCoreSlash, Tree, TreeKey}; use miniconf::{Error::Absent, JsonCoreSlash, Tree, TreeKey};
use serde::{Deserialize, Serialize};
#[config] #[config]
#[derive(Debug, Clone, Copy, Tree)] #[derive(Debug, Clone, Copy, Serialize, Deserialize, Tree)]
struct Config { struct SubConfig {
skipped: i32, skipped: i32,
#[min] #[min]
min: i32, min: i32,
@ -17,6 +18,13 @@ struct Config {
description: i32, description: i32,
} }
#[config]
#[derive(Debug, Clone, Copy, Tree)]
struct Config {
#[tree(depth(2))]
sub_config: SubConfig,
}
#[test] #[test]
fn keys() { fn keys() {
for (id, field) in ["skipped", "min", "max", "default", "description"] for (id, field) in ["skipped", "min", "max", "default", "description"]
@ -24,7 +32,7 @@ fn keys() {
.enumerate() .enumerate()
{ {
assert_eq!( assert_eq!(
Config::traverse_by_key(std::iter::once(field), |index, name| { SubConfig::traverse_by_key(std::iter::once(field), |index, name| {
assert_eq!((id, field), (index, name)); assert_eq!((id, field), (index, name));
Ok::<_, ()>(()) Ok::<_, ()>(())
}), }),
@ -36,16 +44,16 @@ fn keys() {
#[test] #[test]
fn sub_keys() { fn sub_keys() {
assert_eq!( assert_eq!(
Config::traverse_by_key(["skipped", "value"].into_iter(), |_, _| Ok::<_, ()>(())), SubConfig::traverse_by_key(["skipped", "value"].into_iter(), |_, _| Ok::<_, ()>(())),
Ok(1) Ok(1)
); );
for field in ["min", "max", "default", "description"] { for field in ["min", "max", "default", "description"] {
assert_eq!( assert_eq!(
Config::traverse_by_key([field, "value"].into_iter(), |_, _| Ok::<_, ()>(())), SubConfig::traverse_by_key([field, "value"].into_iter(), |_, _| Ok::<_, ()>(())),
Ok(2) Ok(2)
); );
assert_eq!( assert_eq!(
Config::traverse_by_key([field, field].into_iter(), |_, _| Ok::<_, ()>(())), SubConfig::traverse_by_key([field, field].into_iter(), |_, _| Ok::<_, ()>(())),
Ok(2) Ok(2)
); );
} }
@ -54,12 +62,12 @@ fn sub_keys() {
#[test] #[test]
fn serialize() { fn serialize() {
let mut buffer = [0u8; 32]; let mut buffer = [0u8; 32];
let config = Config { let config = SubConfig {
skipped: 1, skipped: 1,
min: __ConfigMin::new(2), min: __SubConfigMin::new(2),
max: __ConfigMax::new(3), max: __SubConfigMax::new(3),
default: __ConfigDefault::new(4), default: __SubConfigDefault::new(4),
description: __ConfigDescription::new(5), description: __SubConfigDescription::new(5),
}; };
for (input, output) in [ for (input, output) in [
@ -85,12 +93,12 @@ fn serialize() {
#[test] #[test]
fn deserialize() { fn deserialize() {
let mut config = Config { let mut config = SubConfig {
skipped: 0, skipped: 0,
min: __ConfigMin::new(0), min: __SubConfigMin::new(0),
max: __ConfigMax::new(0), max: __SubConfigMax::new(0),
default: __ConfigDefault::new(0), default: __SubConfigDefault::new(0),
description: __ConfigDescription::new(0), description: __SubConfigDescription::new(0),
}; };
for input in [ for input in [
@ -118,3 +126,22 @@ fn deserialize() {
assert_eq!(res, Err(Absent(1))); assert_eq!(res, Err(Absent(1)));
} }
} }
#[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(),
];
let paths: Vec<String> = Config::iter_paths::<String>("/")
.filter_map(|path| path.ok())
.collect();
assert_eq!(paths, control);
}