1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
extern crate fs_extra;

use indexed_file::Indexable;
use std::path::Path;

pub struct File {
    pub file_path: Box<String>,
    file: indexed_file::File,
    file_fs: Box<Path>,
    header_line: bool,
}

impl File {
    pub fn header(&self) -> usize {
        if self.header_line == true {
            1
        } else {
            0
        }
    }

    pub fn lines(&self) -> usize {
        self.file.total_lines()
    }

    pub fn name(&self) -> String {
        String::from(self.file_fs.file_name().unwrap().to_str().unwrap())
    }

    pub fn new(file_path: &str, header_line: bool) -> Option<File> {
        let file = if let Ok(file_path) = indexed_file::File::open_raw(file_path) {
            file_path
        } else {
            eprintln!("No such file found");
            return None;
        };

        let f = Box::new(file_path.to_string());

        Some(File {
            file_path: f,
            file,
            file_fs: Box::from(Path::new(file_path)),
            header_line,
        })
    }

    pub fn base_name(&self) -> Option<String> {
        let name = self.name().to_lowercase();

        if name.to_lowercase().contains(".csv") {
            Some(name.replace(".csv", ""))
        } else {
            None
        }
    }
}