diff --git a/src/csv/args.rs b/src/csv/args.rs index c1d1001..9ee0ae8 100644 --- a/src/csv/args.rs +++ b/src/csv/args.rs @@ -3,18 +3,18 @@ use clap::Parser; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] pub struct MyArgs { - #[arg(name = "source")] // Define the argument without a short or long version + #[arg(name = "source", help = "First file of the comparision.")] pub source: String, - #[arg(name = "compare")] // Define the argument without a short or long version + #[arg(name = "compare", help = "Second file of the comparision.")] pub compare: String, - #[arg(short, long)] + #[arg(short, long, help = "Column which is base of the comparision.")] pub column: usize, - #[arg(short, long)] + #[arg(short, long, help = "Optional output filename.")] pub output: Option, - #[arg(short, long, default_value_t = ';'.to_string())] + #[arg(short, long, default_value_t = ';'.to_string(), help = "Column separator.")] pub delimiter: String, } diff --git a/src/csv/compare.rs b/src/csv/compare.rs index 8650126..6aa63d9 100644 --- a/src/csv/compare.rs +++ b/src/csv/compare.rs @@ -9,46 +9,27 @@ use super::args::MyArgs; #[derive(Debug)] pub struct Params { - delimiter: String, - source: String, - compared: String, - column: usize, - output: Option, + pub delimiter: String, + pub source: String, + pub compared: String, + pub column: usize, + pub output: Option, } -impl Params { - pub fn new_from_matches(matches: MyArgs) -> Self { - Self { +impl From for Params { + + fn from(matches: MyArgs) -> Self { + Params { delimiter: matches.delimiter, source: matches.source, compared: matches.compare, column: matches.column, output: { let this = matches.output; - this.map(|x| x.to_string()) + this.map(|x| ToString::to_string(&x)) }, } } - - pub fn delimiter(&self) -> &String { - &self.delimiter - } - - pub fn source(&self) -> &String { - &self.source - } - - pub fn compared(&self) -> &String { - &self.compared - } - - pub fn column(&self) -> usize { - self.column - } - - pub fn output_mut(&mut self) -> &Option { - &self.output - } } pub struct CsvLine { @@ -88,7 +69,11 @@ pub fn write_to_file(filename: String, diff: Vec) { } } -pub fn compare(reader: BufReader, reader2: BufReader, params: &Params) -> Result, String> { +pub fn compare( + reader: BufReader, + reader2: BufReader, + params: &Params, +) -> Result, String> { let vec1: Vec = reader .lines() .map(|line| { @@ -120,7 +105,7 @@ pub fn compare(reader: BufReader, reader2: BufReader, params: &Param fn find_diff(vec1: &[CsvLine], vec2: &[CsvLine], params: &Params) -> Result, String> { let mut diff: Vec = Vec::new(); for i in vec1.iter() { - let splitter: Vec<&str> = i.line.split(params.delimiter()).collect(); + let splitter: Vec<&str> = i.line.split(¶ms.delimiter).collect(); if splitter.len() <= params.column { return Err("Column out of bound".to_string()); } @@ -128,8 +113,8 @@ fn find_diff(vec1: &[CsvLine], vec2: &[CsvLine], params: &Params) -> Result = r.line.split(params.delimiter()).collect(); - let search = splitter_b[params.column()]; + let splitter_b: Vec<&str> = r.line.split(¶ms.delimiter).collect(); + let search = splitter_b[params.column]; search == value }), None diff --git a/src/main.rs b/src/main.rs index 8234fbe..adbcd8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,17 +8,13 @@ use csv::compare; use crate::csv::args::MyArgs; use crate::csv::compare::{write_to_file, Params}; -/// # Panics -/// -/// Panics if delimter is set wrong and column to compare -/// is not available. fn main() { let args = MyArgs::parse(); - let mut params: Params = Params::new_from_matches(args); + let params: Params = Params::from(args); let file: File = { - let this = File::open(params.source()); + let this = File::open(¶ms.source); match this { Ok(file) => file, Err(e) => { @@ -28,7 +24,7 @@ fn main() { } }; let file2 = { - let this = File::open(params.compared()); + let this = File::open(¶ms.compared); match this { Ok(file) => file, Err(e) => { @@ -49,7 +45,7 @@ fn main() { } }; - let option_string_ref: &Option = params.output_mut(); + let option_string_ref: Option = params.output; if let Some(string_ref) = option_string_ref { write_to_file(string_ref.clone(), diff); }