BurntSushi / BurntSushi/rust-csv
Deserializing a String field inside a flattende struct fails if the field contains a valid integer
- Dominant language
- Rust
- Stars
- 2k
- Forks
- 257
- PR merge metrics
- No merged PRs in 30d
Description
#### What version of the `csv` crate are you using?
1.3.0
#### Briefly describe the question, bug or feature request.
When deserializing to a `String` field using serde, `csv` can handle a field containing `123`.
However, when doing the same to a flattened field, `csv` can no longer deserialize `123` to the `String` field and I get the error:
```
Err(
Error(
Deserialize {
pos: Some(
Position {
byte: 24,
line: 4,
record: 3,
},
),
err: DeserializeError {
field: None,
kind: Message(
"invalid type: integer `123`, expected a string",
),
},
},
),
)
```
#### Include a complete program demonstrating a problem.
```rust
use serde::Deserialize;
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
struct Inner {
inner_str: String,
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
struct Row {
str: String,
#[serde(flatten)]
inner: Inner,
}
fn main() {
let source = r#"
str,inner_str
A,A
123,A
A,123
"#
.trim();
let mut reader = csv::Reader::from_reader(source.as_bytes());
for line in reader.deserialize::() {
dbg!(&line);
}
}
```
#### What is the observed behavior of the code above?
Output:
```
[src/main.rs:29] &line = Ok(
Row {
str: "A",
inner: Inner {
inner_str: "A",
},
},
)
[src/main.rs:29] &line = Ok(
Row {
str: "123",
inner: Inner {
inner_str: "A",
},
},
)
[src/main.rs:29] &line = Err(
Error(
Deserialize {
pos: Some(
Position {
byte: 24,
line: 4,
record: 3,
},
),
err: DeserializeError {
field: None,
kind: Message(
"invalid type: integer `123`, expected a string",
),
},
},
),
)
```
#### What is the expected or desired behavior of the code above?
Output:
```
[src/main.rs:29] &line = Ok(
Row {
str: "A",
inner: Inner {
inner_str: "A",
},
},
)
[src/main.rs:29] &line = Ok(
Row {
str: "123",
inner: Inner {
inner_str: "A",
},
},
)
[src/main.rs:29] &line = Ok(
Row {
str: "A",
inner: Inner {
inner_str: "123",
},
},
)
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.