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

Use absolute paths in derive macro #124

Merged
merged 2 commits into from
Dec 11, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ For more examples take a look at [tests](/tests)
| container attribute: `#[nserde(rename = "")]` | yes | yes | yes | no |
| container attribute: `#[nserde(proxy = "")]` | yes | yes | no | no |
| container attribute: `#[nserde(transparent)]` | yes | no | no | no |
| container attribute: `#[nserde(crate = "")]` | yes | yes | yes | no |

## Crate features:

Expand Down
62 changes: 38 additions & 24 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ mod parse;
pub fn derive_ser_bin(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);

let crate_name = shared::attrs_crate(input.attributes()).unwrap_or("nanoserde");

if let Some(proxy) = shared::attrs_proxy(input.attributes()) {
return derive_ser_bin_proxy(&proxy, input.name());
return derive_ser_bin_proxy(&proxy, input.name(), crate_name);
}

// ok we have an ident, its either a struct or a enum
match &input {
parse::Data::Struct(struct_) if struct_.named => derive_ser_bin_struct(struct_),
parse::Data::Struct(struct_) => derive_ser_bin_struct_unnamed(struct_),
parse::Data::Enum(enum_) => derive_ser_bin_enum(enum_),
parse::Data::Struct(struct_) if struct_.named => derive_ser_bin_struct(struct_, crate_name),
parse::Data::Struct(struct_) => derive_ser_bin_struct_unnamed(struct_, crate_name),
parse::Data::Enum(enum_) => derive_ser_bin_enum(enum_, crate_name),
_ => unimplemented!("Only structs and enums are supported"),
}
}
Expand All @@ -48,15 +50,17 @@ pub fn derive_ser_bin(input: proc_macro::TokenStream) -> proc_macro::TokenStream
pub fn derive_de_bin(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);

let crate_name = shared::attrs_crate(input.attributes()).unwrap_or("nanoserde");

if let Some(proxy) = shared::attrs_proxy(input.attributes()) {
return derive_de_bin_proxy(&proxy, input.name());
return derive_de_bin_proxy(&proxy, input.name(), crate_name);
}

// ok we have an ident, its either a struct or a enum
match &input {
parse::Data::Struct(struct_) if struct_.named => derive_de_bin_struct(struct_),
parse::Data::Struct(struct_) => derive_de_bin_struct_unnamed(struct_),
parse::Data::Enum(enum_) => derive_de_bin_enum(enum_),
parse::Data::Struct(struct_) if struct_.named => derive_de_bin_struct(struct_, crate_name),
parse::Data::Struct(struct_) => derive_de_bin_struct_unnamed(struct_, crate_name),
parse::Data::Enum(enum_) => derive_de_bin_enum(enum_, crate_name),

_ => unimplemented!("Only structs and enums are supported"),
}
Expand All @@ -67,15 +71,17 @@ pub fn derive_de_bin(input: proc_macro::TokenStream) -> proc_macro::TokenStream
pub fn derive_ser_ron(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);

let crate_name = shared::attrs_crate(input.attributes()).unwrap_or("nanoserde");

if let Some(proxy) = shared::attrs_proxy(input.attributes()) {
return derive_ser_ron_proxy(&proxy, input.name());
return derive_ser_ron_proxy(&proxy, input.name(), crate_name);
}

// ok we have an ident, its either a struct or a enum
match &input {
parse::Data::Struct(struct_) if struct_.named => derive_ser_ron_struct(struct_),
parse::Data::Struct(struct_) => derive_ser_ron_struct_unnamed(struct_),
parse::Data::Enum(enum_) => derive_ser_ron_enum(enum_),
parse::Data::Struct(struct_) if struct_.named => derive_ser_ron_struct(struct_, crate_name),
parse::Data::Struct(struct_) => derive_ser_ron_struct_unnamed(struct_, crate_name),
parse::Data::Enum(enum_) => derive_ser_ron_enum(enum_, crate_name),
_ => unimplemented!("Only structs and enums are supported"),
}
}
Expand All @@ -85,15 +91,17 @@ pub fn derive_ser_ron(input: proc_macro::TokenStream) -> proc_macro::TokenStream
pub fn derive_de_ron(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);

let crate_name = shared::attrs_crate(input.attributes()).unwrap_or("nanoserde");

if let Some(proxy) = shared::attrs_proxy(input.attributes()) {
return derive_de_ron_proxy(&proxy, input.name());
return derive_de_ron_proxy(&proxy, input.name(), crate_name);
}

// ok we have an ident, its either a struct or a enum
match &input {
parse::Data::Struct(struct_) if struct_.named => derive_de_ron_struct(struct_),
parse::Data::Struct(struct_) => derive_de_ron_struct_unnamed(struct_),
parse::Data::Enum(enum_) => derive_de_ron_enum(enum_),
parse::Data::Struct(struct_) if struct_.named => derive_de_ron_struct(struct_, crate_name),
parse::Data::Struct(struct_) => derive_de_ron_struct_unnamed(struct_, crate_name),
parse::Data::Enum(enum_) => derive_de_ron_enum(enum_, crate_name),
_ => unimplemented!("Only structs and enums are supported"),
}
}
Expand All @@ -103,15 +111,19 @@ pub fn derive_de_ron(input: proc_macro::TokenStream) -> proc_macro::TokenStream
pub fn derive_ser_json(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);

let crate_name = shared::attrs_crate(input.attributes()).unwrap_or("nanoserde");

if let Some(proxy) = shared::attrs_proxy(input.attributes()) {
return derive_ser_json_proxy(&proxy, input.name());
return derive_ser_json_proxy(&proxy, input.name(), crate_name);
}

// ok we have an ident, its either a struct or a enum
match &input {
parse::Data::Struct(struct_) if struct_.named => derive_ser_json_struct(struct_),
parse::Data::Struct(struct_) => derive_ser_json_struct_unnamed(struct_),
parse::Data::Enum(enum_) => derive_ser_json_enum(enum_),
parse::Data::Struct(struct_) if struct_.named => {
derive_ser_json_struct(struct_, crate_name)
}
parse::Data::Struct(struct_) => derive_ser_json_struct_unnamed(struct_, crate_name),
parse::Data::Enum(enum_) => derive_ser_json_enum(enum_, crate_name),
_ => unimplemented!(""),
}
}
Expand All @@ -121,15 +133,17 @@ pub fn derive_ser_json(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
pub fn derive_de_json(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);

let crate_name = shared::attrs_crate(input.attributes()).unwrap_or("nanoserde");

if let Some(proxy) = shared::attrs_proxy(input.attributes()) {
return derive_de_json_proxy(&proxy, input.name());
return derive_de_json_proxy(&proxy, input.name(), crate_name);
}

// ok we have an ident, its either a struct or a enum
match &input {
parse::Data::Struct(struct_) if struct_.named => derive_de_json_struct(struct_),
parse::Data::Struct(struct_) => derive_de_json_struct_unnamed(struct_),
parse::Data::Enum(enum_) => derive_de_json_enum(enum_),
parse::Data::Struct(struct_) if struct_.named => derive_de_json_struct(struct_, crate_name),
parse::Data::Struct(struct_) => derive_de_json_struct_unnamed(struct_, crate_name),
parse::Data::Enum(enum_) => derive_de_json_enum(enum_, crate_name),
parse::Data::Union(_) => unimplemented!("Unions are not supported"),
}
}
Loading
Loading