Crates.io | csv-stream |
lib.rs | csv-stream |
version | 0.1.3 |
source | src |
created_at | 2021-11-26 14:19:04.24906 |
updated_at | 2021-11-26 15:00:47.326634 |
description | For building CSVs as Streams or Iterators |
homepage | |
repository | https://github.com/conradludgate/csv-stream |
max_upload_size | |
id | 488032 |
size | 90,553 |
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
"#
);