moved logic to lib.rs
parent
0146db0a18
commit
e6e13c651f
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
58
src/main.rs
58
src/main.rs
|
@ -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::fs::File;
|
||||||
use std::io::{BufRead, BufReader, Write};
|
use std::io::{BufReader};
|
||||||
#[macro_use]
|
|
||||||
extern crate clap;
|
|
||||||
|
|
||||||
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() {
|
fn main() {
|
||||||
let yaml = load_yaml!("cli.yml");
|
let yaml = load_yaml!("cli.yml");
|
||||||
let matches = App::from_yaml(yaml).get_matches();
|
let matches = App::from_yaml(yaml).get_matches();
|
||||||
|
@ -84,10 +41,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.output.is_empty() == false {
|
if params.output.is_empty() == false {
|
||||||
let mut file = File::create(params.output).unwrap();
|
write_to_file(params, diff);
|
||||||
for line in diff.iter() {
|
|
||||||
writeln!(&mut file, "{}", line.line).unwrap();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
print_lines(diff);
|
print_lines(diff);
|
||||||
}
|
}
|
||||||
|
@ -97,7 +51,7 @@ fn main() {
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{fs::File, io::BufReader, path::Path};
|
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> {
|
fn get_reader() -> BufReader<File> {
|
||||||
let path = Path::new("./assets/csv1.csv");
|
let path = Path::new("./assets/csv1.csv");
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"rustc_fingerprint":8779373148605506356,"outputs":{"17598535894874457435":{"success":true,"status":"","code":0,"stdout":"rustc 1.54.0 (a178d0322 2021-07-26)\nbinary: rustc\ncommit-hash: a178d0322ce20e33eac124758e837cbd80a6f633\ncommit-date: 2021-07-26\nhost: x86_64-unknown-linux-gnu\nrelease: 1.54.0\nLLVM version: 12.0.1\n","stderr":""},"2797684049618456168":{"success":false,"status":"exit status: 1","code":1,"stdout":"","stderr":"error: `-Csplit-debuginfo` is unstable on this platform\n\n"},"931469667778813386":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/mace/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
|
{"rustc_fingerprint":8779373148605506356,"outputs":{"931469667778813386":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/mace/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""},"17598535894874457435":{"success":true,"status":"","code":0,"stdout":"rustc 1.54.0 (a178d0322 2021-07-26)\nbinary: rustc\ncommit-hash: a178d0322ce20e33eac124758e837cbd80a6f633\ncommit-date: 2021-07-26\nhost: x86_64-unknown-linux-gnu\nrelease: 1.54.0\nLLVM version: 12.0.1\n","stderr":""},"2797684049618456168":{"success":false,"status":"exit status: 1","code":1,"stdout":"","stderr":"error: `-Csplit-debuginfo` is unstable on this platform\n\n"}},"successes":{}}
|
|
@ -0,0 +1 @@
|
||||||
|
a3991e01b75db47f
|
|
@ -0,0 +1 @@
|
||||||
|
{"rustc":17807758859236181817,"features":"[]","target":14477650929426316123,"profile":1144844575097113612,"path":1036222786711178230,"deps":[[1107353818263796810,"csvcompare",false,15761132437638236415],[4345520495555234844,"clap",false,8805950514040045324]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/csvcompare-0a3eeaf976571bb4/dep-bin-csvcompare"}}],"rustflags":[],"metadata":8456049419002105797,"config":0,"compile_kind":0}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
This file has an mtime of when this was started.
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
This file has an mtime of when this was started.
|
|
@ -0,0 +1 @@
|
||||||
|
98338d7bc269fd3b
|
|
@ -0,0 +1 @@
|
||||||
|
{"rustc":17807758859236181817,"features":"[]","target":14477650929426316123,"profile":14050059120794533848,"path":1036222786711178230,"deps":[[1107353818263796810,"csvcompare",false,11213782078068079805],[4345520495555234844,"clap",false,14202572740688878169]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/csvcompare-6794bca2257b8e78/dep-test-bin-csvcompare"}}],"rustflags":[],"metadata":8456049419002105797,"config":0,"compile_kind":0}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
This file has an mtime of when this was started.
|
|
@ -0,0 +1 @@
|
||||||
|
ff48502b72cabada
|
|
@ -0,0 +1 @@
|
||||||
|
{"rustc":17807758859236181817,"features":"[]","target":12726061379515898114,"profile":1144844575097113612,"path":10872709659218687626,"deps":[[4345520495555234844,"clap",false,8805950514040045324]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/csvcompare-8a2e071e9ed95a04/dep-lib-csvcompare"}}],"rustflags":[],"metadata":8456049419002105797,"config":0,"compile_kind":0}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
This file has an mtime of when this was started.
|
|
@ -0,0 +1 @@
|
||||||
|
d4437e130a6797d4
|
|
@ -0,0 +1 @@
|
||||||
|
{"rustc":17807758859236181817,"features":"[]","target":14477650929426316123,"profile":6415348288391478785,"path":1036222786711178230,"deps":[[1107353818263796810,"csvcompare",false,15761132437638236415],[4345520495555234844,"clap",false,8805950514040045324]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/csvcompare-b0d53b777eb69535/dep-test-bin-csvcompare"}}],"rustflags":[],"metadata":8456049419002105797,"config":0,"compile_kind":0}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
This file has an mtime of when this was started.
|
|
@ -0,0 +1 @@
|
||||||
|
bdbce307635b9f9b
|
|
@ -0,0 +1 @@
|
||||||
|
{"rustc":17807758859236181817,"features":"[]","target":12726061379515898114,"profile":18108590124580271077,"path":10872709659218687626,"deps":[[4345520495555234844,"clap",false,14202572740688878169]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/csvcompare-ba87fe827ffc5437/dep-lib-csvcompare"}}],"rustflags":[],"metadata":8456049419002105797,"config":0,"compile_kind":0}
|
|
@ -0,0 +1 @@
|
||||||
|
3b84b08a6c5bc3e7
|
|
@ -0,0 +1 @@
|
||||||
|
{"rustc":17807758859236181817,"features":"[]","target":14477650929426316123,"profile":18108590124580271077,"path":1036222786711178230,"deps":[[1107353818263796810,"csvcompare",false,11213782078068079805],[4345520495555234844,"clap",false,14202572740688878169]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/csvcompare-da6c08677d53a299/dep-bin-csvcompare"}}],"rustflags":[],"metadata":8456049419002105797,"config":0,"compile_kind":0}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
This file has an mtime of when this was started.
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
This file has an mtime of when this was started.
|
|
@ -0,0 +1 @@
|
||||||
|
9ef17e73f3fa74a5
|
|
@ -0,0 +1 @@
|
||||||
|
{"rustc":17807758859236181817,"features":"[]","target":12726061379515898114,"profile":6415348288391478785,"path":10872709659218687626,"deps":[[4345520495555234844,"clap",false,8805950514040045324]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/csvcompare-e511a0deeeacf128/dep-test-lib-csvcompare"}}],"rustflags":[],"metadata":8456049419002105797,"config":0,"compile_kind":0}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
This file has an mtime of when this was started.
|
|
@ -0,0 +1 @@
|
||||||
|
dbb8c82f878d4260
|
|
@ -0,0 +1 @@
|
||||||
|
{"rustc":17807758859236181817,"features":"[]","target":12726061379515898114,"profile":14050059120794533848,"path":10872709659218687626,"deps":[[4345520495555234844,"clap",false,14202572740688878169]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/csvcompare-f55edbeb9973f651/dep-test-lib-csvcompare"}}],"rustflags":[],"metadata":8456049419002105797,"config":0,"compile_kind":0}
|
Binary file not shown.
|
@ -1 +1 @@
|
||||||
/home/mace/repos/rust/csvcompare/target/debug/csvcompare: /home/mace/repos/rust/csvcompare/src/cli.yml /home/mace/repos/rust/csvcompare/src/main.rs
|
/home/mace/repos/rust/csvcompare/target/debug/csvcompare: /home/mace/repos/rust/csvcompare/src/cli.yml /home/mace/repos/rust/csvcompare/src/lib.rs /home/mace/repos/rust/csvcompare/src/main.rs
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-0a3eeaf976571bb4.rmeta: src/main.rs src/cli.yml
|
||||||
|
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-0a3eeaf976571bb4.d: src/main.rs src/cli.yml
|
||||||
|
|
||||||
|
src/main.rs:
|
||||||
|
src/cli.yml:
|
Binary file not shown.
|
@ -0,0 +1,6 @@
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-6794bca2257b8e78: src/main.rs src/cli.yml
|
||||||
|
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-6794bca2257b8e78.d: src/main.rs src/cli.yml
|
||||||
|
|
||||||
|
src/main.rs:
|
||||||
|
src/cli.yml:
|
|
@ -0,0 +1,5 @@
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-8a2e071e9ed95a04.rmeta: src/lib.rs
|
||||||
|
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-8a2e071e9ed95a04.d: src/lib.rs
|
||||||
|
|
||||||
|
src/lib.rs:
|
|
@ -0,0 +1,6 @@
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-b0d53b777eb69535.rmeta: src/main.rs src/cli.yml
|
||||||
|
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-b0d53b777eb69535.d: src/main.rs src/cli.yml
|
||||||
|
|
||||||
|
src/main.rs:
|
||||||
|
src/cli.yml:
|
|
@ -0,0 +1,7 @@
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-ba87fe827ffc5437.rmeta: src/lib.rs
|
||||||
|
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/libcsvcompare-ba87fe827ffc5437.rlib: src/lib.rs
|
||||||
|
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-ba87fe827ffc5437.d: src/lib.rs
|
||||||
|
|
||||||
|
src/lib.rs:
|
Binary file not shown.
|
@ -0,0 +1,6 @@
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-da6c08677d53a299: src/main.rs src/cli.yml
|
||||||
|
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-da6c08677d53a299.d: src/main.rs src/cli.yml
|
||||||
|
|
||||||
|
src/main.rs:
|
||||||
|
src/cli.yml:
|
|
@ -0,0 +1,5 @@
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-e511a0deeeacf128.rmeta: src/lib.rs
|
||||||
|
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-e511a0deeeacf128.d: src/lib.rs
|
||||||
|
|
||||||
|
src/lib.rs:
|
Binary file not shown.
|
@ -0,0 +1,5 @@
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-f55edbeb9973f651: src/lib.rs
|
||||||
|
|
||||||
|
/home/mace/repos/rust/csvcompare/target/debug/deps/csvcompare-f55edbeb9973f651.d: src/lib.rs
|
||||||
|
|
||||||
|
src/lib.rs:
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue