File size: 1,707 Bytes
d5bfab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
use csv::WriterBuilder;
use serde::Serialize;
use std::error::Error;
use std::path::Path;
pub fn create_csv_file<S: Serialize>(records: &Vec<S>, output_path: &Path) -> Result<(), Box<dyn Error>> {
let mut wtr = WriterBuilder::new()
.has_headers(true)
.delimiter(b';')
.from_path(output_path)?;
for record in records {
wtr.serialize(record)?;
}
wtr.flush()?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
use std::fs;
#[derive(Serialize)]
struct Record {
count: u32,
instruction: String,
constant: i32,
}
fn create_record(count: u32, instruction: &str, constant: i32) -> Record {
Record {
count: count,
instruction: instruction.to_string(),
constant: constant
}
}
#[test]
fn test_10000_create_csv_file() -> Result<(), Box<dyn Error>> {
// Arrange
let tempdir = tempfile::tempdir().unwrap();
let basedir = PathBuf::from(&tempdir.path()).join("test_10000_create_csv_file");
fs::create_dir(&basedir)?;
let csv_path: PathBuf = basedir.join("data.csv");
let records: Vec<Record> = vec![
create_record(5, "mov", -3),
create_record(17, "add", 8),
create_record(99, "gcd", 42),
];
// Act
create_csv_file(&records, &csv_path)?;
// Assert
let expected_file_content =
r#"count;instruction;constant
5;mov;-3
17;add;8
99;gcd;42
"#;
let actual_file_content: String = fs::read_to_string(&csv_path)?;
assert_eq!(actual_file_content, expected_file_content);
Ok(())
}
}
|