Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datatype Copy Rust's Structure #126

Merged
merged 15 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions macros/src/data_type_from/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@ use crate::utils::parse_attrs;

pub fn derive(input: proc_macro::TokenStream) -> syn::Result<proc_macro::TokenStream> {
let DeriveInput {
ident, data, attrs, ..
ident,
data,
attrs,
generics,
..
} = &parse_macro_input::parse::<DeriveInput>(input)?;

let mut attrs = parse_attrs(attrs)?;
let container_attrs = ContainerAttr::from_attrs(&mut attrs)?;

let crate_ref = container_attrs.crate_name.unwrap_or_else(|| quote!(specta));

if generics.params.len() > 0 {
return Err(syn::Error::new_spanned(
generics,
"DataTypeFrom does not support generics",
));
}

Ok(match data {
Data::Struct(data) => match &data.fields {
Fields::Named(_) => {
Expand All @@ -42,53 +53,55 @@ pub fn derive(input: proc_macro::TokenStream) -> syn::Result<proc_macro::TokenSt
.expect("'specta::DataTypeFrom' requires named fields.");
let ident_str = ident.to_string();

Some(quote! {
#crate_ref::internal::construct::struct_field(
#ident_str.into(),
false,
false,
t.#ident.into(),
)
})
Some(quote!((#ident_str.into(), #crate_ref::internal::construct::field(
false,
false,
t.#ident.into(),
))))
});

let struct_name = ident.to_string();
quote! {
#[automatically_derived]
impl From<#ident> for #crate_ref::StructType {
fn from(t: #ident) -> #crate_ref::StructType {
#crate_ref::internal::construct::r#struct(vec![], vec![#(#fields),*], None)
#crate_ref::internal::construct::r#struct(#struct_name.into(), vec![], #crate_ref::internal::construct::struct_named(vec![#(#fields),*], None))
}
}

#[automatically_derived]
impl From<#ident> for #crate_ref::DataType {
fn from(t: #ident) -> #crate_ref::DataType {
#crate_ref::DataType::Struct(t.into())
Self::Struct(t.into())
}
}
}
}
Fields::Unnamed(_) => {
let fields = data.fields.iter().enumerate().map(|(i, _)| {
let i = proc_macro2::Literal::usize_unsuffixed(i);
quote!(t.#i.into())
});
let fields = data
.fields
.iter()
.enumerate()
.map(|(i, _)| {
let i = proc_macro2::Literal::usize_unsuffixed(i);
quote!(t.#i.into())
})
.collect::<Vec<_>>();

quote! {
#[automatically_derived]
impl From<#ident> for #crate_ref::TupleType {
fn from(t: #ident) -> #crate_ref::TupleType {
#crate_ref::TupleType::Named {
generics: vec![],
fields: vec![#(#fields),*]
}
#crate_ref::internal::construct::tuple(
vec![#(#fields),*]
)
}
}

#[automatically_derived]
impl From<#ident> for #crate_ref::DataType {
fn from(t: #ident) -> #crate_ref::DataType {
#crate_ref::DataType::Tuple(t.into())
Self::Tuple(t.into())
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions macros/src/type/attr/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct ContainerAttr {
pub export: Option<bool>,
pub doc: Vec<String>,
pub deprecated: Option<String>,

// Struct ony (we pass it anyway so enums get nice errors)
pub transparent: bool,
}

impl_parse! {
Expand Down Expand Up @@ -53,6 +56,7 @@ impl_parse! {
out.deprecated = out.deprecated.take().or(Some(attr.parse_string()?));
}
},
"transparent" => out.transparent = attr.parse_bool().unwrap_or(true)
}
}

Expand All @@ -62,6 +66,7 @@ impl ContainerAttr {
Self::try_from_attrs("specta", attrs, &mut result)?;
#[cfg(feature = "serde")]
Self::try_from_attrs("serde", attrs, &mut result)?;
Self::try_from_attrs("repr", attrs, &mut result)?; // To handle `#[repr(transparent)]`
Self::try_from_attrs("doc", attrs, &mut result)?;
Ok(result)
}
Expand Down
1 change: 1 addition & 0 deletions macros/src/type/attr/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl_parse! {
"skip_serializing" => out.skip = true,
"skip_deserializing" => out.skip = true,
"skip_serializing_if" => out.optional = attr.parse_string()? == *"Option::is_none",
// Specta only attribute
"optional" => out.optional = attr.parse_bool().unwrap_or(true),
"default" => out.optional = attr.parse_bool().unwrap_or(true),
"flatten" => out.flatten = attr.parse_bool().unwrap_or(true)
Expand Down
2 changes: 0 additions & 2 deletions macros/src/type/attr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
pub use container::*;
pub use field::*;
pub use r#enum::*;
pub use r#struct::*;
pub use variant::*;

mod container;
mod r#enum;
mod field;
mod r#struct;
mod variant;
25 changes: 0 additions & 25 deletions macros/src/type/attr/struct.rs

This file was deleted.

Loading
Loading