spcsv/
file_manager.rs

1extern crate fs_extra;
2
3use indexed_file::Indexable;
4use std::path::Path;
5
6pub struct File {
7    pub file_path: Box<String>,
8    file: indexed_file::File,
9    file_fs: Box<Path>,
10    header_line: bool,
11}
12
13impl File {
14    pub fn header(&self) -> usize {
15        if self.header_line == true {
16            1
17        } else {
18            0
19        }
20    }
21
22    pub fn lines(&self) -> usize {
23        self.file.total_lines()
24    }
25
26    pub fn name(&self) -> String {
27        String::from(self.file_fs.file_name().unwrap().to_str().unwrap())
28    }
29
30    pub fn new(file_path: &str, header_line: bool) -> Option<File> {
31        let file = if let Ok(file_path) = indexed_file::File::open_raw(file_path) {
32            file_path
33        } else {
34            eprintln!("No such file found");
35            return None;
36        };
37
38        let f = Box::new(file_path.to_string());
39
40        Some(File {
41            file_path: f,
42            file,
43            file_fs: Box::from(Path::new(file_path)),
44            header_line,
45        })
46    }
47
48    pub fn base_name(&self) -> Option<String> {
49        let name = self.name().to_lowercase();
50
51        if name.to_lowercase().contains(".csv") {
52            Some(name.replace(".csv", ""))
53        } else {
54            None
55        }
56    }
57}