From 844b94e6f25bbdd3c72e2aee0618c95de6d98d4c Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 24 Jul 2021 01:04:56 +0100 Subject: [PATCH] wip resolve Signed-off-by: Ian Jackson --- src/config.rs | 57 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/src/config.rs b/src/config.rs index 9bcf41d..bbcb730 100644 --- a/src/config.rs +++ b/src/config.rs @@ -258,6 +258,9 @@ struct ResolveContext<'c> { agg: &'c Aggregate, link: &'c LinkName, end: LinkEnd, + lookup_ordinary: Vec, + lookup_limit: Vec, + lookup_server_sections_only: Vec, } trait Parseable: Sized { @@ -265,12 +268,11 @@ trait Parseable: Sized { } impl<'c> ResolveContext<'c> { - fn first_of_raw(&self, key: &'static str, - sections: &[ &dyn Fn() -> SectionName ]) + fn first_of_raw(&self, key: &'static str, sections: &[SectionName]) -> Option<&'c RawVal> { for section in sections { if let Some(raw) = self.agg.sections - .get(§ion()) + .get(section) .and_then(|vars: &SectionMap| vars.get(key)) { return Some(raw) @@ -280,8 +282,7 @@ impl<'c> ResolveContext<'c> { } #[throws(AE)] - fn first_of(&self, key: &'static str, - sections: &[ &dyn Fn() -> SectionName ]) + fn first_of(&self, key: &'static str, sections: &[SectionName]) -> Option where T: Parseable { @@ -300,12 +301,7 @@ impl<'c> ResolveContext<'c> { 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, - ])? + self.first_of(key, &self.lookup_ordinary)? .unwrap_or_default() } @@ -314,10 +310,7 @@ impl<'c> ResolveContext<'c> { where T: Parseable + Default + Ord { let val = self.ordinary(key)?; - if let Some(limit) = self.first_of(key, &[ - &|| SN::ServerLimit(self.link.server.clone()), - &|| SN::GlobalLimit, - ])? { + if let Some(limit) = self.first_of(key, &self.lookup_limit)? { min(val, limit) } else { val @@ -348,10 +341,7 @@ impl<'c> ResolveContext<'c> { match self.end { LinkEnd::Client => self.ordinary(key)?, LinkEnd::Server => { - self.first_of(key, &[ - &|| SN::Common, - &|| SN::Server(self.link.server.clone()), - ])? + self.first_of(key, &self.lookup_server_sections_only)? .unwrap_or_default() }, } @@ -370,7 +360,7 @@ fn resolve_instance_config() { pub fn read() { let opts = config::Opts::from_args(); - (||{ + let agg = (||{ let mut agg = Aggregate::default(); agg.read_toplevel(&opts.config)?; @@ -380,6 +370,31 @@ pub fn read() { eprintln!("GOT {:#?}", agg); - Ok::<_,AE>(()) + Ok::<_,AE>(agg) })().context("read configuration")?; + + let link = LinkName { + server: "fooxxx".parse().unwrap(), + client: "127.0.0.1".parse().unwrap(), + }; + + let rctx = ResolveContext { + agg: &agg, + link: &link, + end: LinkEnd::Server, + lookup_ordinary: vec![ + SN::Link(link.clone()), + SN::Client(link.client.clone()), + SN::Server(link.server.clone()), + SN::Common, + ], + lookup_limit: vec![ + SN::ServerLimit(link.server.clone()), + SN::GlobalLimit, + ], + lookup_server_sections_only: vec![ + SN::Common, + SN::Server(link.server.clone()), + ], + }; } -- 2.30.2