rsv_lib/excel/
sort.rs

1use crate::args::Sort;
2use crate::utils::cli_result::CliResult;
3use crate::utils::constants::COMMA;
4use crate::utils::excel::datatype_vec_to_string_vec;
5use crate::utils::filename::new_path;
6use crate::utils::reader::ExcelReader;
7use crate::utils::sort::SortColumns;
8use crate::utils::writer::Writer;
9
10impl Sort {
11    pub fn excel_run(&self) -> CliResult {
12        let path = &self.path();
13
14        // open file and count
15        let mut range = ExcelReader::new(path, self.sheet)?;
16        let out = new_path(path, "-sorted").with_extension("csv");
17        let mut wtr = Writer::file_or_stdout(self.export, &out)?;
18
19        // cols
20        let cols = SortColumns::from(&self.cols)?;
21
22        // header
23        if !self.no_header {
24            let Some(r) = range.next() else { return Ok(()) };
25            wtr.write_excel_line_unchecked(r, COMMA);
26        }
27
28        // lines
29        let mut lines = range
30            .iter()
31            .skip(range.next_called)
32            .map(datatype_vec_to_string_vec)
33            .collect::<Vec<_>>();
34
35        // sort
36        cols.sort_excel_and_write(&mut lines, &mut wtr)?;
37
38        if self.export {
39            println!("Saved to file: {}", out.display())
40        }
41
42        Ok(())
43    }
44}