From b846ba30144633ae1275e8fab9eeabb6a9d87642 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sun, 27 Nov 2022 22:44:40 +0100 Subject: [PATCH] make csv writer more configurable --- satrs-mib/src/res_code.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/satrs-mib/src/res_code.rs b/satrs-mib/src/res_code.rs index dabf939..9635ec0 100644 --- a/satrs-mib/src/res_code.rs +++ b/satrs-mib/src/res_code.rs @@ -57,12 +57,12 @@ impl From for ResultU16InfoSerializable { #[cfg(feature = "std")] pub mod stdmod { + use std::fs::File; use super::*; use std::io; - use std::path::Path; - pub fn print_resultcodes_as_csv(codes: &[ResultU16Info]) -> Result<(), csv::Error> { - let mut wtr = csv::Writer::from_writer(io::stdout()); + pub fn print_resultcodes_as_csv(writer_builder: csv::WriterBuilder, codes: &[ResultU16Info]) -> Result<(), csv::Error> { + let mut wtr = writer_builder.from_writer(io::stdout()); for result in codes { wtr.serialize(ResultU16InfoSerializable::from(*result))?; } @@ -71,10 +71,11 @@ pub mod stdmod { } pub fn write_resultcodes_to_csv( + writer_builder: csv::WriterBuilder, results: &[ResultU16Info], - path: impl AsRef, + file: File, ) -> Result<(), csv::Error> { - let mut wtr = csv::Writer::from_path(path)?; + let mut wtr = writer_builder.from_writer(file); for result_code in results { wtr.serialize(ResultU16InfoSerializable::from(*result_code))?; } @@ -119,13 +120,18 @@ mod tests { #[test] fn test_printout() { - print_resultcodes_as_csv(TMTC_RESULTS).expect("Priting result codes failed"); + let mut wtrb = csv::WriterBuilder::new(); + wtrb.delimiter(';' as u8); + print_resultcodes_as_csv(wtrb, TMTC_RESULTS).expect("Priting result codes failed"); } #[test] fn test_csv_export() { let csvpath = Path::new(CSV_NAME); - write_resultcodes_to_csv(TMTC_RESULTS, csvpath).expect("CSV export failed"); + let mut wtrb = csv::WriterBuilder::new(); + let file = File::create(csvpath).expect("Creating CSV file failed"); + wtrb.delimiter(';' as u8); + write_resultcodes_to_csv(wtrb, TMTC_RESULTS, file).expect("CSV export failed"); assert!(csvpath.exists()); let file = File::open(csvpath).expect("Opening CSV file failed"); let buf_reader = BufReader::new(file); @@ -134,22 +140,22 @@ mod tests { 0 => { assert!(line.is_ok()); let line = line.unwrap(); - assert_eq!(line, "raw,group_id,unique_id,name,group_str,info"); + assert_eq!(line, "raw;group_id;unique_id;name;group_str;info"); } 1 => { assert!(line.is_ok()); let line = line.unwrap(); - assert_eq!(line, "0x0000,0x00,0x00,INVALID_PUS_SERVICE,,"); + assert_eq!(line, "0x0000;0x00;0x00;INVALID_PUS_SERVICE;;"); } 2 => { assert!(line.is_ok()); let line = line.unwrap(); - assert_eq!(line, "0x0001,0x00,0x01,INVALID_PUS_SUBSERVICE,,"); + assert_eq!(line, "0x0001;0x00;0x01;INVALID_PUS_SUBSERVICE;;"); } 3 => { assert!(line.is_ok()); let line = line.unwrap(); - assert_eq!(line, "0x0002,0x00,0x02,NOT_ENOUGH_APP_DATA,,Not enough data inside the TC application data field"); + assert_eq!(line, "0x0002;0x00;0x02;NOT_ENOUGH_APP_DATA;;Not enough data inside the TC application data field"); } _ => (), }