From 5dcdfa6e5c6f48de35f3e4f78a6c2eb6ca4bf5ca Mon Sep 17 00:00:00 2001 From: UlmerMan <manuel.ulmer@gmx.net> Date: Sat, 5 Apr 2025 12:59:40 +0200 Subject: [PATCH] Moved everything in lib.rs --- src/csv_writer.rs | 57 ---------------------------------------------- src/lib.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 59 deletions(-) delete mode 100644 src/csv_writer.rs diff --git a/src/csv_writer.rs b/src/csv_writer.rs deleted file mode 100644 index c56a3d5..0000000 --- a/src/csv_writer.rs +++ /dev/null @@ -1,57 +0,0 @@ -//! This is a general CSV writer that can be used to write any data to a CSV file. - -use crate::CSVData; - -/// Struct to write data to a CSV file. -pub struct CSVWriter<H, T> -where - H: IntoIterator<Item = String> + Clone, - T: CSVData, -{ - writer: csv::Writer<std::fs::File>, - header: H, - data: T, -} - -/// Implementation of CSVWriter -impl<H, T> CSVWriter<H, T> -where - H: IntoIterator<Item = String> + Clone, - T: CSVData, -{ - /// Creates a new instance of CSVWriter. - pub fn new(csv_file: String, header: H, data: T) -> CSVWriter<H, T> { - CSVWriter { - writer: csv::WriterBuilder::new() - .delimiter(b'|') - .quote_style(csv::QuoteStyle::Never) - .from_path(csv_file) - .unwrap(), - header, - data, - } - } - - /// Writes the header to the CSV file. - pub fn write_header(&mut self) { - self.writer - .write_record( - self.header - .clone() - .into_iter() - .collect::<Vec<_>>() - .as_slice(), - ) - .unwrap(); - self.writer.flush().unwrap(); - } - - /// Writes the data to the CSV file. - pub fn write_data(&mut self) { - self.data.refresh(); - self.writer - .write_record(self.data.as_csv_record().as_slice()) - .unwrap(); - self.writer.flush().unwrap(); - } -} diff --git a/src/lib.rs b/src/lib.rs index a0933e0..f675642 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -pub mod csv_writer; +//! This is a general CSV writer that can be used to write any data to a CSV file. /// Trait for data that can be written to a CSV file. pub trait CSVData { @@ -10,4 +10,58 @@ pub trait CSVData { /// Returns the data as a CSV record. fn as_csv_record(&self) -> Vec<String>; -} \ No newline at end of file +} + +/// Struct to write data to a CSV file. +pub struct CSVWriter<H, T> +where + H: IntoIterator<Item = String> + Clone, + T: CSVData, +{ + writer: csv::Writer<std::fs::File>, + header: H, + data: T, +} + +/// Implementation of CSVWriter +impl<H, T> CSVWriter<H, T> +where + H: IntoIterator<Item = String> + Clone, + T: CSVData, +{ + /// Creates a new instance of CSVWriter. + pub fn new(csv_file: String, header: H, data: T) -> CSVWriter<H, T> { + CSVWriter { + writer: csv::WriterBuilder::new() + .delimiter(b'|') + .quote_style(csv::QuoteStyle::Never) + .from_path(csv_file) + .unwrap(), + header, + data, + } + } + + /// Writes the header to the CSV file. + pub fn write_header(&mut self) { + self.writer + .write_record( + self.header + .clone() + .into_iter() + .collect::<Vec<_>>() + .as_slice(), + ) + .unwrap(); + self.writer.flush().unwrap(); + } + + /// Writes the data to the CSV file. + pub fn write_data(&mut self) { + self.data.refresh(); + self.writer + .write_record(self.data.as_csv_record().as_slice()) + .unwrap(); + self.writer.flush().unwrap(); + } +} -- GitLab