52 lines
1.5 KiB
Rust
52 lines
1.5 KiB
Rust
use std::fmt::{Display, Formatter};
|
|
|
|
#[derive(Debug)]
|
|
pub enum GenericError {
|
|
ThingyLibError(thingy_lib::Error),
|
|
GPXError(gpx::errors::Error),
|
|
MessagedError(String, Option<Box<GenericError>>),
|
|
}
|
|
|
|
impl From<thingy_lib::Error> for GenericError {
|
|
fn from(error: thingy_lib::Error) -> Self {
|
|
GenericError::ThingyLibError(error)
|
|
}
|
|
}
|
|
|
|
impl From<gpx::errors::Error> for GenericError {
|
|
fn from(error: gpx::errors::Error) -> Self {
|
|
GenericError::GPXError(error)
|
|
}
|
|
}
|
|
|
|
impl Display for GenericError {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
let err = match self {
|
|
GenericError::ThingyLibError(e) => e.to_string(),
|
|
GenericError::GPXError(e) => format!("GPX error: {}", e),
|
|
GenericError::MessagedError(msg, err) => format!(
|
|
"{}\n {}",
|
|
msg,
|
|
err.as_ref().map(|e| e.to_string()).unwrap_or(String::new())
|
|
),
|
|
};
|
|
write!(f, "{}", err)
|
|
}
|
|
}
|
|
|
|
pub trait MessagedError<A> {
|
|
fn with_msg<T: Into<String>>(self, text: T) -> Result<A, GenericError>;
|
|
}
|
|
|
|
impl<A, B: Into<GenericError>> MessagedError<A> for Result<A, B> {
|
|
fn with_msg<T: Into<String>>(self, text: T) -> Result<A, GenericError> {
|
|
match self {
|
|
Ok(ok) => Ok(ok),
|
|
Err(e) => Err(GenericError::MessagedError(
|
|
text.into(),
|
|
Some(Box::new(e.into())),
|
|
)),
|
|
}
|
|
}
|
|
}
|