From c48a8ad4017e1ac0d0a62d2270ec19a571ab7fb2 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 7 Aug 2021 22:47:50 +0100 Subject: [PATCH] config: get global config Signed-off-by: Ian Jackson --- macros/macros.rs | 15 +++++++++++++-- src/config.rs | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/macros/macros.rs b/macros/macros.rs index 56b17cd..ee79e07 100644 --- a/macros/macros.rs +++ b/macros/macros.rs @@ -67,16 +67,17 @@ pub fn resolve(input: proc_macro::TokenStream) -> proc_macro::TokenStream { } => (f, ident), _ => panic!(), }; -dbg!(&top_ident); let target = &input.ident; let mut names = vec![]; let mut output = vec![]; let mut global_fields = vec![]; + let mut global_assignments = vec![]; for field in &fields.named { //dbg!(field); let fname = &field.ident.as_ref().unwrap(); + let ty = &field.ty; let fname_span = fname.span(); let skl = RefCell::new(None); let set_skl = |new| { @@ -98,6 +99,10 @@ dbg!(&top_ident); attrs: vec![], ..field.clone() }); + global_assignments.push(quote_spanned!(fname_span=> + #fname: <#ty as ResolveGlobal>::resolve + (l.iter().map(|e| &e.#fname)), + )); continue; } method = attr.path.to_token_stream(); @@ -140,7 +145,7 @@ dbg!(&top_ident); } //dbg!(&output); - let global = syn::Ident::new(&format!("{}Common", top_ident), + let global = syn::Ident::new(&format!("{}Global", top_ident), top_ident.span()); let output = quote! { @@ -161,6 +166,12 @@ dbg!(&top_ident); pub struct #global { #( #global_fields ),* } + + impl #global { + pub fn from(l: &[#top_ident]) -> #global { #global { + #( #global_assignments )* + } } + } }; //eprintln!("{}", &output); diff --git a/src/config.rs b/src/config.rs index 98e5a1e..048a91c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -814,6 +814,20 @@ impl InstanceConfig { } } +trait ResolveGlobal<'i> where Self: 'i { + fn resolve(it: I) -> Self + where I: Iterator; +} +impl<'i,T> ResolveGlobal<'i> for T where T: Eq + Clone + Debug + 'i { + fn resolve(mut it: I) -> Self + where I: Iterator + { + let first = it.next().expect("empty instances no global!"); + for x in it { assert_eq!(x, first); } + first.clone() + } +} + #[throws(AE)] pub fn read(opts: &Opts, end: LinkEnd) -> Vec { let agg = (||{ -- 2.30.2