BurntSushi / BurntSushi/rust-csv
document usage of #[serde(flatten)] more thoroughly
- Dominant language
- Rust
- Stars
- 2k
- Forks
- 257
- PR merge metrics
- No merged PRs in 30d
Description
I got a cool bug report on a project of mine that uses the csv crate: https://github.com/LPGhatguy/rojo/issues/145
I deserialize files into a `Vec` of a struct with a shape like this:
```rust
#[derive(Debug, Deserialize)]
struct SomethingEntry {
name: String,
#[serde(flatten)]
values: HashMap,
}
```
The actual structure uses `#[serde(flatten)]` to capture extra columns since it's used in a localization format for a game engine.
#### What version of the `csv` crate are you using?
`1.0.5`
#### Briefly describe the question, bug or feature request.
Deserializing a field that uses `#[serde(flatten)]` on a `HashMap` fails if the value in a record looks like a number.
#### Include a complete program demonstrating a problem.
Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8d7a007589ba375a79a55b1d825ffdb5
```rust
extern crate csv; // 1.0.5
extern crate serde_derive; // 1.0.88
extern crate serde; // 1.0.88
use std::collections::HashMap;
use serde_derive::Deserialize;
#[derive(Debug, Deserialize)]
struct SomethingEntry {
name: String,
#[serde(flatten)]
values: HashMap,
}
fn main() {
let source = r#"name,stat
name,Benjamin
maxHealth,300"#;
let mut rdr = csv::Reader::from_reader(source.as_bytes());
let records: Vec = rdr.deserialize()
.map(Result::unwrap)
.collect();
println!("{:?}", records);
}
```
#### What is the observed behavior of the code above?
The program panics, since csv returns an error:
```
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error(Deserialize { pos: Some(Position { byte: 24, line: 3, record: 2 }), err: DeserializeError { field: None, kind: Message("invalid type: integer `300`, expected a string") } })', src/libcore/result.rs:997:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
```
#### What is the expected or desired behavior of the code above?
The first entry should be have a name of `name` and `values` set to `{"stat": "Benjamin"}`.
The second entry should have a name of `maxHealth` and `values` set to `{"stat": "300"}`.
Since the code works for most inputs, I don't expect it to start failing to parse just because the user put something into the spreadsheet that looks like a number.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the linked Rust playground example and read the full issue thread, including the linked csv crate report. The payload names no documentation file or test; done means adding a clear explanation of #[serde(flatten)] with HashMap and the numeric-looking CSV value case, including the expected behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100