#csv #serde #comma #delimited #field-name

csv-stream

For building CSVs as Streams or Iterators

4 releases

0.1.3 Nov 26, 2021
0.1.2 Nov 26, 2021
0.1.1 Nov 26, 2021
0.1.0 Nov 26, 2021

#1996 in Encoding

Unlicense/MIT

89KB
1.5K SLoC

csv-stream

For building CSVs as Streams or Iterators.

#[derive(Serialize)]
struct Row<'a> {
    city: &'a str,
    country: &'a str,
    // Serde allows us to name our headers exactly,
    // even if they don't match our struct field names.
    #[serde(rename = "popcount")]
    population: u64,
}

let rows = vec![
    Row {
        city: "Boston",
        country: "United States",
        population: 4628910,
    },
    Row {
        city: "Concord",
        country: "United States",
        population: 42695,
    },
];

let row_stream = futures::stream::iter(ROWS);
let csv_stream = WriterBuilder::default().build_stream(row_stream);

let mut buf = vec![];
while let Some(res) = csv_stream.next().await {
    buf.extend_from_slice(&res.unwrap())
}

let buf = String::from_utf8(buf).unwrap();

assert_eq!(
    buf,
    r#"city,country,popcount
Boston,United States,4628910
Concord,United States,42695
"#
);

Dependencies

~0.8–1.3MB
~22K SLoC