|
@@ -1,21 +1,23 @@
|
|
|
use proc_macro::TokenStream;
|
|
|
+use quote::{format_ident, quote};
|
|
|
use syn::{parse_macro_input, DeriveInput};
|
|
|
-use quote::{quote,format_ident};
|
|
|
|
|
|
use convert_case::{Case, Casing};
|
|
|
|
|
|
fn parse_microrm_ref(attrs: &Vec<syn::Attribute>) -> proc_macro2::TokenStream {
|
|
|
for attr in attrs {
|
|
|
- if attr.path.segments.len() == 0 { continue }
|
|
|
+ if attr.path.segments.len() == 0 {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
if attr.tokens.is_empty() {
|
|
|
if attr.path.segments.last().unwrap().ident == "microrm_internal" {
|
|
|
- return quote!{ crate }.into()
|
|
|
+ return quote! { crate }.into();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- quote!{ ::microrm }
|
|
|
+
|
|
|
+ quote! { ::microrm }
|
|
|
}
|
|
|
|
|
|
/// Turns a serializable/deserializable struct into a microrm entity model.
|
|
@@ -34,7 +36,7 @@ fn parse_microrm_ref(attrs: &Vec<syn::Attribute>) -> proc_macro2::TokenStream {
|
|
|
/// outside the microrm library)
|
|
|
/// The following are understood on individual fields
|
|
|
/// - `#[microrm_foreign]`: this is a foreign key (and must be an type implementing `EntityID`)
|
|
|
-#[proc_macro_derive(Entity, attributes(microrm_internal,microrm_foreign))]
|
|
|
+#[proc_macro_derive(Entity, attributes(microrm_internal, microrm_foreign))]
|
|
|
pub fn derive_entity(tokens: TokenStream) -> TokenStream {
|
|
|
let input = parse_macro_input!(tokens as DeriveInput);
|
|
|
|
|
@@ -48,30 +50,29 @@ pub fn derive_entity(tokens: TokenStream) -> TokenStream {
|
|
|
|
|
|
let st = match input.data {
|
|
|
syn::Data::Struct(st) => st,
|
|
|
- _ => panic!("Can only use derive(Entity) on structs!")
|
|
|
+ _ => panic!("Can only use derive(Entity) on structs!"),
|
|
|
};
|
|
|
let fields = match st.fields {
|
|
|
syn::Fields::Named(fields) => fields,
|
|
|
- _ => panic!("Can only use derive(Entity) on non-unit structs with named fields!")
|
|
|
+ _ => panic!("Can only use derive(Entity) on non-unit structs with named fields!"),
|
|
|
};
|
|
|
|
|
|
- for name in fields.named.iter() {
|
|
|
- // println!("ty: {:?}", name.ty);
|
|
|
- }
|
|
|
-
|
|
|
let mut variants = syn::punctuated::Punctuated::<syn::Ident, syn::token::Comma>::new();
|
|
|
- let mut field_names = syn::punctuated::Punctuated::<proc_macro2::TokenStream, syn::token::Comma>::new();
|
|
|
- let mut value_references = syn::punctuated::Punctuated::<proc_macro2::TokenStream, syn::token::Comma>::new();
|
|
|
+ let mut field_names =
|
|
|
+ syn::punctuated::Punctuated::<proc_macro2::TokenStream, syn::token::Comma>::new();
|
|
|
+ let mut value_references =
|
|
|
+ syn::punctuated::Punctuated::<proc_macro2::TokenStream, syn::token::Comma>::new();
|
|
|
for name in fields.named.iter() {
|
|
|
- let converted_case = format!("{}", name.ident.as_ref().unwrap().clone()).to_case(Case::UpperCamel);
|
|
|
+ let converted_case =
|
|
|
+ format!("{}", name.ident.as_ref().unwrap().clone()).to_case(Case::UpperCamel);
|
|
|
let converted_case = format_ident!("{}", converted_case);
|
|
|
variants.push(converted_case.clone());
|
|
|
|
|
|
let field_name = name.ident.as_ref().unwrap().clone();
|
|
|
let field_name_str = format!("{}", field_name);
|
|
|
- field_names.push(quote!{ Self::Column::#converted_case => #field_name_str }.into());
|
|
|
+ field_names.push(quote! { Self::Column::#converted_case => #field_name_str }.into());
|
|
|
|
|
|
- value_references.push(quote!{ &self. #field_name });
|
|
|
+ value_references.push(quote! { &self. #field_name });
|
|
|
}
|
|
|
|
|
|
let field_count = fields.named.iter().count();
|
|
@@ -131,6 +132,7 @@ pub fn derive_entity(tokens: TokenStream) -> TokenStream {
|
|
|
ret
|
|
|
}
|
|
|
|
|
|
+/// Marks a struct as able to be directly used in an Entity to correspond to a single database column.
|
|
|
#[proc_macro_derive(Modelable, attributes(microrm_internal))]
|
|
|
pub fn derive_modelable(tokens: TokenStream) -> TokenStream {
|
|
|
let input = parse_macro_input!(tokens as DeriveInput);
|