From 4d3c089158d44eb94142894543a1bb5b12f42ae4 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 24 Jul 2021 13:32:31 +0100 Subject: [PATCH] config, wip parsing Signed-off-by: Ian Jackson --- src/config.rs | 55 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/src/config.rs b/src/config.rs index b06b25d..9b0f184 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,14 +25,12 @@ pub struct CidrString(pub String); #[derive(hippotat_macros::ResolveConfig)] pub struct InstanceConfig { -/* // Exceptional settings - #[special(special_name, SKL::ServerName)] pub server: String, - pub secret: String, // xxx newytpe - #[special(special_ipif, SKL::Ordinary)] pub ipif: String, + #[special(special_server, SKL::ServerName)] pub server: ServerName, + pub secret: String, //xxx newytpe + #[special(special_ipif, SKL::Ordinary)] pub ipif: String, // Capped settings: -*/ #[limited] pub max_batch_down: u32, #[limited] pub max_queue_time: Duration, #[limited] pub http_timeout: Duration, @@ -81,6 +79,7 @@ pub struct Config { } static OUTSIDE_SECTION: &str = "["; +static SPECIAL_SERVER_SECTION: &str = "SERVER"; #[derive(Default,Debug)] struct Aggregate { @@ -264,6 +263,7 @@ struct ResolveContext<'c> { link: &'c LinkName, end: LinkEnd, all_sections: Vec, + special_server_section: SectionName, } trait Parseable: Sized { @@ -324,6 +324,12 @@ enum SectionKindList { } use SectionKindList as SKL; +impl SectionName { + fn special_server_section() -> Self { SN::Server(ServerName( + SPECIAL_SERVER_SECTION.into() + )) } +} + impl SectionKindList { fn contains(self, s: &SectionName) -> bool { match self { @@ -343,17 +349,17 @@ impl SectionKindList { SKL::ServerName => matches!(s, SN::Common) | matches!(s, SN::Server(ServerName(name)) - if name == "SERVER"), + if name == SPECIAL_SERVER_SECTION), } } } impl<'c> ResolveContext<'c> { - fn first_of_raw(&self, key: &'static str, sections: SectionKindList) - -> Option<&'c RawVal> { - for section in self.all_sections.iter() - .filter(|s| sections.contains(s)) - { + fn lookup_raw<'n, S>(&self, key: &'static str, sections: S) + -> Option<&'c RawVal> + where S: Iterator + { + for section in sections { if let Some(raw) = self.agg.sections .get(section) .and_then(|vars: &SectionMap| vars.get(key)) @@ -364,6 +370,15 @@ impl<'c> ResolveContext<'c> { None } + fn first_of_raw(&self, key: &'static str, sections: SectionKindList) + -> Option<&'c RawVal> { + self.lookup_raw( + key, + self.all_sections.iter() + .filter(|s| sections.contains(s)) + ) + } + #[throws(AE)] fn first_of(&self, key: &'static str, sections: SectionKindList) -> Option @@ -420,9 +435,7 @@ impl<'c> ResolveContext<'c> { } #[throws(AE)] - pub fn special_ipif(&self, key: &'static str) -> T - where T: Parseable + Default - { + pub fn special_ipif(&self, key: &'static str) -> String { match self.end { LinkEnd::Client => self.ordinary(key)?, LinkEnd::Server => { @@ -431,6 +444,19 @@ impl<'c> ResolveContext<'c> { }, } } + + #[throws(AE)] + pub fn special_server(&self, key: &'static str) -> ServerName { + let raw = match self.lookup_raw( + "server", + [ &SectionName::Common, &self.special_server_section ].iter().cloned() + ) { + Some(RawVal { val: Some(ref got),.. }) => got, + Some(RawVal { val: None,.. }) => throw!(anyhow!("value needed")), + None => SPECIAL_SERVER_SECTION, + }; + ServerName(raw.into()) + } } /* @@ -478,5 +504,6 @@ pub fn read() { SN::ServerLimit(link.server.clone()), SN::GlobalLimit, ], + special_server_section: SN::special_server_section(), }; } -- 2.30.2