mirror of
https://github.com/astral-sh/uv.git
synced 2025-07-07 13:25:00 +00:00
Add a derive macro for Combine
(#4325)
## Summary Saves us some boilerplate when adding settings in the future.
This commit is contained in:
parent
83067c1802
commit
74c05683bb
7 changed files with 71 additions and 123 deletions
40
crates/uv-macros/src/lib.rs
Normal file
40
crates/uv-macros/src/lib.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, DeriveInput};
|
||||
|
||||
#[proc_macro_derive(CombineOptions)]
|
||||
pub fn derive_combine(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
impl_combine(&input)
|
||||
}
|
||||
|
||||
fn impl_combine(ast: &DeriveInput) -> TokenStream {
|
||||
let name = &ast.ident;
|
||||
let fields = if let syn::Data::Struct(syn::DataStruct {
|
||||
fields: syn::Fields::Named(ref fields),
|
||||
..
|
||||
}) = ast.data
|
||||
{
|
||||
&fields.named
|
||||
} else {
|
||||
unimplemented!();
|
||||
};
|
||||
|
||||
let combines = fields.iter().map(|f| {
|
||||
let name = &f.ident;
|
||||
quote! {
|
||||
#name: self.#name.combine(other.#name)
|
||||
}
|
||||
});
|
||||
|
||||
let gen = quote! {
|
||||
impl crate::Combine for #name {
|
||||
fn combine(self, other: #name) -> #name {
|
||||
#name {
|
||||
#(#combines),*
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
gen.into()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue