|
19 | 19 |
|
20 | 20 | use proc_macro::TokenStream; |
21 | 21 | use quote::quote; |
22 | | -use syn::{parse_macro_input, DeriveInput, Data, Fields}; |
| 22 | +use syn::{parse_macro_input, Data, DeriveInput, Fields}; |
23 | 23 |
|
24 | 24 | /// Derives builder-style setter methods for struct fields. |
25 | 25 | /// |
@@ -77,60 +77,67 @@ pub fn derive_setters(input: TokenStream) -> TokenStream { |
77 | 77 | let methods = match &input.data { |
78 | 78 | Data::Struct(data) => match &data.fields { |
79 | 79 | Fields::Named(fields) => { |
80 | | - fields.named.iter().filter_map(|f| { |
81 | | - let field_name = f.ident.as_ref()?; |
82 | | - let field_ty = &f.ty; |
| 80 | + fields |
| 81 | + .named |
| 82 | + .iter() |
| 83 | + .filter_map(|f| { |
| 84 | + let field_name = f.ident.as_ref()?; |
| 85 | + let field_ty = &f.ty; |
83 | 86 |
|
84 | | - // Parse field attributes |
85 | | - let mut skip = false; |
86 | | - let mut use_into = false; |
87 | | - let mut use_raw = false; |
| 87 | + // Parse field attributes |
| 88 | + let mut skip = false; |
| 89 | + let mut use_into = false; |
| 90 | + let mut use_raw = false; |
88 | 91 |
|
89 | | - for attr in &f.attrs { |
90 | | - if attr.path().is_ident("setters") { |
91 | | - let _ = attr.parse_nested_meta(|meta| { |
92 | | - if meta.path.is_ident("skip") { |
93 | | - skip = true; |
94 | | - } else if meta.path.is_ident("into") { |
95 | | - use_into = true; |
96 | | - } else if meta.path.is_ident("raw") { |
97 | | - use_raw = true; |
98 | | - } |
99 | | - Ok(()) |
100 | | - }); |
| 92 | + for attr in &f.attrs { |
| 93 | + if attr.path().is_ident("setters") { |
| 94 | + let _ = attr.parse_nested_meta(|meta| { |
| 95 | + if meta.path.is_ident("skip") { |
| 96 | + skip = true; |
| 97 | + } else if meta.path.is_ident("into") { |
| 98 | + use_into = true; |
| 99 | + } else if meta.path.is_ident("raw") { |
| 100 | + use_raw = true; |
| 101 | + } |
| 102 | + Ok(()) |
| 103 | + }); |
| 104 | + } |
101 | 105 | } |
102 | | - } |
103 | 106 |
|
104 | | - if skip { |
105 | | - return None; |
106 | | - } |
| 107 | + if skip { |
| 108 | + return None; |
| 109 | + } |
107 | 110 |
|
108 | | - // Auto-detect Option<T> types (unless raw is specified) |
109 | | - let is_option = !use_raw && is_option_type(field_ty); |
| 111 | + // Auto-detect Option<T> types (unless raw is specified) |
| 112 | + let is_option = !use_raw && is_option_type(field_ty); |
110 | 113 |
|
111 | | - let (param_ty, value_expr) = if is_option { |
112 | | - let inner_ty = extract_option_inner_type(field_ty); |
113 | | - if use_into { |
114 | | - (quote! { impl Into<#inner_ty> }, quote! { Some(value.into()) }) |
| 114 | + let (param_ty, value_expr) = if is_option { |
| 115 | + let inner_ty = extract_option_inner_type(field_ty); |
| 116 | + if use_into { |
| 117 | + ( |
| 118 | + quote! { impl Into<#inner_ty> }, |
| 119 | + quote! { Some(value.into()) }, |
| 120 | + ) |
| 121 | + } else { |
| 122 | + (quote! { #inner_ty }, quote! { Some(value) }) |
| 123 | + } |
| 124 | + } else if use_into { |
| 125 | + (quote! { impl Into<#field_ty> }, quote! { value.into() }) |
115 | 126 | } else { |
116 | | - (quote! { #inner_ty }, quote! { Some(value) }) |
117 | | - } |
118 | | - } else if use_into { |
119 | | - (quote! { impl Into<#field_ty> }, quote! { value.into() }) |
120 | | - } else { |
121 | | - (quote! { #field_ty }, quote! { value }) |
122 | | - }; |
| 127 | + (quote! { #field_ty }, quote! { value }) |
| 128 | + }; |
123 | 129 |
|
124 | | - let method = quote! { |
125 | | - #[must_use] |
126 | | - pub fn #field_name(mut self, value: #param_ty) -> Self { |
127 | | - self.#field_name = #value_expr; |
128 | | - self |
129 | | - } |
130 | | - }; |
| 130 | + let method = quote! { |
| 131 | + #[must_use] |
| 132 | + pub fn #field_name(mut self, value: #param_ty) -> Self { |
| 133 | + self.#field_name = #value_expr; |
| 134 | + self |
| 135 | + } |
| 136 | + }; |
131 | 137 |
|
132 | | - Some(method) |
133 | | - }).collect::<Vec<_>>() |
| 138 | + Some(method) |
| 139 | + }) |
| 140 | + .collect::<Vec<_>>() |
134 | 141 | } |
135 | 142 | _ => vec![], |
136 | 143 | }, |
|
0 commit comments