|
@@ -25,22 +25,24 @@ fn is_elided(attrs: &Vec<syn::Attribute>) -> bool {
|
|
|
attrs.iter().filter(|a| a.path.is_ident("elide")).count() > 0
|
|
|
}
|
|
|
|
|
|
+fn is_unique(attrs: &Vec<syn::Attribute>) -> bool {
|
|
|
+ attrs.iter().filter(|a| a.path.is_ident("unique")).count() > 0
|
|
|
+}
|
|
|
+
|
|
|
pub fn derive(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
|
let input: syn::DeriveInput = syn::parse_macro_input!(tokens);
|
|
|
|
|
|
- let parts = if let syn::Data::Struct(syn::DataStruct {
|
|
|
- struct_token: _,
|
|
|
- fields: syn::Fields::Named(fields),
|
|
|
- semi_token: _,
|
|
|
- }) = input.data
|
|
|
- {
|
|
|
- fields
|
|
|
+ let parts = match input.data {
|
|
|
+ syn::Data::Struct(syn::DataStruct {
|
|
|
+ struct_token: _,
|
|
|
+ fields: syn::Fields::Named(fields),
|
|
|
+ semi_token: _,
|
|
|
+ }) => fields
|
|
|
.named
|
|
|
.into_iter()
|
|
|
.map(|f| (f.ident.unwrap(), f.ty, f.attrs))
|
|
|
- .collect::<Vec<_>>()
|
|
|
- } else {
|
|
|
- panic!("Can only derive Entity on data structs with named fields!");
|
|
|
+ .collect::<Vec<_>>(),
|
|
|
+ _ => panic!("Can only derive Entity on data structs with named fields!"),
|
|
|
};
|
|
|
|
|
|
let entity_ident = input.ident;
|
|
@@ -67,29 +69,22 @@ pub fn derive(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
|
|
|
|
let vis = input.vis;
|
|
|
|
|
|
- let unique_ident = format_ident!("unique");
|
|
|
-
|
|
|
// collect list of unique parts
|
|
|
let unique_parts = parts
|
|
|
.iter()
|
|
|
- .filter(|part| {
|
|
|
- part.2.iter().any(|attr| {
|
|
|
- attr.parse_meta()
|
|
|
- .map(|a| a.path().is_ident(&unique_ident))
|
|
|
- .unwrap_or(false)
|
|
|
- })
|
|
|
- })
|
|
|
+ .filter(|part| is_unique(&part.2))
|
|
|
.cloned()
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
let part_defs = parts.iter().map(|part| {
|
|
|
let part_combined_name = make_combined_name(part);
|
|
|
+ let part_base_ident = &part.0;
|
|
|
let part_base_name = &part.0.to_string();
|
|
|
let part_type = &part.1;
|
|
|
|
|
|
let placeholder = format!("${}_{}", entity_ident, part_base_name);
|
|
|
|
|
|
- let unique = unique_parts.iter().any(|p| p.0 == part.0);
|
|
|
+ let unique = is_unique(&part.2);
|
|
|
|
|
|
let doc = extract_doc_comment(&part.2);
|
|
|
|
|
@@ -111,6 +106,10 @@ pub fn derive(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
|
fn desc() -> Option<&'static str> {
|
|
|
#doc
|
|
|
}
|
|
|
+
|
|
|
+ fn get_datum(from: &Self::Entity) -> &Self::Datum {
|
|
|
+ &from.#part_base_ident
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
});
|
|
@@ -214,6 +213,10 @@ pub fn derive(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
|
fn placeholder() -> &'static str { "TODO" }
|
|
|
|
|
|
fn desc() -> Option<&'static str> { None }
|
|
|
+
|
|
|
+ fn get_datum(from: &Self::Entity) -> &Self::Datum {
|
|
|
+ unreachable!()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
impl ::microrm::schema::datum::Datum for #id_ident {
|