rsv_lib/io/
sort.rs

1use crate::args::Sort;
2use crate::utils::sort::SortColumns;
3use crate::utils::writer::Writer;
4use crate::utils::{cli_result::CliResult, filename::new_file};
5use std::io::{stdin, BufRead};
6
7impl Sort {
8    pub fn io_run(&self) -> CliResult {
9        // rdr and wtr
10        let mut rdr = stdin().lock().lines();
11        let out = new_file("sorted.csv");
12        let mut wtr = Writer::file_or_stdout(self.export, &out)?;
13
14        // cols
15        let cols = SortColumns::from(&self.cols)?;
16
17        // header
18        if !self.no_header {
19            let Some(r) = rdr.next() else { return Ok(()) };
20            wtr.write_str_unchecked(r?)
21        }
22
23        // lines
24        let lines = rdr.filter_map(|i| i.ok()).collect::<Vec<_>>();
25
26        // sort
27        cols.sort_and_write(&lines, self.sep, self.quote, &mut wtr)?;
28
29        if self.export {
30            println!("Saved to file: {}", out.display())
31        }
32
33        Ok(())
34    }
35}