Skip to content

Commit

Permalink
Use absolute paths in derive macro generated code to permit not using…
Browse files Browse the repository at this point in the history
… top-level use statements
  • Loading branch information
novafacing authored and knickish committed Dec 11, 2024
1 parent 27a9f61 commit 5217035
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
48 changes: 29 additions & 19 deletions derive/src/serde_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use proc_macro::TokenStream;

pub fn derive_ser_bin_proxy(proxy_type: &str, type_: &str) -> TokenStream {
format!(
"impl SerBin for {} {{
"impl nanoserde::SerBin for {} {{
fn ser_bin(&self, s: &mut Vec<u8>) {{
let proxy: {} = self.into();
proxy.ser_bin(s);
Expand All @@ -24,9 +24,9 @@ pub fn derive_ser_bin_proxy(proxy_type: &str, type_: &str) -> TokenStream {

pub fn derive_de_bin_proxy(proxy_type: &str, type_: &str) -> TokenStream {
format!(
"impl DeBin for {} {{
"impl nanoserde::DeBin for {} {{
fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result<Self, nanoserde::DeBinErr> {{
let proxy: {} = DeBin::de_bin(o, d)?;
let proxy: {} = nanoserde::DeBin::de_bin(o, d)?;
::core::result::Result::Ok(Into::into(&proxy))
}}
}}",
Expand Down Expand Up @@ -58,7 +58,7 @@ pub fn derive_ser_bin_struct(struct_: &Struct) -> TokenStream {
}
}
format!(
"impl{} SerBin for {}{} {{
"impl{} nanoserde::SerBin for {}{} {{
fn ser_bin(&self, s: &mut Vec<u8>) {{
{}
}}
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn derive_ser_bin_struct_unnamed(struct_: &Struct) -> TokenStream {
}
}
format!(
"impl{} SerBin for {}{} {{
"impl{} nanoserde::SerBin for {}{} {{
fn ser_bin(&self, s: &mut Vec<u8>) {{
{}
}}
Expand All @@ -112,20 +112,24 @@ pub fn derive_de_bin_struct(struct_: &Struct) -> TokenStream {
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: {} = DeBin::de_bin(o, d)?;", proxy);
l!(
body,
"let proxy: {} = nanoserde::DeBin::de_bin(o, d)?;",
proxy
);
l!(body, "Into::into(&proxy)");
l!(body, "},")
} else {
l!(
body,
"{}: DeBin::de_bin(o, d)?,",
"{}: nanoserde::DeBin::de_bin(o, d)?,",
field.field_name.as_ref().unwrap()
);
}
}

format!(
"impl{} DeBin for {}{} {{
"impl{} nanoserde::DeBin for {}{} {{
fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result<Self, nanoserde::DeBinErr> {{
::core::result::Result::Ok(Self {{
{}
Expand All @@ -151,16 +155,20 @@ pub fn derive_de_bin_struct_unnamed(struct_: &Struct) -> TokenStream {
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: {} = DeBin::de_bin(o, d)?;", proxy);
l!(
body,
"let proxy: {} = nanoserde::DeBin::de_bin(o, d)?;",
proxy
);
l!(body, "Into::into(&proxy)");
l!(body, "},")
} else {
l!(body, "{}: DeBin::de_bin(o, d)?,", n);
l!(body, "{}: nanoserde::DeBin::de_bin(o, d)?,", n);
}
}

format!(
"impl{} DeBin for {}{} {{
"impl{} nanoserde::DeBin for {}{} {{
fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result<Self, nanoserde::DeBinErr> {{
::core::result::Result::Ok(Self {{
{}
Expand Down Expand Up @@ -245,7 +253,7 @@ pub fn derive_ser_bin_enum(enum_: &Enum) -> TokenStream {
}

format!(
"impl{} SerBin for {}{} {{
"impl{} nanoserde::SerBin for {}{} {{
fn ser_bin(&self, s: &mut Vec<u8>) {{
match self {{
{}
Expand Down Expand Up @@ -290,7 +298,7 @@ pub fn derive_de_bin_enum(enum_: &Enum) -> TokenStream {
variant.field_name.as_ref().unwrap()
);
for _ in contents {
l!(r, "DeBin::de_bin(o, d)?,",);
l!(r, "nanoserde::DeBin::de_bin(o, d)?,",);
}
l!(r, "),")
}
Expand All @@ -307,7 +315,7 @@ pub fn derive_de_bin_enum(enum_: &Enum) -> TokenStream {
for f in contents.fields.iter() {
l!(
r,
"{}: DeBin::de_bin(o, d)?,",
"{}: nanoserde::DeBin::de_bin(o, d)?,",
f.field_name.as_ref().unwrap()
);
}
Expand All @@ -320,15 +328,17 @@ pub fn derive_de_bin_enum(enum_: &Enum) -> TokenStream {
}

format!(
"impl{} DeBin for {}{} {{
"impl{} nanoserde::DeBin for {}{} {{
fn de_bin(o:&mut usize, d:&[u8]) -> ::core::result::Result<Self, nanoserde::DeBinErr> {{
let id: u16 = DeBin::de_bin(o,d)?;
let id: u16 = nanoserde::DeBin::de_bin(o,d)?;
Ok(match id {{
{}
_ => return ::core::result::Result::Err(nanoserde::DeBinErr::new(*o, 0, d.len()))
}})
}}
}}", generic_w_bounds,enum_.name,generic_no_bounds, r)
.parse()
.unwrap()
}}",
generic_w_bounds, enum_.name, generic_no_bounds, r
)
.parse()
.unwrap()
}
24 changes: 12 additions & 12 deletions derive/src/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use proc_macro::TokenStream;

pub fn derive_ser_json_proxy(proxy_type: &str, type_: &str) -> TokenStream {
format!(
"impl SerJson for {} {{
"impl nanoserde::SerJson for {} {{
fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{
let proxy: {} = self.into();
proxy.ser_json(d, s);
Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn derive_ser_json_struct(struct_: &Struct) -> TokenStream {

format!(
"
impl{} SerJson for {}{} {{
impl{} nanoserde::SerJson for {}{} {{
fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{
s.st_pre();
{}
Expand Down Expand Up @@ -222,7 +222,7 @@ 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(DeJson::de_json(s, i) ?)}},",
"\"{}\" => {{s.next_colon(i) ?;{} = Some(nanoserde::DeJson::de_json(s, i) ?)}},",
json_field_name,
local_var
);
Expand All @@ -249,10 +249,10 @@ pub fn derive_de_json_named(name: &str, defaults: bool, fields: &[Field]) -> Tok

pub fn derive_de_json_proxy(proxy_type: &str, type_: &str) -> TokenStream {
format!(
"impl DeJson for {} {{
"impl nanoserde::DeJson for {} {{
#[allow(clippy::ignored_unit_patterns)]
fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result<Self, nanoserde::DeJsonErr> {{
let proxy: {} = DeJson::de_json(s, i)?;
let proxy: {} = nanoserde::DeJson::de_json(s, i)?;
::core::result::Result::Ok(Into::into(&proxy))
}}
}}",
Expand All @@ -275,7 +275,7 @@ pub fn derive_de_json_struct(struct_: &Struct) -> TokenStream {
let (generic_w_bounds, generic_no_bounds) = struct_bounds_strings(struct_, "DeJson");

format!(
"impl{} DeJson for {}{} {{
"impl{} nanoserde::DeJson for {}{} {{
#[allow(clippy::ignored_unit_patterns)]
fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result<Self,
nanoserde::DeJsonErr> {{
Expand Down Expand Up @@ -412,7 +412,7 @@ pub fn derive_ser_json_enum(enum_: &Enum) -> TokenStream {

format!(
"
impl SerJson for {} {{
impl nanoserde::SerJson for {} {{
fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{
match self {{
{}
Expand Down Expand Up @@ -468,7 +468,7 @@ pub fn derive_de_json_enum(enum_: &Enum) -> TokenStream {
for _ in contents.iter() {
l!(
field_names,
"{let r = DeJson::de_json(s,i)?;s.eat_comma_block(i)?;r},"
"{let r = nanoserde::DeJson::de_json(s,i)?;s.eat_comma_block(i)?;r},"
);
}
l!(
Expand All @@ -486,7 +486,7 @@ pub fn derive_de_json_enum(enum_: &Enum) -> TokenStream {
}

let mut r = format!(
"impl{} DeJson for {}{} {{
"impl{} nanoserde::DeJson for {}{} {{
#[allow(clippy::ignored_unit_patterns)]
fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result<Self, nanoserde::DeJsonErr> {{
match s.tok {{",
Expand Down Expand Up @@ -568,7 +568,7 @@ pub fn derive_ser_json_struct_unnamed(struct_: &Struct) -> TokenStream {

format!(
"
impl{} SerJson for {}{} {{
impl{} nanoserde::SerJson for {}{} {{
fn ser_json(&self, d: usize, s: &mut nanoserde::SerJsonState) {{
{}
}}
Expand All @@ -592,7 +592,7 @@ pub fn derive_de_json_struct_unnamed(struct_: &Struct) -> TokenStream {
let transparent = shared::attrs_transparent(&struct_.attributes);

for _ in &struct_.fields {
l!(body, "{ let r = DeJson::de_json(s, i)?;");
l!(body, "{ let r = nanoserde::DeJson::de_json(s, i)?;");
if struct_.fields.len() != 1 {
l!(body, " s.eat_comma_block(i)?;");
}
Expand Down Expand Up @@ -620,7 +620,7 @@ pub fn derive_de_json_struct_unnamed(struct_: &Struct) -> TokenStream {
};

format! ("
impl{} DeJson for {}{} {{
impl{} nanoserde::DeJson for {}{} {{
#[allow(clippy::ignored_unit_patterns)]
fn de_json(s: &mut nanoserde::DeJsonState, i: &mut core::str::Chars) -> ::core::result::Result<Self,nanoserde::DeJsonErr> {{
{}
Expand Down
24 changes: 12 additions & 12 deletions derive/src/serde_ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::shared;

pub fn derive_ser_ron_proxy(proxy_type: &str, type_: &str) -> TokenStream {
format!(
"impl SerRon for {} {{
"impl nanoserde::SerRon for {} {{
fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{
let proxy: {} = self.into();
proxy.ser_ron(d, s);
Expand All @@ -24,9 +24,9 @@ pub fn derive_ser_ron_proxy(proxy_type: &str, type_: &str) -> TokenStream {

pub fn derive_de_ron_proxy(proxy_type: &str, type_: &str) -> TokenStream {
format!(
"impl DeRon for {} {{
"impl nanoserde::DeRon for {} {{
fn de_ron(_s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result<Self, nanoserde::DeRonErr> {{
let proxy: {} = DeRon::deserialize_ron(i)?;
let proxy: {} = nanoserde::DeRon::deserialize_ron(i)?;
::core::result::Result::Ok(Into::into(&proxy))
}}
}}",
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn derive_ser_ron_struct(struct_: &Struct) -> TokenStream {

format!(
"
impl SerRon for {} {{
impl nanoserde::SerRon for {} {{
fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{
s.st_pre();
{}
Expand Down Expand Up @@ -102,7 +102,7 @@ pub fn derive_ser_ron_struct_unnamed(struct_: &Struct) -> TokenStream {
}
format!(
"
impl SerRon for {} {{
impl nanoserde::SerRon for {} {{
fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{
s.out.push('(');
{}
Expand Down Expand Up @@ -223,7 +223,7 @@ pub fn derive_de_ron_named(name: &String, fields: &Vec<Field>, attributes: &[Att
inner,
"\"{}\" => {{
s.next_colon(i)?;
{} = Some(DeRon::de_ron(s, i)?)
{} = Some(nanoserde::DeRon::de_ron(s, i)?)
}},",
ron_field_name,
local_var
Expand Down Expand Up @@ -274,7 +274,7 @@ pub fn derive_de_ron_struct(struct_: &Struct) -> TokenStream {
);

format!(
"impl DeRon for {} {{
"impl nanoserde::DeRon for {} {{
fn de_ron(s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result<Self,nanoserde::DeRonErr> {{
::core::result::Result::Ok({})
}}
Expand All @@ -290,15 +290,15 @@ pub fn derive_de_ron_struct_unnamed(struct_: &Struct) -> TokenStream {
l!(
body,
"{{
let r = DeRon::de_ron(s, i)?;
let r = nanoserde::DeRon::de_ron(s, i)?;
s.eat_comma_paren(i)?;
r
}},"
);
}

format! ("
impl DeRon for {} {{
impl nanoserde::DeRon for {} {{
fn de_ron(s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result<Self,nanoserde::DeRonErr> {{
s.paren_open(i)?;
let r = Self({});
Expand Down Expand Up @@ -404,7 +404,7 @@ pub fn derive_ser_ron_enum(enum_: &Enum) -> TokenStream {
}
format!(
"
impl SerRon for {} {{
impl nanoserde::SerRon for {} {{
fn ser_ron(&self, d: usize, s: &mut nanoserde::SerRonState) {{
match self {{
{}
Expand Down Expand Up @@ -448,7 +448,7 @@ pub fn derive_de_ron_enum(enum_: &Enum) -> TokenStream {
l!(
inner,
"{
let r = DeRon::de_ron(s, i)?;
let r = nanoserde::DeRon::de_ron(s, i)?;
s.eat_comma_paren(i)?;
r
}, "
Expand All @@ -475,7 +475,7 @@ pub fn derive_de_ron_enum(enum_: &Enum) -> TokenStream {
}

format! ("
impl DeRon for {} {{
impl nanoserde::DeRon for {} {{
fn de_ron(s: &mut nanoserde::DeRonState, i: &mut core::str::Chars) -> ::core::result::Result<Self,nanoserde::DeRonErr> {{
// we are expecting an identifier
s.ident(i)?;
Expand Down

0 comments on commit 5217035

Please sign in to comment.