use std::{
error::Error,
fmt::{self, Display},
};
use thiserror::Error;
pub type MicrosandboxUtilsResult<T> = Result<T, MicrosandboxUtilsError>;
#[derive(pretty_error_debug::Debug, Error)]
pub enum MicrosandboxUtilsError {
#[error("path validation error: {0}")]
PathValidation(String),
#[error("file not found at: {0}\nSource: {1}")]
FileNotFound(String, String),
#[error("io error: {0}")]
IoError(#[from] std::io::Error),
#[error("runtime error: {0}")]
Runtime(String),
#[error("nix error: {0}")]
NixError(#[from] nix::Error),
#[error("Custom error: {0}")]
Custom(#[from] AnyError),
}
#[derive(Debug)]
pub struct AnyError {
error: anyhow::Error,
}
impl MicrosandboxUtilsError {
pub fn custom(error: impl Into<anyhow::Error>) -> MicrosandboxUtilsError {
MicrosandboxUtilsError::Custom(AnyError {
error: error.into(),
})
}
}
impl AnyError {
pub fn downcast<T>(&self) -> Option<&T>
where
T: Display + fmt::Debug + Send + Sync + 'static,
{
self.error.downcast_ref::<T>()
}
}
#[allow(non_snake_case)]
pub fn Ok<T>(value: T) -> MicrosandboxUtilsResult<T> {
Result::Ok(value)
}
impl PartialEq for AnyError {
fn eq(&self, other: &Self) -> bool {
self.error.to_string() == other.error.to_string()
}
}
impl Display for AnyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.error)
}
}
impl Error for AnyError {}