expense_tracker/
file_parser.rs

1use std::error::Error;
2use std::fs::OpenOptions;
3use std::io::Read;
4use std::path::{Path, PathBuf};
5
6use crate::config::FILE_NAME;
7
8pub fn read_file_content(file_path: Option<PathBuf>) -> Result<String, Box<dyn Error>> {
9    let path = file_path.unwrap_or(Path::new(FILE_NAME).to_path_buf());
10    let mut file = OpenOptions::new().read(true).open(&path)?;
11    // .unwrap_or_else(|err| {
12    //     if err.kind() == ErrorKind::NotFound {
13    //         eprintln!("No records yet at {}", path.display());
14    //         exit(0);
15    //     } else {
16    //         eprintln!("Error : {}", err);
17    //         exit(0);
18    //     }
19    // });
20
21    let mut content = String::from("");
22
23    file.read_to_string(&mut content)?;
24
25    Ok(content)
26}
27
28// #[cfg(test)]
29// mod test {
30//     use super::*;
31
32//     #[test]
33//     #[should_panic(expected="No records yet at ")]
34//     fn test_load_expenses_from_psv() {
35//         let _ = read_file_content(Some("./expense_db.csv"));
36//     }
37// }