diff --git a/README.md b/README.md index 9214a77..a9c621f 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/derive/src/lib.rs b/derive/src/lib.rs index bb79dd2..455c088 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -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"), } } @@ -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"), } @@ -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"), } } @@ -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"), } } @@ -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!(""), } } @@ -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"), } } diff --git a/derive/src/serde_bin.rs b/derive/src/serde_bin.rs index 980ceb1..3054e83 100644 --- a/derive/src/serde_bin.rs +++ b/derive/src/serde_bin.rs @@ -8,37 +8,38 @@ use crate::{ use proc_macro::TokenStream; -pub fn derive_ser_bin_proxy(proxy_type: &str, type_: &str) -> TokenStream { +pub fn derive_ser_bin_proxy(proxy_type: &str, type_: &str, crate_name: &str) -> TokenStream { format!( - "impl nanoserde::SerBin for {} {{ + "impl {}::SerBin for {} {{ fn ser_bin(&self, s: &mut Vec) {{ let proxy: {} = self.into(); proxy.ser_bin(s); }} }}", - type_, proxy_type + crate_name, type_, proxy_type ) .parse() .unwrap() } -pub fn derive_de_bin_proxy(proxy_type: &str, type_: &str) -> TokenStream { +pub fn derive_de_bin_proxy(proxy_type: &str, type_: &str, crate_name: &str) -> TokenStream { format!( - "impl nanoserde::DeBin for {} {{ - fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ - let proxy: {} = nanoserde::DeBin::de_bin(o, d)?; + "impl {}::DeBin for {} {{ + fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ + let proxy: {} = {}::DeBin::de_bin(o, d)?; ::core::result::Result::Ok(Into::into(&proxy)) }} }}", - type_, proxy_type + crate_name, type_, crate_name, proxy_type, crate_name ) .parse() .unwrap() } -pub fn derive_ser_bin_struct(struct_: &Struct) -> TokenStream { +pub fn derive_ser_bin_struct(struct_: &Struct, crate_name: &str) -> TokenStream { let mut body = String::new(); - let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "SerBin"); + let (generic_w_bounds, generic_no_bounds) = + struct_bounds_strings(struct_, "SerBin", crate_name); for field in &struct_.fields { if let Some(proxy) = crate::shared::attrs_proxy(&field.attributes) { @@ -58,12 +59,13 @@ pub fn derive_ser_bin_struct(struct_: &Struct) -> TokenStream { } } format!( - "impl{} nanoserde::SerBin for {}{} {{ + "impl{} {}::SerBin for {}{} {{ fn ser_bin(&self, s: &mut Vec) {{ {} }} }}", generic_w_bounds, + crate_name, struct_ .name .as_ref() @@ -75,9 +77,10 @@ pub fn derive_ser_bin_struct(struct_: &Struct) -> TokenStream { .unwrap() } -pub fn derive_ser_bin_struct_unnamed(struct_: &Struct) -> TokenStream { +pub fn derive_ser_bin_struct_unnamed(struct_: &Struct, crate_name: &str) -> TokenStream { let mut body = String::new(); - let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "SerBin"); + let (generic_w_bounds, generic_no_bounds) = + struct_bounds_strings(struct_, "SerBin", crate_name); for (n, field) in struct_.fields.iter().enumerate() { if let Some(proxy) = crate::shared::attrs_proxy(&field.attributes) { @@ -88,12 +91,13 @@ pub fn derive_ser_bin_struct_unnamed(struct_: &Struct) -> TokenStream { } } format!( - "impl{} nanoserde::SerBin for {}{} {{ + "impl{} {}::SerBin for {}{} {{ fn ser_bin(&self, s: &mut Vec) {{ {} }} }}", generic_w_bounds, + crate_name, struct_ .name .as_ref() @@ -105,91 +109,98 @@ pub fn derive_ser_bin_struct_unnamed(struct_: &Struct) -> TokenStream { .unwrap() } -pub fn derive_de_bin_struct(struct_: &Struct) -> TokenStream { +pub fn derive_de_bin_struct(struct_: &Struct, crate_name: &str) -> TokenStream { let mut body = String::new(); - let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "DeBin"); + let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "DeBin", crate_name); for field in &struct_.fields { if let Some(proxy) = crate::shared::attrs_proxy(&field.attributes) { l!(body, "{}: {{", field.field_name.as_ref().unwrap()); l!( body, - "let proxy: {} = nanoserde::DeBin::de_bin(o, d)?;", - proxy + "let proxy: {} = {}::DeBin::de_bin(o, d)?;", + proxy, + crate_name ); l!(body, "Into::into(&proxy)"); l!(body, "},") } else { l!( body, - "{}: nanoserde::DeBin::de_bin(o, d)?,", - field.field_name.as_ref().unwrap() + "{}: {}::DeBin::de_bin(o, d)?,", + field.field_name.as_ref().unwrap(), + crate_name ); } } format!( - "impl{} nanoserde::DeBin for {}{} {{ - fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ + "impl{} {}::DeBin for {}{} {{ + fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ ::core::result::Result::Ok(Self {{ {} }}) }} }}", generic_w_bounds, + crate_name, struct_ .name .as_ref() .expect("Shouldnt have an anonymous struct here"), generic_no_bounds, + crate_name, body ) .parse() .unwrap() } -pub fn derive_de_bin_struct_unnamed(struct_: &Struct) -> TokenStream { +pub fn derive_de_bin_struct_unnamed(struct_: &Struct, crate_name: &str) -> TokenStream { let mut body = String::new(); - let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "DeBin"); + let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "DeBin", crate_name); for (n, field) in struct_.fields.iter().enumerate() { if let Some(proxy) = crate::shared::attrs_proxy(&field.attributes) { l!(body, "{}: {{", n); l!( body, - "let proxy: {} = nanoserde::DeBin::de_bin(o, d)?;", - proxy + "let proxy: {} = {}::DeBin::de_bin(o, d)?;", + proxy, + crate_name ); l!(body, "Into::into(&proxy)"); l!(body, "},") } else { - l!(body, "{}: nanoserde::DeBin::de_bin(o, d)?,", n); + l!(body, "{}: {}::DeBin::de_bin(o, d)?,", n, crate_name); } } format!( - "impl{} nanoserde::DeBin for {}{} {{ - fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ + "impl{} {}::DeBin for {}{} {{ + fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ ::core::result::Result::Ok(Self {{ {} }}) }} }}", generic_w_bounds, + crate_name, struct_ .name .as_ref() .expect("Shouldnt have an anonymous struct here"), generic_no_bounds, + crate_name, body ) .parse() .unwrap() } -pub fn derive_ser_bin_enum(enum_: &Enum) -> TokenStream { +pub fn derive_ser_bin_enum(enum_: &Enum, crate_name: &str) -> TokenStream { let mut r = String::new(); - let (generic_w_bounds, generic_no_bounds) = enum_bounds_strings(enum_, "SerBin"); + let (generic_w_bounds, generic_no_bounds) = enum_bounds_strings(enum_, "SerBin", crate_name); for (index, variant) in enum_.variants.iter().enumerate() { let lit = format!("{}u16", index); @@ -253,22 +264,22 @@ pub fn derive_ser_bin_enum(enum_: &Enum) -> TokenStream { } format!( - "impl{} nanoserde::SerBin for {}{} {{ + "impl{} {}::SerBin for {}{} {{ fn ser_bin(&self, s: &mut Vec) {{ match self {{ {} }} }} }}", - generic_w_bounds, enum_.name, generic_no_bounds, r + generic_w_bounds, crate_name, enum_.name, generic_no_bounds, r ) .parse() .unwrap() } -pub fn derive_de_bin_enum(enum_: &Enum) -> TokenStream { +pub fn derive_de_bin_enum(enum_: &Enum, crate_name: &str) -> TokenStream { let mut r = String::new(); - let (generic_w_bounds, generic_no_bounds) = enum_bounds_strings(enum_, "DeBin"); + let (generic_w_bounds, generic_no_bounds) = enum_bounds_strings(enum_, "DeBin", crate_name); for (index, variant) in enum_.variants.iter().enumerate() { let lit = format!("{}u16", index); @@ -298,7 +309,7 @@ pub fn derive_de_bin_enum(enum_: &Enum) -> TokenStream { variant.field_name.as_ref().unwrap() ); for _ in contents { - l!(r, "nanoserde::DeBin::de_bin(o, d)?,",); + l!(r, "{}::DeBin::de_bin(o, d)?,", crate_name); } l!(r, "),") } @@ -315,8 +326,9 @@ pub fn derive_de_bin_enum(enum_: &Enum) -> TokenStream { for f in contents.fields.iter() { l!( r, - "{}: nanoserde::DeBin::de_bin(o, d)?,", - f.field_name.as_ref().unwrap() + "{}: {}::DeBin::de_bin(o, d)?,", + f.field_name.as_ref().unwrap(), + crate_name ); } l!(r, "},"); @@ -328,16 +340,23 @@ pub fn derive_de_bin_enum(enum_: &Enum) -> TokenStream { } format!( - "impl{} nanoserde::DeBin for {}{} {{ - fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ - let id: u16 = nanoserde::DeBin::de_bin(o,d)?; + "impl{} {}::DeBin for {}{} {{ + fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result {{ + let id: u16 = {}::DeBin::de_bin(o,d)?; Ok(match id {{ {} - _ => return ::core::result::Result::Err(nanoserde::DeBinErr::new(*o, 0, d.len())) + _ => return ::core::result::Result::Err({}::DeBinErr::new(*o, 0, d.len())) }}) }} }}", - generic_w_bounds, enum_.name, generic_no_bounds, r + generic_w_bounds, + crate_name, + enum_.name, + generic_no_bounds, + crate_name, + crate_name, + r, + crate_name ) .parse() .unwrap() diff --git a/derive/src/serde_json.rs b/derive/src/serde_json.rs index 7a31ea6..c4e97c6 100644 --- a/derive/src/serde_json.rs +++ b/derive/src/serde_json.rs @@ -11,15 +11,15 @@ use crate::{ use proc_macro::TokenStream; -pub fn derive_ser_json_proxy(proxy_type: &str, type_: &str) -> TokenStream { +pub fn derive_ser_json_proxy(proxy_type: &str, type_: &str, crate_name: &str) -> TokenStream { format!( - "impl nanoserde::SerJson for {} {{ - fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{ + "impl {}::SerJson for {} {{ + fn ser_json(&self, d: usize, s: &mut {}::SerJsonState) {{ let proxy: {} = self.into(); proxy.ser_json(d, s); }} }}", - type_, proxy_type + crate_name, type_, crate_name, proxy_type ) .parse() .unwrap() @@ -39,9 +39,10 @@ fn ser_proxy_guard(fieldname: &str, field: &Field) -> String { } } -pub fn derive_ser_json_struct(struct_: &Struct) -> TokenStream { +pub fn derive_ser_json_struct(struct_: &Struct, crate_name: &str) -> TokenStream { let mut s = String::new(); - let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "SerJson"); + let (generic_w_bounds, generic_no_bounds) = + struct_bounds_strings(struct_, "SerJson", crate_name); l!(s, "let mut first_field_was_serialized = false;"); @@ -106,8 +107,8 @@ pub fn derive_ser_json_struct(struct_: &Struct) -> TokenStream { format!( " - impl{} nanoserde::SerJson for {}{} {{ - fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{ + impl{} {}::SerJson for {}{} {{ + fn ser_json(&self, d: usize, s: &mut {}::SerJsonState) {{ s.st_pre(); {} s.st_post(d); @@ -115,18 +116,25 @@ pub fn derive_ser_json_struct(struct_: &Struct) -> TokenStream { }} ", generic_w_bounds, + crate_name, struct_ .name .as_ref() .expect("Cannot implement for anonymous struct"), generic_no_bounds, + crate_name, s ) .parse() .unwrap() } -pub fn derive_de_json_named(name: &str, defaults: bool, fields: &[Field]) -> TokenStream { +pub fn derive_de_json_named( + name: &str, + defaults: bool, + fields: &[Field], + crate_name: &str, +) -> TokenStream { let mut local_vars = Vec::new(); let mut struct_field_names = Vec::new(); let mut json_field_names = Vec::new(); @@ -222,9 +230,10 @@ pub fn derive_de_json_named(name: &str, defaults: bool, fields: &[Field]) -> Tok for (json_field_name, local_var) in matches.iter() { l!( r, - "\"{}\" => {{s.next_colon(i) ?;{} = Some(nanoserde::DeJson::de_json(s, i) ?)}},", + "\"{}\" => {{s.next_colon(i) ?;{} = Some({}::DeJson::de_json(s, i) ?)}},", json_field_name, - local_var + local_var, + crate_name ); } // TODO: maybe introduce "exhaustive" attribute? @@ -247,22 +256,22 @@ pub fn derive_de_json_named(name: &str, defaults: bool, fields: &[Field]) -> Tok r.parse().unwrap() } -pub fn derive_de_json_proxy(proxy_type: &str, type_: &str) -> TokenStream { +pub fn derive_de_json_proxy(proxy_type: &str, type_: &str, crate_name: &str) -> TokenStream { format!( - "impl nanoserde::DeJson for {} {{ + "impl {}::DeJson for {} {{ #[allow(clippy::ignored_unit_patterns)] - fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ - let proxy: {} = nanoserde::DeJson::de_json(s, i)?; + fn de_json(s: &mut {}::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ + let proxy: {} = {}::DeJson::de_json(s, i)?; ::core::result::Result::Ok(Into::into(&proxy)) }} }}", - type_, proxy_type + crate_name, type_, crate_name, crate_name, proxy_type, crate_name ) .parse() .unwrap() } -pub fn derive_de_json_struct(struct_: &Struct) -> TokenStream { +pub fn derive_de_json_struct(struct_: &Struct, crate_name: &str) -> TokenStream { let body = derive_de_json_named( struct_ .name @@ -271,21 +280,23 @@ pub fn derive_de_json_struct(struct_: &Struct) -> TokenStream { shared::attrs_default(&struct_.attributes).is_some() || shared::attrs_default_with(&struct_.attributes).is_some(), &struct_.fields[..], + crate_name, ); - let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "DeJson"); + let (generic_w_bounds, generic_no_bounds) = + struct_bounds_strings(struct_, "DeJson", crate_name); format!( - "impl{} nanoserde::DeJson for {}{} {{ + "impl{} {}::DeJson for {}{} {{ #[allow(clippy::ignored_unit_patterns)] - fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ + fn de_json(s: &mut {}::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ ::core::result::Result::Ok({{ {} }}) }} - }}", generic_w_bounds, struct_.name.as_ref().expect("Cannot implement for anonymous struct"), generic_no_bounds, body) + }}", generic_w_bounds, crate_name, struct_.name.as_ref().expect("Cannot implement for anonymous struct"), generic_no_bounds, crate_name, crate_name, body) .parse().unwrap() } -pub fn derive_ser_json_enum(enum_: &Enum) -> TokenStream { +pub fn derive_ser_json_enum(enum_: &Enum, crate_name: &str) -> TokenStream { let mut r = String::new(); for variant in enum_.variants.iter() { @@ -412,23 +423,23 @@ pub fn derive_ser_json_enum(enum_: &Enum) -> TokenStream { format!( " - impl nanoserde::SerJson for {} {{ - fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{ + impl {}::SerJson for {} {{ + fn ser_json(&self, d: usize, s: &mut {}::SerJsonState) {{ match self {{ {} }} }} }}", - enum_.name, r + crate_name, enum_.name, crate_name, r ) .parse() .unwrap() } -pub fn derive_de_json_enum(enum_: &Enum) -> TokenStream { +pub fn derive_de_json_enum(enum_: &Enum, crate_name: &str) -> TokenStream { let mut r_units = String::new(); let mut r_rest = String::new(); - let (generic_w_bounds, generic_no_bounds) = enum_bounds_strings(enum_, "DeJson"); + let (generic_w_bounds, generic_no_bounds) = enum_bounds_strings(enum_, "DeJson", crate_name); for variant in &enum_.variants { let field_name = variant.field_name.clone().unwrap(); @@ -457,6 +468,7 @@ pub fn derive_de_json_enum(enum_: &Enum) -> TokenStream { &format!("Self::{}", &field_name), false, &contents.fields, + crate_name, ); l!(r_rest, "\"{}\" => {{ {} }}, ", json_variant_name, body); } @@ -468,7 +480,8 @@ pub fn derive_de_json_enum(enum_: &Enum) -> TokenStream { for _ in contents.iter() { l!( field_names, - "{let r = nanoserde::DeJson::de_json(s,i)?;s.eat_comma_block(i)?;r}," + "{{let r = {}::DeJson::de_json(s,i)?;s.eat_comma_block(i)?;r}},", + crate_name ); } l!( @@ -486,17 +499,17 @@ pub fn derive_de_json_enum(enum_: &Enum) -> TokenStream { } let mut r = format!( - "impl{} nanoserde::DeJson for {}{} {{ + "impl{} {}::DeJson for {}{} {{ #[allow(clippy::ignored_unit_patterns)] - fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ + fn de_json(s: &mut {}::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ match s.tok {{", - generic_w_bounds, generic_no_bounds, enum_.name, + generic_w_bounds, crate_name, enum_.name, generic_no_bounds, crate_name, crate_name ); if !r_rest.is_empty() { r.push_str(&format!( " - nanoserde::DeJsonTok::CurlyOpen => {{ + {}::DeJsonTok::CurlyOpen => {{ s.curly_open(i)?; let _ = s.string(i)?; s.colon(i)?; @@ -507,21 +520,21 @@ pub fn derive_de_json_enum(enum_: &Enum) -> TokenStream { s.curly_close(i)?; r }},", - r_rest, + crate_name, r_rest, )) } if !r_units.is_empty() { r.push_str(&format!( " - nanoserde::DeJsonTok::Str => {{ + {}::DeJsonTok::Str => {{ let _ = s.string(i)?; ::core::result::Result::Ok(match s.strbuf.as_ref() {{ {} _ => return ::core::result::Result::Err(s.err_enum(&s.strbuf)) }}) }},", - r_units, + crate_name, r_units, )) } @@ -537,9 +550,10 @@ pub fn derive_de_json_enum(enum_: &Enum) -> TokenStream { r.parse().unwrap() } -pub fn derive_ser_json_struct_unnamed(struct_: &Struct) -> TokenStream { +pub fn derive_ser_json_struct_unnamed(struct_: &Struct, crate_name: &str) -> TokenStream { let mut body = String::new(); - let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "SerJson"); + let (generic_w_bounds, generic_no_bounds) = + struct_bounds_strings(struct_, "SerJson", crate_name); let transparent = shared::attrs_transparent(&struct_.attributes); @@ -568,31 +582,34 @@ pub fn derive_ser_json_struct_unnamed(struct_: &Struct) -> TokenStream { format!( " - impl{} nanoserde::SerJson for {}{} {{ - fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{ + impl{} {}::SerJson for {}{} {{ + fn ser_json(&self, d: usize, s: &mut {}::SerJsonState) {{ {} }} }}", generic_w_bounds, + crate_name, struct_ .name .as_ref() .expect("Cannot implement for anonymous struct"), generic_no_bounds, + crate_name, body ) .parse() .unwrap() } -pub fn derive_de_json_struct_unnamed(struct_: &Struct) -> TokenStream { +pub fn derive_de_json_struct_unnamed(struct_: &Struct, crate_name: &str) -> TokenStream { let mut body = String::new(); - let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "DeJson"); + let (generic_w_bounds, generic_no_bounds) = + struct_bounds_strings(struct_, "DeJson", crate_name); let transparent = shared::attrs_transparent(&struct_.attributes); for _ in &struct_.fields { - l!(body, "{ let r = nanoserde::DeJson::de_json(s, i)?;"); + l!(body, "{{ let r = {}::DeJson::de_json(s, i)?;", crate_name); if struct_.fields.len() != 1 { l!(body, " s.eat_comma_block(i)?;"); } @@ -620,12 +637,12 @@ pub fn derive_de_json_struct_unnamed(struct_: &Struct) -> TokenStream { }; format! (" - impl{} nanoserde::DeJson for {}{} {{ + impl{} {}::DeJson for {}{} {{ #[allow(clippy::ignored_unit_patterns)] - fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ + fn de_json(s: &mut {}::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result {{ {} ::core::result::Result::Ok(r) }} - }}",generic_w_bounds, struct_.name.as_ref().expect("Cannot implement for anonymous struct"), generic_no_bounds, body + }}",generic_w_bounds, crate_name, struct_.name.as_ref().expect("Cannot implement for anonymous struct"), generic_no_bounds, crate_name, crate_name,body ).parse().unwrap() } diff --git a/derive/src/serde_ron.rs b/derive/src/serde_ron.rs index fadb38f..44f5118 100644 --- a/derive/src/serde_ron.rs +++ b/derive/src/serde_ron.rs @@ -8,35 +8,35 @@ use proc_macro::TokenStream; use crate::shared; -pub fn derive_ser_ron_proxy(proxy_type: &str, type_: &str) -> TokenStream { +pub fn derive_ser_ron_proxy(proxy_type: &str, type_: &str, crate_name: &str) -> TokenStream { format!( - "impl nanoserde::SerRon for {} {{ - fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{ + "impl {}::SerRon for {} {{ + fn ser_ron(&self, d: usize, s: &mut {}::SerRonState) {{ let proxy: {} = self.into(); proxy.ser_ron(d, s); }} }}", - type_, proxy_type + crate_name, type_, crate_name, proxy_type ) .parse() .unwrap() } -pub fn derive_de_ron_proxy(proxy_type: &str, type_: &str) -> TokenStream { +pub fn derive_de_ron_proxy(proxy_type: &str, type_: &str, crate_name: &str) -> TokenStream { format!( - "impl nanoserde::DeRon for {} {{ - fn de_ron(_s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ - let proxy: {} = nanoserde::DeRon::deserialize_ron(i)?; + "impl {}::DeRon for {} {{ + fn de_ron(_s: &mut {}::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ + let proxy: {} = {}::DeRon::deserialize_ron(i)?; ::core::result::Result::Ok(Into::into(&proxy)) }} }}", - type_, proxy_type + crate_name, type_, crate_name, crate_name, proxy_type, crate_name ) .parse() .unwrap() } -pub fn derive_ser_ron_struct(struct_: &Struct) -> TokenStream { +pub fn derive_ser_ron_struct(struct_: &Struct, crate_name: &str) -> TokenStream { let mut s = String::new(); for field in &struct_.fields { @@ -72,25 +72,27 @@ pub fn derive_ser_ron_struct(struct_: &Struct) -> TokenStream { format!( " - impl nanoserde::SerRon for {} {{ - fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{ + impl {}::SerRon for {} {{ + fn ser_ron(&self, d: usize, s: &mut {}::SerRonState) {{ s.st_pre(); {} s.st_post(d); }} }} ", + crate_name, struct_ .name .as_ref() .expect("Cannot implement for anonymous struct"), + crate_name, s ) .parse() .unwrap() } -pub fn derive_ser_ron_struct_unnamed(struct_: &Struct) -> TokenStream { +pub fn derive_ser_ron_struct_unnamed(struct_: &Struct, crate_name: &str) -> TokenStream { let mut body = String::new(); let last = struct_.fields.len() - 1; @@ -102,24 +104,31 @@ pub fn derive_ser_ron_struct_unnamed(struct_: &Struct) -> TokenStream { } format!( " - impl nanoserde::SerRon for {} {{ - fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{ + impl {}::SerRon for {} {{ + fn ser_ron(&self, d: usize, s: &mut {}::SerRonState) {{ s.out.push('('); {} s.out.push(')'); }} }}", + crate_name, struct_ .name .as_ref() .expect("Cannot implement for anonymous struct"), + crate_name, body ) .parse() .unwrap() } -pub fn derive_de_ron_named(name: &String, fields: &Vec, attributes: &[Attribute]) -> String { +pub fn derive_de_ron_named( + name: &String, + fields: &Vec, + attributes: &[Attribute], + crate_name: &str, +) -> String { let mut local_vars = Vec::new(); let mut struct_field_names = Vec::new(); let mut ron_field_names = Vec::new(); @@ -223,10 +232,11 @@ pub fn derive_de_ron_named(name: &String, fields: &Vec, attributes: &[Att inner, "\"{}\" => {{ s.next_colon(i)?; - {} = Some(nanoserde::DeRon::de_ron(s, i)?) + {} = Some({}::DeRon::de_ron(s, i)?) }},", ron_field_name, - local_var + local_var, + crate_name ); } format!( @@ -263,7 +273,7 @@ pub fn derive_de_ron_named(name: &String, fields: &Vec, attributes: &[Att ) } -pub fn derive_de_ron_struct(struct_: &Struct) -> TokenStream { +pub fn derive_de_ron_struct(struct_: &Struct, crate_name: &str) -> TokenStream { let body = derive_de_ron_named( struct_ .name @@ -271,45 +281,47 @@ pub fn derive_de_ron_struct(struct_: &Struct) -> TokenStream { .expect("Cannot implement for anonymous struct"), &struct_.fields, &struct_.attributes, + crate_name, ); format!( - "impl nanoserde::DeRon for {} {{ - fn de_ron(s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ + "impl {}::DeRon for {} {{ + fn de_ron(s: &mut {}::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ ::core::result::Result::Ok({}) }} - }}", struct_.name.as_ref().expect("Cannot implement for anonymous struct"), body) + }}", crate_name, struct_.name.as_ref().expect("Cannot implement for anonymous struct"), crate_name, crate_name, body) .parse() .unwrap() } -pub fn derive_de_ron_struct_unnamed(struct_: &Struct) -> TokenStream { +pub fn derive_de_ron_struct_unnamed(struct_: &Struct, crate_name: &str) -> TokenStream { let mut body = String::new(); for _ in &struct_.fields { l!( body, "{{ - let r = nanoserde::DeRon::de_ron(s, i)?; + let r = {}::DeRon::de_ron(s, i)?; s.eat_comma_paren(i)?; r - }}," + }},", + crate_name ); } format! (" - impl nanoserde::DeRon for {} {{ - fn de_ron(s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ + impl {}::DeRon for {} {{ + fn de_ron(s: &mut {}::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ s.paren_open(i)?; let r = Self({}); s.paren_close(i)?; ::core::result::Result::Ok(r) }} - }}",struct_.name.as_ref().expect("Cannot implement for anonymous struct"), body + }}", crate_name, struct_.name.as_ref().expect("Cannot implement for anonymous struct"), crate_name, crate_name, body ).parse().unwrap() } -pub fn derive_ser_ron_enum(enum_: &Enum) -> TokenStream { +pub fn derive_ser_ron_enum(enum_: &Enum, crate_name: &str) -> TokenStream { let mut body = String::new(); for variant in &enum_.variants { @@ -404,20 +416,20 @@ pub fn derive_ser_ron_enum(enum_: &Enum) -> TokenStream { } format!( " - impl nanoserde::SerRon for {} {{ - fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{ + impl {}::SerRon for {} {{ + fn ser_ron(&self, d: usize, s: &mut {}::SerRonState) {{ match self {{ {} }} }} }}", - enum_.name, body + crate_name, enum_.name, crate_name, body ) .parse() .unwrap() } -pub fn derive_de_ron_enum(enum_: &Enum) -> TokenStream { +pub fn derive_de_ron_enum(enum_: &Enum, crate_name: &str) -> TokenStream { let mut body = String::new(); for variant in &enum_.variants { let ident = variant.field_name.clone().unwrap(); @@ -436,7 +448,7 @@ pub fn derive_de_ron_enum(enum_: &Enum) -> TokenStream { .. } => { let name = format!("{}::{}", enum_.name, ident); - let inner = derive_de_ron_named(&name, &contents.fields, &[]); + let inner = derive_de_ron_named(&name, &contents.fields, &[], crate_name); l!(body, "\"{}\" => {}", ident, inner); } Type { @@ -447,11 +459,12 @@ pub fn derive_de_ron_enum(enum_: &Enum) -> TokenStream { for _ in contents.iter() { l!( inner, - "{ - let r = nanoserde::DeRon::de_ron(s, i)?; + "{{ + let r = {}::DeRon::de_ron(s, i)?; s.eat_comma_paren(i)?; r - }, " + }}, ", + crate_name ) } @@ -475,8 +488,8 @@ pub fn derive_de_ron_enum(enum_: &Enum) -> TokenStream { } format! (" - impl nanoserde::DeRon for {} {{ - fn de_ron(s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ + impl {}::DeRon for {} {{ + fn de_ron(s: &mut {}::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result {{ // we are expecting an identifier s.ident(i)?; ::core::result::Result::Ok(match s.identbuf.as_ref() {{ @@ -484,5 +497,5 @@ pub fn derive_de_ron_enum(enum_: &Enum) -> TokenStream { _ => return ::core::result::Result::Err(s.err_enum(&s.identbuf)) }}) }} - }}", enum_.name, body).parse().unwrap() + }}", crate_name, enum_.name, crate_name, crate_name, body).parse().unwrap() } diff --git a/derive/src/shared.rs b/derive/src/shared.rs index a1fbce2..70e8468 100644 --- a/derive/src/shared.rs +++ b/derive/src/shared.rs @@ -84,8 +84,22 @@ pub fn attrs_serialize_none_as_null(attributes: &[crate::parse::Attribute]) -> b .any(|attr| attr.tokens.len() == 1 && attr.tokens[0] == "serialize_none_as_null") } +pub fn attrs_crate(attributes: &[crate::parse::Attribute]) -> Option<&str> { + attributes.iter().find_map(|attr| { + if attr.tokens.len() == 2 && attr.tokens[0] == "crate" { + Some(attr.tokens[1].as_str()) + } else { + None + } + }) +} + #[cfg(any(feature = "binary", feature = "json"))] -pub(crate) fn struct_bounds_strings(struct_: &Struct, bound_name: &str) -> (String, String) { +pub(crate) fn struct_bounds_strings( + struct_: &Struct, + bound_name: &str, + crate_name: &str, +) -> (String, String) { let generics: &Vec<_> = &struct_.generics; if generics.is_empty() { @@ -94,7 +108,7 @@ pub(crate) fn struct_bounds_strings(struct_: &Struct, bound_name: &str) -> (Stri let mut generic_w_bounds = "<".to_string(); for generic in generics.iter().filter(|g| g.ident_only() != "Self") { generic_w_bounds += generic - .full_with_const(&[format!("nanoserde::{}", bound_name).as_str()], true) + .full_with_const(&[format!("{}::{}", crate_name, bound_name).as_str()], true) .as_str(); generic_w_bounds += ", "; } @@ -110,7 +124,11 @@ pub(crate) fn struct_bounds_strings(struct_: &Struct, bound_name: &str) -> (Stri } #[cfg(any(feature = "binary", feature = "json"))] -pub(crate) fn enum_bounds_strings(enum_: &Enum, bound_name: &str) -> (String, String) { +pub(crate) fn enum_bounds_strings( + enum_: &Enum, + bound_name: &str, + crate_name: &str, +) -> (String, String) { let generics: &Vec<_> = &enum_.generics; if generics.is_empty() { @@ -119,7 +137,7 @@ pub(crate) fn enum_bounds_strings(enum_: &Enum, bound_name: &str) -> (String, St let mut generic_w_bounds = "<".to_string(); for generic in generics.iter().filter(|g| g.ident_only() != "Self") { generic_w_bounds += generic - .full_with_const(&[format!("nanoserde::{}", bound_name).as_str()], true) + .full_with_const(&[format!("{}::{}", crate_name, bound_name).as_str()], true) .as_str(); generic_w_bounds += ", "; } diff --git a/tests/bin.rs b/tests/bin.rs index 3a3371d..2f8e9d6 100644 --- a/tests/bin.rs +++ b/tests/bin.rs @@ -295,3 +295,29 @@ fn array_leak_test() { assert!(TOGGLED_ON_DROP.load(std::sync::atomic::Ordering::SeqCst)) } + +#[test] +fn binary_crate() { + use nanoserde as renamed; + #[derive(renamed::DeBin, renamed::SerBin, PartialEq)] + #[nserde(crate = "renamed")] + pub struct Test { + pub a: i32, + pub b: f32, + c: Option, + d: Option, + } + + let test: Test = Test { + a: 1, + b: 2., + c: Some("asd".to_string()), + d: None, + }; + + let bytes = renamed::SerBin::serialize_bin(&test); + + let test_deserialized = renamed::DeBin::deserialize_bin(&bytes).unwrap(); + + assert!(test == test_deserialized); +} diff --git a/tests/json.rs b/tests/json.rs index d143727..e767bbe 100644 --- a/tests/json.rs +++ b/tests/json.rs @@ -1177,3 +1177,28 @@ fn test_deser_oversized_value() { ) ); } + +#[test] +fn json_crate() { + use nanoserde as renamed; + #[derive(renamed::DeJson)] + #[nserde(crate = "renamed")] + pub struct Test { + pub a: f32, + pub b: f32, + c: Option, + d: Option, + } + + let json = r#"{ + "a": 1, + "b": 2.0, + "d": "hello" + }"#; + + let test: Test = renamed::DeJson::deserialize_json(json).unwrap(); + assert_eq!(test.a, 1.); + assert_eq!(test.b, 2.); + assert_eq!(test.d.unwrap(), "hello"); + assert_eq!(test.c, None); +} diff --git a/tests/ron.rs b/tests/ron.rs index 2706a95..7569617 100644 --- a/tests/ron.rs +++ b/tests/ron.rs @@ -638,3 +638,29 @@ fn test_deser_oversized_value() { ) ); } + +#[test] +fn ron_crate() { + use nanoserde as renamed; + + #[derive(renamed::DeRon)] + #[nserde(crate = "renamed")] + pub struct Test { + a: i32, + b: f32, + c: Option, + d: Option, + } + + let ron = r#"( + a: 1, + b: 2.0, + d: "hello", + )"#; + + let test: Test = renamed::DeRon::deserialize_ron(ron).unwrap(); + assert_eq!(test.a, 1); + assert_eq!(test.b, 2.); + assert_eq!(test.c, None); + assert_eq!(test.d.unwrap(), "hello"); +}