|
|
@ -24,6 +24,7 @@ fn derive_struct(input: &DeriveInput, struct_data: &DataStruct) -> TokenStream {
|
|
|
|
let name = ident.to_string();
|
|
|
|
let name = ident.to_string();
|
|
|
|
let (impl_generics, ty_generics, _) = input.generics.split_for_impl();
|
|
|
|
let (impl_generics, ty_generics, _) = input.generics.split_for_impl();
|
|
|
|
let where_clause = add_rusty_bound(&input.generics);
|
|
|
|
let where_clause = add_rusty_bound(&input.generics);
|
|
|
|
|
|
|
|
let rusty_value = get_rusty_value_crate();
|
|
|
|
|
|
|
|
|
|
|
|
match &struct_data.fields {
|
|
|
|
match &struct_data.fields {
|
|
|
|
syn::Fields::Named(FieldsNamed { named, .. }) => {
|
|
|
|
syn::Fields::Named(FieldsNamed { named, .. }) => {
|
|
|
@ -35,9 +36,9 @@ fn derive_struct(input: &DeriveInput, struct_data: &DataStruct) -> TokenStream {
|
|
|
|
let field_count = named.len();
|
|
|
|
let field_count = named.len();
|
|
|
|
|
|
|
|
|
|
|
|
TokenStream::from(quote! {
|
|
|
|
TokenStream::from(quote! {
|
|
|
|
impl #impl_generics rusty_value::RustyValue for #ident #ty_generics #where_clause {
|
|
|
|
impl #impl_generics #rusty_value::RustyValue for #ident #ty_generics #where_clause {
|
|
|
|
fn into_rusty_value(self) -> rusty_value::Value {
|
|
|
|
fn into_rusty_value(self) -> #rusty_value::Value {
|
|
|
|
use rusty_value::*;
|
|
|
|
use #rusty_value::*;
|
|
|
|
let mut values = std::collections::HashMap::with_capacity(#field_count);
|
|
|
|
let mut values = std::collections::HashMap::with_capacity(#field_count);
|
|
|
|
|
|
|
|
|
|
|
|
#(
|
|
|
|
#(
|
|
|
@ -61,9 +62,9 @@ fn derive_struct(input: &DeriveInput, struct_data: &DataStruct) -> TokenStream {
|
|
|
|
let field_count = unnamed.len();
|
|
|
|
let field_count = unnamed.len();
|
|
|
|
|
|
|
|
|
|
|
|
TokenStream::from(quote! {
|
|
|
|
TokenStream::from(quote! {
|
|
|
|
impl #impl_generics rusty_value::RustyValue for #ident #ty_generics #where_clause {
|
|
|
|
impl #impl_generics #rusty_value::RustyValue for #ident #ty_generics #where_clause {
|
|
|
|
fn into_rusty_value(self) -> rusty_value::Value {
|
|
|
|
fn into_rusty_value(self) -> #rusty_value::Value {
|
|
|
|
use rusty_value::*;
|
|
|
|
use #rusty_value::*;
|
|
|
|
let mut values = Vec::with_capacity(#field_count);
|
|
|
|
let mut values = Vec::with_capacity(#field_count);
|
|
|
|
|
|
|
|
|
|
|
|
#(
|
|
|
|
#(
|
|
|
@ -79,9 +80,9 @@ fn derive_struct(input: &DeriveInput, struct_data: &DataStruct) -> TokenStream {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
syn::Fields::Unit => TokenStream::from(quote! {
|
|
|
|
syn::Fields::Unit => TokenStream::from(quote! {
|
|
|
|
impl #impl_generics rusty_value::RustyValue for #ident #ty_generics #where_clause {
|
|
|
|
impl #impl_generics #rusty_value::RustyValue for #ident #ty_generics #where_clause {
|
|
|
|
fn into_rusty_value(self) -> rusty_value::Value {
|
|
|
|
fn into_rusty_value(self) -> #rusty_value::Value {
|
|
|
|
use rusty_value::*;
|
|
|
|
use #rusty_value::*;
|
|
|
|
Value::Struct(Struct{
|
|
|
|
Value::Struct(Struct{
|
|
|
|
name: #name.to_string(),
|
|
|
|
name: #name.to_string(),
|
|
|
|
fields: Fields::Unit,
|
|
|
|
fields: Fields::Unit,
|
|
|
@ -101,14 +102,15 @@ fn derive_enum(input: &DeriveInput, enum_data: &DataEnum) -> TokenStream {
|
|
|
|
.iter()
|
|
|
|
.iter()
|
|
|
|
.map(|v| create_enum_value_match(ident, v))
|
|
|
|
.map(|v| create_enum_value_match(ident, v))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let rusty_value = get_rusty_value_crate();
|
|
|
|
|
|
|
|
|
|
|
|
TokenStream::from(quote! {
|
|
|
|
TokenStream::from(quote! {
|
|
|
|
impl #impl_generics rusty_value::RustyValue for #ident #ty_generics #where_clause {
|
|
|
|
impl #impl_generics #rusty_value::RustyValue for #ident #ty_generics #where_clause {
|
|
|
|
fn into_rusty_value(self) -> rusty_value::Value {
|
|
|
|
fn into_rusty_value(self) -> #rusty_value::Value {
|
|
|
|
let enum_val = match self {
|
|
|
|
let enum_val = match self {
|
|
|
|
#( #variant_matchers )*
|
|
|
|
#( #variant_matchers )*
|
|
|
|
};
|
|
|
|
};
|
|
|
|
rusty_value::Value::Enum(enum_val)
|
|
|
|
#rusty_value::Value::Enum(enum_val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
@ -118,6 +120,7 @@ fn create_enum_value_match(ident: &syn::Ident, variant: &Variant) -> proc_macro2
|
|
|
|
let enum_name = ident.to_string();
|
|
|
|
let enum_name = ident.to_string();
|
|
|
|
let variant_ident = &variant.ident;
|
|
|
|
let variant_ident = &variant.ident;
|
|
|
|
let variant_name = variant_ident.to_string();
|
|
|
|
let variant_name = variant_ident.to_string();
|
|
|
|
|
|
|
|
let rusty_value = get_rusty_value_crate();
|
|
|
|
|
|
|
|
|
|
|
|
match &variant.fields {
|
|
|
|
match &variant.fields {
|
|
|
|
syn::Fields::Named(FieldsNamed { named, .. }) => {
|
|
|
|
syn::Fields::Named(FieldsNamed { named, .. }) => {
|
|
|
@ -130,7 +133,7 @@ fn create_enum_value_match(ident: &syn::Ident, variant: &Variant) -> proc_macro2
|
|
|
|
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
quote! {
|
|
|
|
#ident::#variant_ident { #( #field_idents, )* } => {
|
|
|
|
#ident::#variant_ident { #( #field_idents, )* } => {
|
|
|
|
use rusty_value::*;
|
|
|
|
use #rusty_value::*;
|
|
|
|
|
|
|
|
|
|
|
|
let mut fields = std::collections::HashMap::with_capacity(#field_count);
|
|
|
|
let mut fields = std::collections::HashMap::with_capacity(#field_count);
|
|
|
|
#(
|
|
|
|
#(
|
|
|
@ -154,7 +157,7 @@ fn create_enum_value_match(ident: &syn::Ident, variant: &Variant) -> proc_macro2
|
|
|
|
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
quote! {
|
|
|
|
#ident::#variant_ident ( #( #field_names, )* ) => {
|
|
|
|
#ident::#variant_ident ( #( #field_names, )* ) => {
|
|
|
|
use rusty_value::*;
|
|
|
|
use #rusty_value::*;
|
|
|
|
|
|
|
|
|
|
|
|
let mut fields = Vec::with_capacity(#field_count);
|
|
|
|
let mut fields = Vec::with_capacity(#field_count);
|
|
|
|
#(
|
|
|
|
#(
|
|
|
@ -170,7 +173,7 @@ fn create_enum_value_match(ident: &syn::Ident, variant: &Variant) -> proc_macro2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
syn::Fields::Unit => quote! {
|
|
|
|
syn::Fields::Unit => quote! {
|
|
|
|
#ident::#variant_ident => {
|
|
|
|
#ident::#variant_ident => {
|
|
|
|
use rusty_value::*;
|
|
|
|
use #rusty_value::*;
|
|
|
|
|
|
|
|
|
|
|
|
Enum {
|
|
|
|
Enum {
|
|
|
|
name: #enum_name.to_string(),
|
|
|
|
name: #enum_name.to_string(),
|
|
|
@ -197,3 +200,11 @@ fn add_rusty_bound(generics: &Generics) -> WhereClause {
|
|
|
|
.extend(new_predicates);
|
|
|
|
.extend(new_predicates);
|
|
|
|
generics.where_clause.unwrap()
|
|
|
|
generics.where_clause.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn get_rusty_value_crate() -> proc_macro2::TokenStream {
|
|
|
|
|
|
|
|
use proc_macro_crate::{crate_name, FoundCrate};
|
|
|
|
|
|
|
|
match crate_name("rusty_value") {
|
|
|
|
|
|
|
|
Ok(FoundCrate::Itself) | Err(_) => quote!(rusty_value),
|
|
|
|
|
|
|
|
Ok(FoundCrate::Name(name)) => quote!(#name),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|