Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a default String value to a deserialized serde struct fails with trait Deserialize<'_> not implemented

Tags:

rust

serde

I have the following struct that is deserialized from an XML file.

#[derive(Deserialize, Debug)]
pub struct RawItem {
    id: String,
    title: String,
    description: Option<String>,
    link: String,
    image_link: String,
    availability: String,
    price: String,
}

So far so good, the object is properly deserialized. Now, my XML data has slightly changed and the price field got optional. I want to price to fallback to "0.0" when missing, therefore I used serde default mechanism :

#[derive(Deserialize, Debug)]
pub struct RawItem {
    id: String,
    title: String,
    description: Option<String>,
    link: String,
    image_link: String,
    availability: String,
    #[serde(default = "0.0")] // Added this new line
    price: String,
}

For some reason, it fails with the following error :

error: failed to parse path: "0.0"
  --> /home/xxxxxx/xxxxx/xxxxx/xxxxxx/xxxx/xxxxx/src/xxxx_specifics.rs:81:20
   |
81 |     #[serde(default = "0.0")]
   |                       ^^^^^

error[E0277]: the trait bound `xxxx_specifics::RawItem: Deserialize<'_>` is not satisfied
    --> /home/xxxxxx/xxxxx/xxxxx/xxxxxx/xxxx/xxxxx/src/xxxx_specifics.rs:68:2
     |
68   |     #[serde(rename = "item")]
     |     ^ the trait `Deserialize<'_>` is not implemented for `xxxx_specifics::RawItem`
     |
     = note: required because of the requirements on the impl of `Deserialize<'_>` for `Vec<xxxx_specifics::RawItem>`
note: required by `next_element`
    --> /home/xxxxx/serde-1.0.130/src/de/mod.rs:1703:5
     |
1703 | /     fn next_element<T>(&mut self) -> Result<Option<T>, Self::Error>
1704 | |     where
1705 | |         T: Deserialize<'de>,
     | |____________________________^

error[E0277]: the trait bound `xxxx_specifics::RawItem: Deserialize<'_>` is not satisfied
    --> /home/xxxxxx/xxxxx/xxxxx/xxxxxx/xxxx/xxxxx/src/xxxx_specifics.rs:68:2
     |
68   |     #[serde(rename = "item")]
     |     ^ the trait `Deserialize<'_>` is not implemented for `xxxx_specifics::RawItem`
     |
     = note: required because of the requirements on the impl of `Deserialize<'_>` for `Vec<xxxx_specifics::RawItem>`
note: required by `next_value`
    --> /home/xxxx/serde-1.0.130/src/de/mod.rs:1842:5
     |
1842 | /     fn next_value<V>(&mut self) -> Result<V, Self::Error>
1843 | |     where
1844 | |         V: Deserialize<'de>,
     | |____________________________^

For more information about this error, try `rustc --explain E0277`.
error: could not compile `inko_importer` due to 3 previous errors

Any reason with serde's default would fail with a simple default value on a String ?

like image 679
Overdrivr Avatar asked Sep 13 '25 02:09

Overdrivr


1 Answers

default attribute expects a method (actually a string with the method path) to be called:

use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct RawItem {
    #[serde(default = "default_0")] // Added this new line
    price: String
}

fn default_0() -> String {
    "0.0".to_string()
}

Playground

like image 90
Netwave Avatar answered Sep 14 '25 22:09

Netwave