1- use crate :: args:: Count ;
21use crate :: utils:: return_result:: { CliResultData , ResultData } ;
32use std:: fs:: File ;
43use std:: io:: { BufRead , BufReader } ;
5- use std:: path:: Path ;
4+ use std:: path:: { Path , PathBuf } ;
65extern 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) ]
2720fn 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) ]
5345fn count_dir_files ( path : & Path ) -> Result < usize , Box < dyn std:: error:: Error > > {
5446 let mut file_n = 0 ;
5547 let mut dir_n = 0 ;
0 commit comments