From: Ian Jackson Date: Tue, 10 Jan 2023 20:37:22 +0000 (+0000) Subject: Config inspection: Move extra keys into trait X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=96c2a63ff75ee27b2b6a44c26ef974a58a8de40b;p=hippotat.git Config inspection: Move extra keys into trait This involves splitting the trait. I've chosen the structure to minimise work done by the macro. Signed-off-by: Ian Jackson --- diff --git a/client/client.rs b/client/client.rs index 627a799..2f284c7 100644 --- a/client/client.rs +++ b/client/client.rs @@ -338,13 +338,7 @@ async fn main() { let opts = ::parse(); let (ics,) = config::startup("hippotat", LinkEnd::Client, &opts.config, &opts.log, |ics| { - implement_print_config(&mut ics.iter(), &opts.print_config, - &|ic, k| Some(match k { - "link" => &ic.link, - "server" => &ic.link.server, - "client" => &ic.link.client, - k => return ic.inspect_key(k), - }))?; + implement_print_config(&mut ics.iter(), &opts.print_config)?; Ok((ics,)) }); diff --git a/macros/macros.rs b/macros/macros.rs index 4303f00..d010459 100644 --- a/macros/macros.rs +++ b/macros/macros.rs @@ -172,9 +172,9 @@ pub fn resolve(input: proc_macro::TokenStream) -> proc_macro::TokenStream { top_ident.span()); let mk_inspects = |self_, inspects: Vec<_>| quote! { - impl InspectableConfig for #self_ { - fn inspect_key(&self, field: &'_ str) - -> Option<&dyn InspectableConfigValue> { + impl InspectableConfigAuto for #self_ { + fn inspect_key_auto(&self, field: &'_ str) + -> Option<&dyn InspectableConfigValue> { Some(match field { #( #inspects )* _ => return None, diff --git a/server/server.rs b/server/server.rs index 97aa326..3a3643c 100644 --- a/server/server.rs +++ b/server/server.rs @@ -165,7 +165,7 @@ async fn async_main(opts: Opts, daemon: Option) { let global_config = config::InstanceConfigGlobal::from(&ics); implement_print_config(&mut iter::once(&global_config), - &opts.print_config, &|_,__| None)?; + &opts.print_config)?; if let Some(pidfile_path) = opts.pidfile.as_ref() { (||{ diff --git a/src/config.rs b/src/config.rs index 2d37d6e..711ede7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -90,22 +90,43 @@ pub struct Opts { pub extra_config: Vec, } +pub trait InspectableConfigAuto { + fn inspect_key_auto(&self, field: &'_ str) + -> Option<&dyn InspectableConfigValue>; +} pub trait InspectableConfig: Debug { fn inspect_key(&self, field: &'_ str) -> Option<&dyn InspectableConfigValue>; } +impl InspectableConfig for InstanceConfigGlobal { + fn inspect_key(&self, field: &'_ str) + -> Option<&dyn InspectableConfigValue> { + self.inspect_key_auto(field) + } +} + +impl InspectableConfig for InstanceConfig { + fn inspect_key(&self, field: &'_ str) + -> Option<&dyn InspectableConfigValue> { + Some(match field { + "link" => &self.link, + "server" => &self.link.server, + "client" => &self.link.client, + k => return self.inspect_key_auto(k), + }) + } +} + #[throws(AE)] -pub fn implement_print_config<'c, C: InspectableConfig>( +pub fn implement_print_config<'c, C: InspectableConfig + 'c>( configs: impl Iterator, arg: &Option, - extra_key: &dyn Fn(&'c C, &str) -> Option<&'c dyn InspectableConfigValue> ) { #[throws(AE)] pub fn print_one_config<'f>( config: &dyn InspectableConfig, arg: &str, - extra_key: &dyn Fn(&str) -> Option<&'f dyn InspectableConfigValue> ) { let output = arg .split(',') @@ -114,7 +135,6 @@ pub fn implement_print_config<'c, C: InspectableConfig>( return Ok(format!("{:#?}", &config)); } let insp = config.inspect_key(key) - .or_else(|| extra_key(key)) .ok_or_else(|| anyhow!("unknown config key {:?}", key))?; Ok::<_,AE>(DisplayInspectable(insp).to_string()) }) @@ -125,7 +145,7 @@ pub fn implement_print_config<'c, C: InspectableConfig>( if let Some(arg) = arg { for config in configs { - print_one_config(config, arg, &|k| extra_key(config, k))?; + print_one_config(config, arg)?; } process::exit(0); }