You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#[derive(Debug, serde::Deserialize)]
struct Test {
symbol: String,
#[serde(default)]
up: f64,
}
#[test]
fn deserialise_empty_up() {
let s = "symbol,up\ndfsdfws,";
let mut rdr = csv::Reader::from_reader(s.as_bytes());
for row in rdr.deserialize() {
let option: Test = row.expect("Failure");
dbg!(&option);
}
}
The field up is empty in the string; this should default to 0.0; but instead it fails with
I think, this is mostly rust-csv issue. #[serde(default)] applied only to missed fields. It is up to rust-csv to say whether field exist or not in CSV data. It decides that if field is declared in the header it is exist.
It could be hard to fix this error only on rust-csv side, because proper solution dependent also on the deserialized type. For example, for String empty string is a valid data and you probably does not want, that you will not be able to deserialize empty strings without #[serde(default)] on the string field. From the other hand for f64 empty string is invalid data. Probably this can be solved by having a setting in the rust-csv for selecting a behaviour (treat_empty_fields_as_missing).
The field
up
is empty in the string; this should default to 0.0; but instead it fails withthread 'tests::deserialise' panicked at 'Failure: Error(Deserialize { pos: Some(Position { byte: 10, line: 2, record: 1 }), err: DeserializeError { field: Some(1), kind: ParseFloat(ParseFloatError { kind: Empty }) } })', data_model\examples\upload_options.rs:89:36
Setting it to 0.0 works fine
Using
serde = { version = "^1.0", features = ["derive"] }
The text was updated successfully, but these errors were encountered: