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