chiark / gitweb /
config: inspectable config keys
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 25 Sep 2022 18:44:52 +0000 (19:44 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 25 Sep 2022 18:44:52 +0000 (19:44 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
macros/macros.rs
src/config.rs
src/prelude.rs
src/types.rs

index 1eaa7580dfb68604a30a28a941179f6296d9ed5f..1a158a7d10824e063a94c9189bad2ab19b61299d 100644 (file)
@@ -79,6 +79,7 @@ pub fn resolve(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
   let mut output = vec![];
   let mut global_fields = vec![];
   let mut global_assignments = vec![];
+  let mut inspects = vec![];
   for field in &fields.named {
     //dbg!(field);
     let fname = &field.ident.as_ref().unwrap();
@@ -146,6 +147,9 @@ pub fn resolve(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
     output.push(quote!{
       #fname: rctx. #method ( #fname_lit, #skl )?,
     });
+    inspects.push(quote!{
+      #fname_lit => &self.#fname,
+    });
     //eprintln!("{:?} method={:?} skl={:?}", field.ident, method, skl);
   }
   //dbg!(&output);
@@ -165,6 +169,14 @@ pub fn resolve(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
           #( #output )*
         })
       }
+
+      pub fn inspect_key(&self, field: &'_ str)
+                         -> Option<&dyn InspectableConfigValue> {
+        Some(match field {
+          #( #inspects )*
+          _ => return None,
+        })
+      }
     }
 
     #[derive(Debug)]
index 648baf3bec809f461ec77ec40673ec642e65ace2..f3dfe4907eb2f7232ec3926e75c45de564320d2c 100644 (file)
@@ -88,6 +88,54 @@ pub struct Opts {
   /// Additional config files or dirs, which can override the others
   #[structopt(long, multiple=true, number_of_values=1)]
   pub extra_config: Vec<PathBuf>,
+  
+  /// Print configuration rather than running
+  #[structopt(long)]
+  pub print_config: bool,
+}
+
+pub trait InspectableConfigValue {
+  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result;
+}
+#[macro_export]
+macro_rules! impl_inspectable_config_value {
+  { $t:ty as $trait:path } => {
+    impl InspectableConfigValue for $t {
+      fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        <Self as $trait>::fmt(self, f)
+      }
+    }
+  };
+  
+  { Vec<$t:ty> } => {
+    impl InspectableConfigValue for Vec<$t> {
+      #[throws(fmt::Error)]
+      fn fmt(&self, f: &mut fmt::Formatter) {
+        let mut first = Some(());
+        for v in self.iter() {
+          if first.take().is_none() { write!(f, " ")?; }
+          InspectableConfigValue::fmt(v, f)?;
+        }
+      }
+    }
+  };
+}
+
+impl_inspectable_config_value!{ String as Display }
+impl_inspectable_config_value!{ u16 as Display }
+impl_inspectable_config_value!{ u32 as Display }
+impl_inspectable_config_value!{ hyper::Uri as Display }
+
+impl_inspectable_config_value!{ IpAddr as Display }
+impl_inspectable_config_value!{ ipnet::IpNet as Display }
+impl_inspectable_config_value!{ Vec<IpAddr> }
+impl_inspectable_config_value!{ Vec<ipnet::IpNet> }
+
+impl InspectableConfigValue for Duration {
+  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    let v = self.as_secs_f64();
+    Display::fmt(&v, f)
+  }
 }
 
 #[ext(pub)]
@@ -119,6 +167,7 @@ impl Debug for Secret {
   #[throws(fmt::Error)]
   fn fmt(&self, f: &mut fmt::Formatter) { write!(f, "Secret(***)")? }
 }
+impl_inspectable_config_value!{ Secret as Debug }
 
 #[derive(Debug,Clone,Hash,Eq,PartialEq)]
 pub enum SectionName {
index 0ca0ec190e30b6a6fa934eb5e8f58cc0f0037ccc..35e012ebdf7b670856ceb310a7dbb21f147110ad 100644 (file)
@@ -55,7 +55,9 @@ pub use eyre::eyre as anyhow;
 pub use eyre::WrapErr;
 pub use eyre::Error as AE;
 
-pub use crate::config::{self, InstanceConfig, u32Ext as _};
+pub use crate::config::{self, InspectableConfigValue, InstanceConfig};
+pub use crate::config::u32Ext as _;
+pub use crate::impl_inspectable_config_value;
 pub use crate::ini;
 pub use crate::ipif::Ipif;
 pub use crate::multipart::{self, PartName, MetadataFieldIterator};
index 1f55aa6a7eda9effa527931dbfc9576174eb8ca3..c5b4737ad58f66543635bbd51d3dc1b23adb7175 100644 (file)
@@ -74,3 +74,5 @@ impl Display for LinkName {
     write!(f, "[{} {}]", &self.server, &self.client)?;
   }
 }
+
+impl_inspectable_config_value!{ LinkName as Display }