1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum Error {
5 #[error("Generic Error: {0}")]
6 Generic(String),
7}
8
9impl From<String> for Error {
10 fn from(s: String) -> Self {
11 Error::Generic(s)
12 }
13}
14
15pub type Result<T> = std::result::Result<T, Error>;
16
17pub trait Context<T> {
18 fn context<S: Into<String>>(self, context: S) -> Result<T>;
19}
20
21impl<T> Context<T> for Option<T> {
22 fn context<S: Into<String>>(self, context: S) -> Result<T> {
23 match self {
24 Some(value) => Ok(value),
25 None => Err(Error::Generic(context.into())),
26 }
27 }
28}
29
30#[macro_export]
31macro_rules! bail {
32 ($($arg:tt)*) => {
33 return Err(Error::Generic(format!($($arg)*)))
34 };
35}