rust-csv
rust-csv copied to clipboard
Unexpected UTF-8 error
version: 1.1.6
Problem
When deserializing a CSV file without headers into a struct (with serde), a UTF-8 error is returned, even though I think the file is valid UTF-8.
Code
use std::path::Path;
use csv::{ReaderBuilder, Terminator};
use serde::{de::DeserializeOwned, Deserialize};
fn main() {
let filename = std::env::args().next().unwrap();
let records: Vec<PopulationRecord> = read_records(filename).unwrap();
println!("{:?}", records);
}
#[derive(Debug, Deserialize)]
struct PopulationRecord {
city: String,
state: String,
country: String,
population: u64,
}
fn read_records<P: AsRef<Path>, R: DeserializeOwned>(path: P) -> Result<Vec<R>, csv::Error> {
let mut rdr = ReaderBuilder::new()
.has_headers(false)
.terminator(Terminator::CRLF)
.from_path(path)?;
rdr.deserialize().collect()
}
Observed behaviour
I have downloaded the smallpop-no-headers.csv file from the examples directory. Then running the above program on it gives:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error(Utf8 { pos: Some(Position { byte: 0, line: 1, record: 0 }), err: Utf8Error { field: 0, valid_up_to: 25 } })', src/main.rs:8:65
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expected behaviour
Should give no error, but print the records.