From 8385382da77b331d6b670c5d291349d1fb21dc92 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 10 Jan 2023 00:34:15 +0000 Subject: [PATCH] Config inspection: Reorganise implement_print_config We want this to be able to - handle out-of-course keys - do word splitting in the implementaton Signed-off-by: Ian Jackson --- client/client.rs | 2 +- server/server.rs | 2 +- src/config.rs | 21 ++++++++++----------- src/prelude.rs | 2 +- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/client/client.rs b/client/client.rs index 0f3cbbf..96476c8 100644 --- a/client/client.rs +++ b/client/client.rs @@ -332,7 +332,7 @@ async fn main() { &opts.config, &opts.log, |ics| { if let Some(arg) = &opts.print_config { for ic in &ics { - ic.implement_print_config(arg)?; + implement_print_config(arg, &|k| ic.inspect_key(k))?; } process::exit(0); } diff --git a/server/server.rs b/server/server.rs index ff69b0b..0b9ffed 100644 --- a/server/server.rs +++ b/server/server.rs @@ -161,7 +161,7 @@ async fn async_main(opts: Opts, daemon: Option) { let global_config = config::InstanceConfigGlobal::from(&ics); if let Some(arg) = &opts.print_config { - global_config.implement_print_config(arg)?; + implement_print_config(arg, &|k| global_config.inspect_key(k))?; process::exit(0); } diff --git a/src/config.rs b/src/config.rs index 1ecc479..fff0546 100644 --- a/src/config.rs +++ b/src/config.rs @@ -93,18 +93,17 @@ pub struct Opts { pub trait InspectableConfig { fn inspect_key(&self, field: &'_ str) -> Option<&dyn InspectableConfigValue>; +} - #[throws(AE)] - fn implement_print_config(&self, key: &str) where Self: Sized { - #[throws(AE)] - fn inner(self_: &dyn InspectableConfig, key: &str) { - if let Some(inspectable) = self_.inspect_key(key) { - println!("{}", DisplayInspectable(inspectable)); - } else { - throw!(anyhow!("unknown config key {:?}", key)); - } - } - inner(self, key)?; +#[throws(AE)] +pub fn implement_print_config<'f>( + key: &str, + inspect_key: &dyn Fn(&str) -> Option<&'f dyn InspectableConfigValue> +) { + if let Some(inspectable) = inspect_key(key) { + println!("{}", DisplayInspectable(inspectable)); + } else { + throw!(anyhow!("unknown config key {:?}", key)); } } diff --git a/src/prelude.rs b/src/prelude.rs index e38c3d5..c4ee088 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -55,7 +55,7 @@ pub use eyre::eyre as anyhow; pub use eyre::WrapErr; pub use eyre::Error as AE; -pub use crate::config::{self, InstanceConfig}; +pub use crate::config::{self, InstanceConfig, implement_print_config}; pub use crate::config::{InspectableConfig, InspectableConfigValue}; pub use crate::config::{DisplayInspectable, U32Ext as _}; pub use crate::impl_inspectable_config_value; -- 2.30.2