Skip to content

Commit

Permalink
nanoserde: fix object proxying
Browse files Browse the repository at this point in the history
It looks like json object proxying was originally intended to be implemented, but it wasn't by tests, and produced code that failed to compile with the following warning:

```
error[E0308]: mismatched types
   --> crates/nanoserde/tests/json.rs:960:14
    |
960 |     #[derive(DeJson, SerJson, PartialEq, Debug)]
    |              ^^^^^^
    |              |
    |              expected `&str`, found `&mut Chars<'_>`
    |              arguments to this function are incorrect
    |
    = note:      expected reference `&str`
            found mutable reference `&mut Chars<'_>`
note: associated function defined here
   --> /workspaces/macroquad-test/crates/nanoserde/src/serde_json.rs:82:8
    |
82  |     fn deserialize_json(input: &str) -> Result<Self, DeJsonErr> {
    |        ^^^^^^^^^^^^^^^^
    = note: this error originates in the derive macro `DeJson` (in Nightly builds, run with -Z macro-backtrace for more info)
```

This change should fix that.
  • Loading branch information
Adjective-Object authored and knickish committed Oct 7, 2024
1 parent 938fcf8 commit 48aa068
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions derive/src/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ pub fn derive_de_json_proxy(proxy_type: &str, type_: &str) -> TokenStream {
format!(
"impl 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::deserialize_json(i)?;
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)?;
::core::result::Result::Ok(Into::into(&proxy))
}}
}}",
Expand Down
36 changes: 36 additions & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,42 @@ fn field_proxy() {
assert!(test == test_deserialized);
}

#[test]
fn object_proxy() {
#[derive(DeJson, SerJson, PartialEq, Debug)]
#[nserde(proxy = "Serializable")]
pub struct NonSerializable {
foo: i32,
}

#[derive(DeJson, SerJson, PartialEq, Debug)]
pub struct Serializable {
x: i32,
}

impl From<&NonSerializable> for Serializable {
fn from(non_serializable: &NonSerializable) -> Serializable {
Serializable {
x: non_serializable.foo,
}
}
}
impl From<&Serializable> for NonSerializable {
fn from(serializable: &Serializable) -> NonSerializable {
NonSerializable {
foo: serializable.x,
}
}
}

let test = NonSerializable { foo: 6 };

let bytes = SerJson::serialize_json(&test);
let test_deserialized = DeJson::deserialize_json(&bytes).unwrap();

assert!(test == test_deserialized);
}

#[test]
fn field_option_proxy() {
#[derive(PartialEq, Clone, Debug)]
Expand Down

0 comments on commit 48aa068

Please sign in to comment.