From: Ian Jackson Date: Mon, 9 Jan 2023 18:56:40 +0000 (+0000) Subject: Config inspection: Introduce InspectableConfig trait X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=6213d1610ddc72002b2603f0fab20e66fa124b8d;p=hippotat.git Config inspection: Introduce InspectableConfig trait Signed-off-by: Ian Jackson --- diff --git a/macros/macros.rs b/macros/macros.rs index b030756..341edb2 100644 --- a/macros/macros.rs +++ b/macros/macros.rs @@ -169,16 +169,18 @@ pub fn resolve(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let global = syn::Ident::new(&format!("{}Global", top_ident), top_ident.span()); - let mk_inspects = |inspects: Vec<_>| quote! { - pub fn inspect_key(&self, field: &'_ str) - -> Option<&dyn InspectableConfigValue> { - Some(match field { - #( #inspects )* - _ => return None, - }) + let mk_inspects = |self_, inspects: Vec<_>| quote! { + impl InspectableConfig for #self_ { + fn inspect_key(&self, field: &'_ str) + -> Option<&dyn InspectableConfigValue> { + Some(match field { + #( #inspects )* + _ => return None, + }) + } } }; - let g_inspects = mk_inspects(g_inspects); + let g_inspects = mk_inspects(&global, g_inspects); let output = quote! { impl #target { @@ -203,9 +205,9 @@ pub fn resolve(input: proc_macro::TokenStream) -> proc_macro::TokenStream { pub fn from(l: &[#top_ident]) -> #global { #global { #( #global_assignments )* } } - - #g_inspects } + + #g_inspects }; //eprintln!("{}", &output); diff --git a/src/config.rs b/src/config.rs index b659ceb..7cc9adf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -90,6 +90,11 @@ pub struct Opts { pub extra_config: Vec, } +pub trait InspectableConfig { + fn inspect_key(&self, field: &'_ str) + -> Option<&dyn InspectableConfigValue>; +} + pub trait InspectableConfigValue { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result; } diff --git a/src/prelude.rs b/src/prelude.rs index 2570fcb..e38c3d5 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -55,7 +55,8 @@ pub use eyre::eyre as anyhow; pub use eyre::WrapErr; pub use eyre::Error as AE; -pub use crate::config::{self, InspectableConfigValue, InstanceConfig}; +pub use crate::config::{self, InstanceConfig}; +pub use crate::config::{InspectableConfig, InspectableConfigValue}; pub use crate::config::{DisplayInspectable, U32Ext as _}; pub use crate::impl_inspectable_config_value; pub use crate::ini;