rsv_lib/csv/
estimate.rs

1use crate::args::Estimate;
2use crate::utils::cli_result::CliResult;
3use std::fs::File;
4use std::io::{BufRead, BufReader};
5
6impl Estimate {
7    pub fn csv_run(&self) -> CliResult {
8        // read 20000 lines to estimate bytes per line
9        let file = File::open(self.path())?;
10        let filesize = file.metadata()?.len() as f64;
11
12        let mut total_bytes = 0;
13        let mut n = 0;
14        for l in BufReader::new(file).lines().skip(1) {
15            total_bytes += l.unwrap().len() + 1;
16            n += 1;
17            if n > 20000 {
18                break;
19            }
20        }
21
22        // estimate line count
23        let mut estimate_n = filesize / ((total_bytes as f64) / (n as f64));
24
25        // default to have a header
26        if estimate_n > 1.0 {
27            estimate_n -= 1.0;
28        }
29
30        println!("{}", estimate_n as usize);
31
32        Ok(())
33    }
34}