Overhaul file erorr handling

This commit is contained in:
Joscha 2021-12-15 19:16:11 +00:00
parent 261c304268
commit c07d27aa13
4 changed files with 115 additions and 38 deletions

79
src/files/error.rs Normal file
View file

@ -0,0 +1,79 @@
use std::path::PathBuf;
use std::{io, result};
use super::parse;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Could not resolve {path}: {error}")]
ResolvePath { path: PathBuf, error: io::Error },
#[error("Could not load {file}: {error}")]
ReadFile { file: PathBuf, error: io::Error },
#[error("Could not write {file}: {error}")]
WriteFile { file: PathBuf, error: io::Error },
#[error("Could not resolve timezone {timezone}: {error}")]
ResolveTz { timezone: String, error: io::Error },
#[error("Could not determine local timezone: {error}")]
LocalTz { error: io::Error },
#[error("{0}")]
Parse(#[from] parse::Error),
#[error("{file1} has time zone {tz1} but {file2} has time zone {tz2}")]
TzConflict {
file1: PathBuf,
tz1: String,
file2: PathBuf,
tz2: String,
},
#[error("Not a task")]
NotATask(Vec<usize>),
}
impl Error {
pub fn print(self) {
match self {
Error::ResolvePath { path, error } => {
eprintln!("Could not resolve path {:?}:", path);
eprintln!(" {}", error);
}
Error::ReadFile { file, error } => {
eprintln!("Could not read file {:?}:", file);
eprintln!(" {}", error);
}
Error::WriteFile { file, error } => {
eprintln!("Could not write file {:?}:", file);
eprintln!(" {}", error);
}
Error::ResolveTz { timezone, error } => {
eprintln!("Could not resolve time zone {}:", timezone);
eprintln!(" {}", error);
}
Error::LocalTz { error } => {
eprintln!("Could not determine local timezone:");
eprintln!(" {}", error);
}
Error::Parse(error) => eprintln!("{}", error),
Error::TzConflict {
file1,
tz1,
file2,
tz2,
} => {
eprintln!("Time zone conflict:");
eprintln!(" {:?} has time zone {}", file1, tz1);
eprintln!(" {:?} has time zone {}", file2, tz2);
}
Error::NotATask(numbers) => {
if numbers.is_empty() {
eprintln!("Not a task.");
} else if numbers.len() == 1 {
eprintln!("{} is not a task.", numbers[0]);
} else {
let numbers = numbers.iter().map(|n| n.to_string()).collect::<Vec<_>>();
eprintln!("{} are not tasks.", numbers.join(", "));
}
}
}
}
}
pub type Result<T> = result::Result<T, Error>;