Skip to content

Commit 4dee2ce

Browse files
committed
reorganize lib code
1 parent 372d9a3 commit 4dee2ce

File tree

20 files changed

+252
-188
lines changed

20 files changed

+252
-188
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "csv-txt-excel-parallel-toolkit"
3-
version = "0.4.15"
3+
version = "0.4.16"
44
edition = "2021"
55
authors = ["zhuang dai <[email protected]>"]
66
description = "A parallel and fast command line toolkit for small and large (>10G) CSV, TXT, and EXCEL files, with a unified api."

src/args.rs

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use crate::utils::{
2-
cli_result::E, file::is_excel, filename::full_path, row_split::CsvRowSplitter,
3-
util::get_valid_sep,
4-
};
1+
use crate::utils::{row_split::CsvRowSplitter, util::get_valid_sep};
52
use clap::Args;
6-
use std::path::PathBuf;
73

84
#[derive(Debug, Args)]
95
pub struct Count {
@@ -415,49 +411,6 @@ pub struct Unique {
415411
pub export: bool,
416412
}
417413

418-
macro_rules! command_run {
419-
($method:ident) => {
420-
impl $method {
421-
pub fn path(&self) -> PathBuf {
422-
let p = self.filename.as_ref().unwrap();
423-
full_path(p)
424-
}
425-
426-
pub fn run(&self) {
427-
match &self.filename {
428-
Some(f) => match is_excel(&full_path(f)) {
429-
true => self.excel_run(),
430-
false => self.csv_run(),
431-
},
432-
None => self.io_run(),
433-
}
434-
.handle_err()
435-
}
436-
}
437-
};
438-
}
439-
440-
command_run!(Count);
441-
command_run!(Estimate);
442-
command_run!(Head);
443-
command_run!(Tail);
444-
command_run!(Headers);
445-
command_run!(Clean);
446-
command_run!(Flatten);
447-
command_run!(Frequency);
448-
command_run!(Split);
449-
command_run!(Slice);
450-
command_run!(Select);
451-
command_run!(Stats);
452-
command_run!(Excel2csv);
453-
command_run!(Table);
454-
command_run!(Sort);
455-
command_run!(Search);
456-
command_run!(Sample);
457-
command_run!(To);
458-
command_run!(Unique);
459-
command_run!(Size);
460-
461414
macro_rules! impl_row_split {
462415
($cmd:ident) => {
463416
impl $cmd {

src/csv_lib/count.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
use crate::args::Count;
21
use crate::utils::return_result::{CliResultData, ResultData};
32
use std::fs::File;
43
use std::io::{BufRead, BufReader};
5-
use std::path::Path;
4+
use std::path::{Path, PathBuf};
65
extern crate bytecount;
76

8-
impl Count {
9-
#[allow(dead_code)]
10-
pub fn csv_run_lib(&self) -> CliResultData {
11-
let mut out = ResultData::new();
12-
13-
// current file
14-
let n = match self.path().is_dir() {
15-
true => count_dir_files(&self.path())?,
16-
false => count_file_lines(&self.path(), self.no_header)?,
17-
};
18-
19-
out.insert_header(vec!["count".to_string()]);
20-
out.insert_record(vec![n.to_string()]);
21-
22-
Ok(Some(out))
23-
}
7+
pub fn csv_count(file: &PathBuf, no_header: bool) -> CliResultData {
8+
// current file
9+
let n = match file.is_dir() {
10+
true => count_dir_files(&file)?,
11+
false => count_file_lines(&file, no_header)?,
12+
};
13+
14+
Ok(Some(ResultData {
15+
header: vec!["count".to_string()],
16+
data: vec![vec![n.to_string()]],
17+
}))
2418
}
2519

26-
#[allow(dead_code)]
2720
fn count_file_lines(path: &Path, no_header: bool) -> Result<usize, Box<dyn std::error::Error>> {
2821
// open file and count
2922
let mut n = 0;
@@ -49,7 +42,6 @@ fn count_file_lines(path: &Path, no_header: bool) -> Result<usize, Box<dyn std::
4942
Ok(n)
5043
}
5144

52-
#[allow(dead_code)]
5345
fn count_dir_files(path: &Path) -> Result<usize, Box<dyn std::error::Error>> {
5446
let mut file_n = 0;
5547
let mut dir_n = 0;

src/csv_lib/head.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
use crate::args::Head;
21
use crate::utils::return_result::{CliResultData, ResultData};
2+
use crate::utils::row_split::CsvRowSplitter;
33
use std::fs::File;
44
use std::io::{BufRead, BufReader};
5+
use std::path::PathBuf;
56

6-
impl Head {
7-
#[allow(dead_code)]
8-
pub fn csv_run_lib(&self) -> CliResultData {
9-
let mut out = ResultData::new();
10-
let path = self.path();
7+
pub fn csv_head(
8+
file: &PathBuf,
9+
no_header: bool,
10+
sep: char,
11+
quote: char,
12+
n: usize,
13+
) -> CliResultData {
14+
let mut out = ResultData::new();
1115

12-
// show head n
13-
let mut lines = BufReader::new(File::open(path)?)
14-
.lines()
15-
.take(self.n + 1 - self.no_header as usize);
16+
// show head n
17+
let mut lines = BufReader::new(File::open(file)?)
18+
.lines()
19+
.take(n + 1 - no_header as usize);
1620

17-
// Process header
18-
if let Some(header) = lines.next() {
19-
if let Ok(h) = header {
20-
let h = self.split_row_to_owned_vec(&h);
21-
out.insert_header(h);
22-
}
21+
// Process header
22+
if let Some(header) = lines.next() {
23+
if let Ok(h) = header {
24+
let h = CsvRowSplitter::new(&h, sep, quote).collect_owned();
25+
out.insert_header(h);
2326
}
27+
}
2428

25-
lines.for_each(|r| {
26-
if let Ok(r) = r {
27-
let r = self.split_row_to_owned_vec(&r);
28-
out.insert_record(r);
29-
}
30-
});
29+
lines.for_each(|r| {
30+
if let Ok(r) = r {
31+
let r = CsvRowSplitter::new(&r, sep, quote).collect_owned();
32+
out.insert_record(r);
33+
}
34+
});
3135

32-
Ok(Some(out))
33-
}
36+
Ok(Some(out))
3437
}

src/csv_lib/headers.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
use crate::args::Headers;
21
use crate::utils::return_result::{CliResultData, ResultData};
2+
use crate::utils::row_split::CsvRowSplitter;
33
use std::fs::File;
44
use std::io::{BufRead, BufReader};
5+
use std::path::PathBuf;
56

6-
impl Headers {
7-
#[allow(dead_code)]
8-
pub fn csv_run_lib(&self) -> CliResultData {
9-
let mut out = ResultData::new();
7+
pub fn csv_headers(file: &PathBuf, sep: char, quote: char) -> CliResultData {
8+
let mut out = ResultData::new();
109

11-
// open file and header
12-
let mut rdr = BufReader::new(File::open(self.path())?).lines();
10+
// open file and header
11+
let mut rdr = BufReader::new(File::open(&file)?).lines();
1312

14-
out.insert_header(vec!["column_name".to_string()]);
15-
if let Some(r) = rdr.next() {
16-
self.split(&r?)
17-
.for_each(|v| out.insert_record(vec![v.to_string()]));
18-
};
13+
out.insert_header(vec!["column_name".to_string()]);
14+
if let Some(r) = rdr.next() {
15+
CsvRowSplitter::new(&r?, sep, quote).for_each(|v| out.insert_record(vec![v.to_string()]));
16+
};
1917

20-
Ok(Some(out))
21-
}
18+
Ok(Some(out))
2219
}

src/csv_lib/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pub mod count;
22
pub mod head;
33
pub mod headers;
4-
pub mod size;

src/csv_lib/size.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/excel/sort.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::args::Sort;
22
use crate::utils::cli_result::CliResult;
33
use crate::utils::constants::COMMA;
4-
use crate::utils::excel::datatype_vec_to_string_vec;
4+
use crate::utils::excel::datatype_vec_to_str_vec;
55
use crate::utils::filename::new_path;
66
use crate::utils::reader::ExcelReader;
77
use crate::utils::sort::SortColumns;
@@ -29,7 +29,7 @@ impl Sort {
2929
let mut lines = range
3030
.iter()
3131
.skip(range.next_called)
32-
.map(datatype_vec_to_string_vec)
32+
.map(datatype_vec_to_str_vec)
3333
.collect::<Vec<_>>();
3434

3535
// sort

src/excel_lib/count.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::utils::reader::ExcelReader;
2+
use crate::utils::return_result::{CliResultData, ResultData};
3+
use std::path::PathBuf;
4+
extern crate bytecount;
5+
6+
pub fn excel_count(file: &PathBuf, no_header: bool, sheet: usize) -> CliResultData {
7+
// open file and count
8+
let range = ExcelReader::new(&file, sheet)?;
9+
let mut n = range.len();
10+
11+
// default to have a header
12+
if !no_header && n > 0 {
13+
n -= 1;
14+
}
15+
16+
Ok(Some(ResultData {
17+
header: vec!["count".to_string()],
18+
data: vec![vec![n.to_string()]],
19+
}))
20+
}

0 commit comments

Comments
 (0)