use std::fmt::{Display, Formatter}; #[derive(Debug)] pub enum GenericError { ThingyLibError(thingy_lib::Error), GPXError(gpx::errors::Error), MessagedError(String, Option>), } impl From for GenericError { fn from(error: thingy_lib::Error) -> Self { GenericError::ThingyLibError(error) } } impl From 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 { fn with_msg>(self, text: T) -> Result; } impl> MessagedError for Result { fn with_msg>(self, text: T) -> Result { match self { Ok(ok) => Ok(ok), Err(e) => Err(GenericError::MessagedError( text.into(), Some(Box::new(e.into())), )), } } }