stree 0.1.0

A directory hierarchy mapping format, similar to mtree
Documentation
//! Error definition for the crate

use std::fmt::{self, Debug, Display};
use std::io;

pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;

impl std::error::Error for Error {}

unsafe impl Send for Error {}
unsafe impl Sync for Error {}

/// Represents an error from the stree crate
pub enum Error
{
    /// Mainly for errors generated by this crate
    General(String),
    /// I/O errors
    Io(io::Error),
    /// Generic well-implemented errors
    External(Box<dyn std::error::Error + Send + Sync>)
}

impl Display for Error
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
    {
        match self
        {
            Self::General(msg) => write!(f, "{msg}"),
            Self::Io(e) => write!(f, "{e}"),
            Self::External(e) => write!(f, "{e}")
        }
    }
}

impl Debug for Error
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
    {
        write!(f, "{self}")
    }
}

impl From<io::Error> for Error
{
    fn from(e: io::Error) -> Self
    {
        Self::io(e)
    }
}

impl From<pathstr::Error> for Error
{
    fn from(e: pathstr::Error) -> Self
    {
        Self::external(Box::new(e))
    }
}

impl From<rhash_rs::Error> for Error
{
    fn from(e: rhash_rs::Error) -> Self
    {
        Self::external(Box::new(e))
    }
}

impl From<std::num::ParseIntError> for Error
{
    fn from(e: std::num::ParseIntError) -> Self
    {
        Self::external(Box::new(e))
    }
}

impl From<Box<dyn std::error::Error + Send + Sync>> for Error
{
    fn from(e: Box<dyn std::error::Error + Send + Sync>) -> Self
    {
        Self::external(e)
    }
}

impl Error
{
    /// Construct a general error from a string
    pub fn new<S>(msg: S) -> Self
    where S: Into<String>
    {
        Self::General(msg.into())
    }

    /// Construct an error from an io error
    pub fn io(e: io::Error) -> Self
    {
        Self::Io(e)
    }
    
    /// Construct an error from a well-implemented generic error
    pub fn external(e: Box<dyn std::error::Error + Send + Sync>) -> Self
    {
        Self::External(e)
    }
}

impl From<walkdir::Error> for Error
{
    fn from(e: walkdir::Error) -> Self
    {
        Self::external(Box::new(e))
    }
}

macro_rules! error
{
    ($msg:literal $(, $($arg:tt)*)?) =>
    {
        return Err($crate::error::Error::new(::std::format!($msg $(, $($arg)*)?)))
    };
    ($msg:expr) =>
    {
        return Err($crate::error::Error::new(::std::format!("{}", $msg)))
    };
}

macro_rules! err
{
    ($msg:literal $(, $($arg:tt)*)?) =>
    {
        $crate::error::Error::new(::std::format!($msg $(, $($arg)*)?))
    };
    ($msg:expr) =>
    {
        $crate::error::Error::new(::std::format!("{}", $msg))
    };
}

pub(crate) use {err, error};