rsv_lib/excel/
stats.rs

1use crate::args::Stats;
2use crate::utils::cli_result::CliResult;
3use crate::utils::column::Columns;
4use crate::utils::column_stats::ColumnStats;
5use crate::utils::column_type::ColumnTypes;
6use crate::utils::filename::new_path;
7use crate::utils::reader::ExcelReader;
8use std::fs::File;
9use std::io::{BufWriter, Write};
10
11impl Stats {
12    pub fn excel_run(&self) -> CliResult {
13        let path = &self.path();
14
15        // read file
16        let mut rdr = ExcelReader::new(path, self.sheet)?;
17
18        // Column type
19        let cols = Columns::new(&self.cols).total_col(rdr.column_n()).parse();
20        let col_type = ColumnTypes::guess_from_excel(&rdr, self.no_header, &cols).unwrap();
21
22        // header
23        let name = match self.no_header {
24            true => cols.artificial_n_cols(rdr.column_n()),
25            false => {
26                let Some(r) = rdr.next() else { return Ok(()) };
27                r.iter().map(|i| i.to_string()).collect::<Vec<_>>()
28            }
29        };
30
31        // stats holder
32        let mut stat = ColumnStats::new(&col_type, &name);
33
34        // read
35        rdr.iter()
36            .skip(rdr.next_called)
37            .for_each(|r| stat.parse_excel_row(r));
38
39        // refine result
40        stat.cal_unique_and_mean();
41
42        // print
43        if self.export {
44            let out = new_path(path, "-stats").with_extension("csv");
45            let mut wtr = BufWriter::new(File::create(&out)?);
46            wtr.write_all(stat.to_string().as_bytes())?;
47            println!("Saved to file: {}", out.display());
48        } else {
49            stat.print();
50        }
51
52        println!("Total rows: {}", stat.rows);
53
54        Ok(())
55    }
56}