|
1 | 1 | extern crate proc_macro; |
2 | 2 |
|
| 3 | +mod canyon_entity_macro; |
3 | 4 | mod canyon_macro; |
4 | 5 | mod query_operations; |
5 | 6 | mod utils; |
6 | 7 |
|
7 | 8 | use canyon_connection::CANYON_TOKIO_RUNTIME; |
8 | | -use proc_macro::{Span, TokenStream as CompilerTokenStream}; |
| 9 | +use canyon_entity_macro::parse_canyon_entity_proc_macro_attr; |
| 10 | +use proc_macro::TokenStream as CompilerTokenStream; |
9 | 11 | use proc_macro2::{Ident, TokenStream}; |
10 | 12 | use quote::{quote, ToTokens}; |
11 | 13 | use syn::{DeriveInput, Fields, Type, Visibility}; |
@@ -179,68 +181,8 @@ pub fn canyon_entity( |
179 | 181 | ) -> CompilerTokenStream { |
180 | 182 | let attrs = syn::parse_macro_input!(_meta as syn::AttributeArgs); |
181 | 183 |
|
182 | | - let mut table_name: Option<&str> = None; |
183 | | - let mut schema_name: Option<&str> = None; |
184 | | - |
185 | | - let mut parsing_attribute_error: Option<TokenStream> = None; |
186 | | - |
187 | | - // The parse of the available options to configure the Canyon Entity |
188 | | - for element in &attrs { |
189 | | - match element { |
190 | | - syn::NestedMeta::Meta(m) => { |
191 | | - match m { |
192 | | - syn::Meta::NameValue(nv) => { |
193 | | - let attr_arg_ident = nv |
194 | | - .path |
195 | | - .get_ident() |
196 | | - .expect("Something went wrong parsing the `table_name` argument") |
197 | | - .to_string(); |
198 | | - |
199 | | - if &attr_arg_ident == "table_name" || &attr_arg_ident == "schema" { |
200 | | - match nv.lit { |
201 | | - syn::Lit::Str(ref l) => { |
202 | | - if &attr_arg_ident == "table_name" { |
203 | | - table_name = Some(Box::leak(l.value().into_boxed_str())) |
204 | | - } else { |
205 | | - schema_name = Some(Box::leak(l.value().into_boxed_str())) |
206 | | - } |
207 | | - } |
208 | | - _ => { |
209 | | - parsing_attribute_error = Some(syn::Error::new( |
210 | | - Span::call_site().into(), |
211 | | - "Only string literals are valid values for the attributes" |
212 | | - ).into_compile_error()); |
213 | | - } |
214 | | - } |
215 | | - } else { |
216 | | - parsing_attribute_error = Some( |
217 | | - syn::Error::new( |
218 | | - Span::call_site().into(), |
219 | | - format!( |
220 | | - "Argument: `{:?}` are not allowed in the canyon_macro attr", |
221 | | - &attr_arg_ident |
222 | | - ), |
223 | | - ) |
224 | | - .into_compile_error(), |
225 | | - ); |
226 | | - } |
227 | | - } |
228 | | - _ => { |
229 | | - parsing_attribute_error = Some(syn::Error::new( |
230 | | - Span::call_site().into(), |
231 | | - "Only argument identifiers with a value after an `=` sign are allowed on the `canyon_macros::canyon_entity` proc macro" |
232 | | - ).into_compile_error()); |
233 | | - } |
234 | | - } |
235 | | - } |
236 | | - syn::NestedMeta::Lit(_) => { |
237 | | - parsing_attribute_error = Some(syn::Error::new( |
238 | | - Span::call_site().into(), |
239 | | - "No literal values allowed on the `canyon_macros::canyon_entity` proc macro" |
240 | | - ).into_compile_error()); |
241 | | - } |
242 | | - } |
243 | | - } |
| 184 | + let (table_name, schema_name, parsing_attribute_error) = |
| 185 | + parse_canyon_entity_proc_macro_attr(attrs); |
244 | 186 |
|
245 | 187 | let entity_res = syn::parse::<CanyonEntity>(input); |
246 | 188 |
|
|
0 commit comments