From d84625a0ecda4d999ed7ee22f21a356cec5a5e23 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 24 Jul 2021 19:55:55 +0100 Subject: [PATCH] config: check key sections Signed-off-by: Ian Jackson --- src/config.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index 28b2754..34b9a0c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -153,6 +153,7 @@ static SPECIAL_SERVER_SECTION: &str = "SERVER"; #[derive(Default,Debug)] struct Aggregate { + keys_allowed: HashMap<&'static str, SectionKindList>, sections: HashMap, } @@ -234,9 +235,18 @@ impl Aggregate { let loc = Arc::new(path_for_loc.to_owned()); for (sn, vars) in map { - //dbg!( InstanceConfig::FIELDS );// check xxx vars are in fields - let sn = sn.parse().dcontext(&sn)?; + + for key in vars.keys() { + let skl = self.keys_allowed.get(key.as_str()).ok_or_else( + || anyhow!("unknown configuration key {:?}", key) + )?; + if ! skl.contains(&sn) { + throw!(anyhow!("configuration key {:?} not applicable \ + in this kind of section {:?}", key, &sn)) + } + } + let ent = self.sections.entry(sn).or_default(); for (key, raw) in vars { let raw = match raw { @@ -678,6 +688,9 @@ pub fn read(end: LinkEnd) -> Vec { let agg = (||{ let mut agg = Aggregate::default(); + agg.keys_allowed.extend( + InstanceConfig::FIELDS.iter().cloned() + ); agg.read_string(DEFAULT_CONFIG.into(), "".as_ref()).unwrap(); -- 2.30.2