Skip to content
Snippets Groups Projects
Commit a3ef08d6 authored by Blinningjr's avatar Blinningjr
Browse files

Implemented better filepath error message

parent 7235ace1
No related branches found
No related tags found
No related merge requests found
...@@ -16,17 +16,20 @@ pub fn write_to_file(contents: String, file: PathBuf) -> std::io::Result<()> { ...@@ -16,17 +16,20 @@ pub fn write_to_file(contents: String, file: PathBuf) -> std::io::Result<()> {
/* /*
* Load the tasks from a json file `file`. * Load the tasks from a json file `file`.
*/ */
pub fn load_tasks(file: PathBuf) -> Tasks { pub fn load_tasks(file: PathBuf) -> Result<Tasks, String> {
let mut serialized = String::new(); let mut serialized = String::new();
read_from_file(&mut serialized, file); match read_from_file(&mut serialized, &file) {
return serde_json::from_str(&serialized).unwrap(); Ok(_) => (),
Err(_) => return Err(format!("Error: Invalid filepath {:?}", file)),
};
return Ok(serde_json::from_str(&serialized).unwrap());
} }
/* /*
* Read string from file `file` and store it in `contents`. * Read string from file `file` and store it in `contents`.
*/ */
pub fn read_from_file(contents: &mut String, file: PathBuf) -> std::io::Result<()> { pub fn read_from_file(contents: &mut String, file: &PathBuf) -> std::io::Result<()> {
let mut file = File::open(file)?; let mut file = File::open(file)?;
file.read_to_string(contents)?; file.read_to_string(contents)?;
Ok(()) Ok(())
......
...@@ -38,7 +38,13 @@ fn main() { ...@@ -38,7 +38,13 @@ fn main() {
//println!("{:?}", opt); //println!("{:?}", opt);
let tasks: Tasks = match opt.in_file { let tasks: Tasks = match opt.in_file {
Some(file) => load_tasks(file), Some(file) => match load_tasks(file) {
Ok(ts) => ts,
Err(err) => {
println!("{}", err);
return;
},
},
None => create_tasks(), None => create_tasks(),
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment