improve help, implement From trait, remove getters
parent
43a25a8ec9
commit
ec59af73f2
|
@ -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<String>,
|
||||
|
||||
#[arg(short, long, default_value_t = ';'.to_string())]
|
||||
#[arg(short, long, default_value_t = ';'.to_string(), help = "Column separator.")]
|
||||
pub delimiter: String,
|
||||
}
|
||||
|
|
|
@ -9,46 +9,27 @@ use super::args::MyArgs;
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Params {
|
||||
delimiter: String,
|
||||
source: String,
|
||||
compared: String,
|
||||
column: usize,
|
||||
output: Option<String>,
|
||||
pub delimiter: String,
|
||||
pub source: String,
|
||||
pub compared: String,
|
||||
pub column: usize,
|
||||
pub output: Option<String>,
|
||||
}
|
||||
|
||||
impl Params {
|
||||
pub fn new_from_matches(matches: MyArgs) -> Self {
|
||||
Self {
|
||||
impl From<MyArgs> 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<String> {
|
||||
&self.output
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CsvLine {
|
||||
|
@ -88,7 +69,11 @@ pub fn write_to_file(filename: String, diff: Vec<CsvLine>) {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn compare(reader: BufReader<File>, reader2: BufReader<File>, params: &Params) -> Result<Vec<CsvLine>, String> {
|
||||
pub fn compare(
|
||||
reader: BufReader<File>,
|
||||
reader2: BufReader<File>,
|
||||
params: &Params,
|
||||
) -> Result<Vec<CsvLine>, String> {
|
||||
let vec1: Vec<CsvLine> = reader
|
||||
.lines()
|
||||
.map(|line| {
|
||||
|
@ -120,7 +105,7 @@ pub fn compare(reader: BufReader<File>, reader2: BufReader<File>, params: &Param
|
|||
fn find_diff(vec1: &[CsvLine], vec2: &[CsvLine], params: &Params) -> Result<Vec<CsvLine>, String> {
|
||||
let mut diff: Vec<CsvLine> = 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<Vec<
|
|||
|
||||
let found = !matches!(
|
||||
vec2.iter().position(|r| {
|
||||
let splitter_b: Vec<&str> = 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
|
||||
|
|
12
src/main.rs
12
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<String> = params.output_mut();
|
||||
let option_string_ref: Option<String> = params.output;
|
||||
if let Some(string_ref) = option_string_ref {
|
||||
write_to_file(string_ref.clone(), diff);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue