From 59967ea2efdbc418a3e9b3a6c7f8f5f430424126 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 24 Jul 2021 00:57:11 +0100 Subject: [PATCH] wip resolve Signed-off-by: Ian Jackson --- src/config.rs | 69 +++++++++++++++++++++++++++++---------------------- src/types.rs | 6 +++++ 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/config.rs b/src/config.rs index 60c090b..9bcf41d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -69,12 +69,6 @@ pub enum SectionName { } pub use SectionName as SN; -#[derive(Debug,Clone,Hash,Eq,PartialEq)] -struct LinkName { - server: ServerName, - client: ClientName, -} - #[derive(Debug,Clone)] struct RawVal { val: Option, loc: Arc } type SectionMap = HashMap; @@ -266,8 +260,12 @@ struct ResolveContext<'c> { end: LinkEnd, } +trait Parseable: Sized { + fn parse(s: &Option) -> Result; +} + impl<'c> ResolveContext<'c> { - fn first_of_raw(&self, key: &str, + fn first_of_raw(&self, key: &'static str, sections: &[ &dyn Fn() -> SectionName ]) -> Option<&'c RawVal> { for section in sections { @@ -282,66 +280,79 @@ impl<'c> ResolveContext<'c> { } #[throws(AE)] - fn first_of(&self, key: &str, + fn first_of(&self, key: &'static str, sections: &[ &dyn Fn() -> SectionName ]) -> Option - where T: FromStr + where T: Parseable { match self.first_of_raw(key, sections) { None => None, Some(raw) => Some({ - raw.val.parse() + Parseable::parse(&raw.val) .context(key) - .context(r#"in section "{}""#, &raw.section) +// .with_context(|| format!(r#"in section "{}""#, §ion)) .dcontext(&raw.loc)? }), } } - pub fn ordinary(&self, key: &str) -> T where T: FromStr + Default { + #[throws(AE)] + pub fn ordinary(&self, key: &'static str) -> T + where T: Parseable + Default + { self.first_of(key, &[ - || SN::Link(self.link.clone()), - || SN::Client(self.link.client.clone()), - || SN::Server(self.link.server.clone()), - || SN::Common, + &|| SN::Link(self.link.clone()), + &|| SN::Client(self.link.client.clone()), + &|| SN::Server(self.link.server.clone()), + &|| SN::Common, ])? .unwrap_or_default() } - pub fn limited(&self, key: &str) -> T - where T: FromStr + Default + PartialOrd + #[throws(AE)] + pub fn limited(&self, key: &'static str) -> T + where T: Parseable + Default + Ord { - let val = self.ordinary(key); + let val = self.ordinary(key)?; if let Some(limit) = self.first_of(key, &[ - || SN::LimitServer(self.link.server.clone()), - || SN::LimitGlobal, + &|| SN::ServerLimit(self.link.server.clone()), + &|| SN::GlobalLimit, ])? { - val = min(val, limit) + min(val, limit) + } else { + val } - val } - pub fn client(&self, key: &str) -> T where T: FromStr + Default { + #[throws(AE)] + pub fn client(&self, key: &'static str) -> T + where T: Parseable + Default { match self.end { LinkEnd::Client => self.ordinary(key)?, LinkEnd::Server => default(), } } - pub fn server(&self, key: &str) -> T where T: FromStr + Default { + #[throws(AE)] + pub fn server(&self, key: &'static str) -> T + where T: Parseable + Default { match self.end { LinkEnd::Server => self.ordinary(key)?, LinkEnd::Client => default(), } } - pub fn special_ipif(&self, key: &str) -> T where T: FromStr + Default { + #[throws(AE)] + pub fn special_ipif(&self, key: &'static str) -> T + where T: Parseable + Default + { match self.end { LinkEnd::Client => self.ordinary(key)?, LinkEnd::Server => { self.first_of(key, &[ - || SN::Common, - || SN::Server(self.link.server.clone()), + &|| SN::Common, + &|| SN::Server(self.link.server.clone()), ])? + .unwrap_or_default() }, } } @@ -350,7 +361,7 @@ impl<'c> ResolveContext<'c> { /* fn resolve_instance_config() { InstanceConfig { - max_batch_down: resolve::limited(&agg, "max_batch_down") + max_batch_down: resolve::limited(&agg, "max_batch_down")?.into() } } */ diff --git a/src/types.rs b/src/types.rs index 86652e4..a18ee8b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -10,6 +10,12 @@ pub struct ServerName(pub String); #[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)] pub struct ClientName(pub Ipv4Addr); +#[derive(Debug,Clone,Hash,Eq,PartialEq)] +pub struct LinkName { + pub server: ServerName, + pub client: ClientName, +} + impl FromStr for ClientName { type Err = AE; #[throws(AE)] -- 2.30.2