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 ?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With