Skip to content

Commit

Permalink
Add option to rename the nanoserde crate
Browse files Browse the repository at this point in the history
  • Loading branch information
novafacing authored and knickish committed Dec 11, 2024
1 parent 5217035 commit a7113a2
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 157 deletions.
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

0 comments on commit a7113a2

Please sign in to comment.