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
use std::io::Read;
pub trait FileInterface: Clone + std::fmt::Debug + std::hash::Hash + PartialEq {
fn buffer(&self) -> String {
String::new()
}
fn open_file(&self) -> std::fs::File {
match std::fs::File::open(self.filepath()) {
Ok(file) => file,
Err(e) => panic!("File Error: File Not Found \n{}", e),
}
}
fn file_lines(&self) -> Vec<String> {
let mut buffer = self.buffer();
self.open_file()
.read_to_string(&mut buffer)
.expect("File Error");
buffer.split("\n").map(|s: &str| s.to_string()).collect()
}
fn filepath(&self) -> Box<std::path::Path>;
}
#[derive(Clone, Debug, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct FileExtractor {
pub filepath: String,
pub data: Vec<String>,
}
impl FileExtractor {
fn constructor(filepath: String, data: Vec<String>) -> Self {
Self { filepath, data }
}
pub fn new(path: String) -> Self {
Self::constructor(path, Vec::new())
}
pub fn from<T: std::string::ToString>(path: T) -> Self {
Self::new(path.to_string())
}
}
impl FileInterface for FileExtractor {
fn filepath(&self) -> Box<std::path::Path> {
Box::from(std::path::Path::new(self.filepath.as_str()))
}
}