refactor(config): simplify config reading

!squash me

!squash me
This commit is contained in:
2025-07-10 17:08:42 +03:00
parent b341b7a661
commit b988880c41
20 changed files with 328 additions and 229 deletions

View File

@@ -0,0 +1,42 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{Data, DeriveInput, Fields, Ident};
pub fn impl_from_file(input: DeriveInput) -> TokenStream {
let name = &input.ident;
let file_name = format!("{name}File");
let file_ident = Ident::new(&file_name, name.span());
let fields = match &input.data {
Data::Struct(data) => match &data.fields {
Fields::Named(fields) => &fields.named,
_ => panic!("Only named fields are supported"),
},
_ => panic!("Only structs are supported"),
};
let field_idents = fields.iter().map(|f| &f.ident);
let field_idents2 = field_idents.clone();
quote! {
impl #name {
fn from_file(file: Option<#file_ident>) -> Self
where
#file_ident: Default + Clone
{
let default = #file_ident::default();
let file = file.unwrap_or_else(|| default.clone());
Self {
#(#field_idents: file.#field_idents2.unwrap_or_else(|| default.#field_idents.clone().unwrap())),*
}
}
}
impl From<Option<#file_ident>> for #name {
fn from(value: Option<#file_ident>) -> Self {
Self::from_file(value)
}
}
}.into()
}

10
derive_macro/src/lib.rs Normal file
View File

@@ -0,0 +1,10 @@
mod from_file;
use proc_macro::TokenStream;
use syn::{DeriveInput, parse_macro_input};
#[proc_macro_derive(FromFile)]
pub fn derive_from_file(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
from_file::impl_from_file(input)
}