moved logic to lib.rs

This commit is contained in:
2021-08-29 13:46:12 +02:00
parent 0146db0a18
commit e6e13c651f
203 changed files with 138 additions and 54 deletions
+59
View File
@@ -0,0 +1,59 @@
pub mod csv_compare {
use std::io::Write as IoWrite;
use std::{
fs::File,
io::{BufRead, BufReader},
};
use clap::ArgMatches;
pub struct Params {
pub delimiter: String,
pub source: String,
pub compared: String,
pub column: usize,
pub output: String,
}
impl Params {
pub fn new_from_matches(matches: ArgMatches) -> Self {
Self {
delimiter: matches.value_of("delimiter").unwrap_or(";").to_string(),
source: matches.value_of("source").unwrap().to_string(),
compared: matches.value_of("compared").unwrap().to_string(),
column: matches.value_of("column").unwrap().parse().unwrap(),
output: matches.value_of("output").unwrap().to_string(),
}
}
}
pub struct CsvLine {
pub line: String,
}
impl CsvLine {
pub fn new(line: String) -> Self {
Self { line }
}
}
pub fn load_vector(vector: &mut Vec<CsvLine>, reader: BufReader<File>) {
// Read the file line by line using the lines() iterator from std::io::BufRead.
for (_index, line) in reader.lines().enumerate() {
let line = line.unwrap();
vector.push(CsvLine::new(line.to_string()));
}
}
pub fn print_lines(vector: Vec<CsvLine>) {
for line in vector.iter() {
println!("{}", line.line);
}
}
pub fn write_to_file(params: Params, diff: Vec<CsvLine>) {
let mut file = File::create(params.output).unwrap();
for line in diff.iter() {
writeln!(&mut file, "{}", line.line).unwrap();
}
}
}
+6 -52
View File
@@ -1,52 +1,9 @@
use clap::{App, ArgMatches};
use clap::{App, load_yaml};
use csvcompare::csv_compare::{CsvLine, Params, load_vector, print_lines, write_to_file};
use std::fs::File;
use std::io::{BufRead, BufReader, Write};
#[macro_use]
extern crate clap;
use std::io::{BufReader};
struct Params {
delimiter: String,
source: String,
compared: String,
column: usize,
output: String,
}
impl Params {
pub fn new_from_matches(matches: ArgMatches) -> Self {
Self {
delimiter: matches.value_of("delimiter").unwrap_or(";").to_string(),
source: matches.value_of("source").unwrap().to_string(),
compared: matches.value_of("compared").unwrap().to_string(),
column: matches.value_of("column").unwrap().parse().unwrap(),
output: matches.value_of("output").unwrap().to_string(),
}
}
}
pub(crate) struct CsvLine {
line: String,
}
impl CsvLine {
pub fn new(line: String) -> Self {
Self { line }
}
}
pub(crate) fn load_vector(vector: &mut Vec<CsvLine>, reader: BufReader<File>) {
// Read the file line by line using the lines() iterator from std::io::BufRead.
for (_index, line) in reader.lines().enumerate() {
let line = line.unwrap();
vector.push(CsvLine::new(line.to_string()));
}
}
fn print_lines(vector: Vec<CsvLine>) {
for line in vector.iter() {
println!("{}", line.line);
}
}
fn main() {
let yaml = load_yaml!("cli.yml");
let matches = App::from_yaml(yaml).get_matches();
@@ -84,10 +41,7 @@ fn main() {
}
if params.output.is_empty() == false {
let mut file = File::create(params.output).unwrap();
for line in diff.iter() {
writeln!(&mut file, "{}", line.line).unwrap();
}
write_to_file(params, diff);
} else {
print_lines(diff);
}
@@ -97,7 +51,7 @@ fn main() {
mod tests {
use std::{fs::File, io::BufReader, path::Path};
use crate::{load_vector, CsvLine};
use csvcompare::csv_compare::{CsvLine, load_vector};
fn get_reader() -> BufReader<File> {
let path = Path::new("./assets/csv1.csv");