1use proc_macro::TokenStream;
2
3use quote::quote;
4use syn::{
5 Data, DeriveInput, Field, Fields, ItemEnum, ItemStruct, Type, parse_macro_input, parse_quote,
6};
7
8/// Derives the `MergeFrom` trait for a struct.
9///
10/// This macro automatically implements `MergeFrom` by calling `merge_from`
11/// on all fields in the struct.
12///
13/// # Example
14///
15/// ```ignore
16/// #[derive(Clone, MergeFrom)]
17/// struct MySettings {
18/// field1: Option<String>,
19/// field2: SomeOtherSettings,
20/// }
21/// ```
22#[proc_macro_derive(MergeFrom)]
23pub fn derive_merge_from(input: TokenStream) -> TokenStream {
24 let input = parse_macro_input!(input as DeriveInput);
25
26 let name = &input.ident;
27 let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
28
29 let merge_body = match &input.data {
30 Data::Struct(data_struct) => match &data_struct.fields {
31 Fields::Named(fields) => {
32 let field_merges = fields.named.iter().map(|field| {
33 let field_name = &field.ident;
34 quote! {
35 self.#field_name.merge_from(&other.#field_name);
36 }
37 });
38
39 quote! {
40 #(#field_merges)*
41 }
42 }
43 Fields::Unnamed(fields) => {
44 let field_merges = fields.unnamed.iter().enumerate().map(|(i, _)| {
45 let field_index = syn::Index::from(i);
46 quote! {
47 self.#field_index.merge_from(&other.#field_index);
48 }
49 });
50
51 quote! {
52 #(#field_merges)*
53 }
54 }
55 Fields::Unit => {
56 quote! {
57 // No fields to merge for unit structs
58 }
59 }
60 },
61 Data::Enum(_) => {
62 quote! {
63 *self = other.clone();
64 }
65 }
66 Data::Union(_) => {
67 panic!("MergeFrom cannot be derived for unions");
68 }
69 };
70
71 let expanded = quote! {
72 impl #impl_generics crate::merge_from::MergeFrom for #name #ty_generics #where_clause {
73 fn merge_from(&mut self, other: &Self) {
74 use crate::merge_from::MergeFrom as _;
75 #merge_body
76 }
77 }
78 };
79
80 TokenStream::from(expanded)
81}
82
83/// Registers the setting type with the SettingsStore. Note that you need to
84/// have `gpui` in your dependencies for this to work.
85#[proc_macro_derive(RegisterSetting)]
86pub fn derive_register_setting(input: TokenStream) -> TokenStream {
87 let input = syn::parse_macro_input!(input as DeriveInput);
88 let type_name = &input.ident;
89
90 quote! {
91 settings::private::inventory::submit! {
92 settings::private::RegisteredSetting {
93 settings_value: || {
94 Box::new(settings::private::SettingValue::<#type_name> {
95 global_value: None,
96 local_values: Vec::new(),
97 })
98 },
99 from_settings: |content| Box::new(<#type_name as settings::Settings>::from_settings(content)),
100 id: || std::any::TypeId::of::<#type_name>(),
101 }
102 }
103 }
104 .into()
105}
106
107// Adds serde attributes to each field with type Option<T>:
108// #serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "settings::deserialize_fallible")
109#[proc_macro_attribute]
110pub fn with_fallible_options(_args: TokenStream, input: TokenStream) -> TokenStream {
111 fn apply_on_fields(fields: &mut Fields) {
112 match fields {
113 Fields::Unit => {}
114 Fields::Named(fields) => {
115 for field in &mut fields.named {
116 add_if_option(field)
117 }
118 }
119 Fields::Unnamed(fields) => {
120 for field in &mut fields.unnamed {
121 add_if_option(field)
122 }
123 }
124 }
125 }
126
127 fn add_if_option(field: &mut Field) {
128 match &field.ty {
129 Type::Path(syn::TypePath { qself: None, path })
130 if path.leading_colon.is_none()
131 && path.segments.len() == 1
132 && path.segments[0].ident == "Option" => {}
133 _ => return,
134 }
135 let attr = parse_quote!(
136 #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with="crate::fallible_options::deserialize")]
137 );
138 field.attrs.push(attr);
139 }
140
141 if let Ok(mut input) = syn::parse::<ItemStruct>(input.clone()) {
142 apply_on_fields(&mut input.fields);
143 quote!(#input).into()
144 } else if let Ok(mut input) = syn::parse::<ItemEnum>(input) {
145 for variant in &mut input.variants {
146 apply_on_fields(&mut variant.fields);
147 }
148 quote!(#input).into()
149 } else {
150 panic!("with_fallible_options can only be applied to struct or enum definitions.");
151 }
152}