csv-stream

Crates.iocsv-stream
lib.rscsv-stream
version0.1.3
sourcesrc
created_at2021-11-26 14:19:04.24906
updated_at2021-11-26 15:00:47.326634
descriptionFor building CSVs as Streams or Iterators
homepage
repositoryhttps://github.com/conradludgate/csv-stream
max_upload_size
id488032
size90,553
Conrad Ludgate (conradludgate)

documentation

README

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
"#
);
Commit count: 5

cargo fmt