From 274949c9994479551c2cde5d651e1c76fce32a18 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 24 Jul 2021 11:14:51 +0100 Subject: [PATCH] config, break out var section list Signed-off-by: Ian Jackson --- macros/macros.rs | 2 ++ src/config.rs | 60 +++++++++++++++++++++++++++++++++++------------- src/lib.rs | 5 ++++ 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/macros/macros.rs b/macros/macros.rs index 0bcfc33..be1dee1 100644 --- a/macros/macros.rs +++ b/macros/macros.rs @@ -2,6 +2,8 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // There is NO WARRANTY. +#![allow(unused_imports)] // xxx + use syn::{parse_macro_input, Data, DataStruct, DeriveInput}; use quote::{quote, quote_spanned}; use proc_macro2::Literal; diff --git a/src/config.rs b/src/config.rs index 8ad04e8..e72f998 100644 --- a/src/config.rs +++ b/src/config.rs @@ -264,9 +264,7 @@ struct ResolveContext<'c> { agg: &'c Aggregate, link: &'c LinkName, end: LinkEnd, - lookup_ordinary: Vec, - lookup_limit: Vec, - lookup_server_sections_only: Vec, + all_sections: Vec, } trait Parseable: Sized { @@ -292,10 +290,46 @@ macro_rules! parseable_from_str { ($t:ty) => { } } parseable_from_str!{u32} +#[derive(Debug,Copy,Clone)] +enum SectionKindList { + Ordinary, + Limited, + Limits, + ClientAgnostic, + ServerName, +} +use SectionKindList as SKL; + +impl SectionKindList { + fn contains(self, s: &SectionName) -> bool { + match self { + SKL::Ordinary => matches!(s, SN::Link(_) + | SN::Client(_) + | SN::Server(_) + | SN::Common), + + SKL::Limits => matches!(s, SN::ServerLimit(_) + | SN::GlobalLimit), + + SKL::ClientAgnostic => matches!(s, SN::Common + | SN::Server(_)), + + SKL::Limited => SKL::Ordinary.contains(s) + | SKL::Limits .contains(s), + + SKL::ServerName => matches!(s, SN::Common) + | matches!(s, SN::Server(ServerName(name)) + if name == "SERVER"), + } + } +} + impl<'c> ResolveContext<'c> { - fn first_of_raw(&self, key: &'static str, sections: &[SectionName]) + fn first_of_raw(&self, key: &'static str, sections: SectionKindList) -> Option<&'c RawVal> { - for section in sections { + for section in self.all_sections.iter() + .filter(|s| sections.contains(s)) + { if let Some(raw) = self.agg.sections .get(section) .and_then(|vars: &SectionMap| vars.get(key)) @@ -307,7 +341,7 @@ impl<'c> ResolveContext<'c> { } #[throws(AE)] - fn first_of(&self, key: &'static str, sections: &[SectionName]) + fn first_of(&self, key: &'static str, sections: SectionKindList) -> Option where T: Parseable { @@ -326,7 +360,7 @@ impl<'c> ResolveContext<'c> { pub fn ordinary(&self, key: &'static str) -> T where T: Parseable + Default { - self.first_of(key, &self.lookup_ordinary)? + self.first_of(key, SKL::Ordinary)? .unwrap_or_default() } @@ -335,7 +369,7 @@ impl<'c> ResolveContext<'c> { where T: Parseable + Default + Ord { let val = self.ordinary(key)?; - if let Some(limit) = self.first_of(key, &self.lookup_limit)? { + if let Some(limit) = self.first_of(key, SKL::Limits)? { min(val, limit) } else { val @@ -366,7 +400,7 @@ impl<'c> ResolveContext<'c> { match self.end { LinkEnd::Client => self.ordinary(key)?, LinkEnd::Server => { - self.first_of(key, &self.lookup_server_sections_only)? + self.first_of(key, SKL::ClientAgnostic)? .unwrap_or_default() }, } @@ -408,19 +442,13 @@ pub fn read() { agg: &agg, link: &link, end: LinkEnd::Server, - lookup_ordinary: vec![ + all_sections: 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()), - ], }; } diff --git a/src/lib.rs b/src/lib.rs index 1f6a3cd..42196f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,11 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // There is NO WARRANTY. +#![allow(unused_imports)] // xxx +#![allow(unreachable_code)] // xxx +#![allow(dead_code)] // xxx +#![allow(unused_variables)] // xxx + #![feature(io_error_more)] // EK::IsADirectory pub mod prelude; -- 2.30.2