lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

error.rs (3340B)


      1 use core::fmt;
      2 
      3 /// Crate-wide error for callers that prefer one Radroots-owned error type.
      4 ///
      5 /// Focused module APIs retain their narrower errors; each converts into this
      6 /// aggregate without exposing errors owned by implementation dependencies.
      7 #[non_exhaustive]
      8 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
      9 pub enum Error {
     10     Currency(crate::currency::ParseError),
     11     Decimal(crate::decimal::Error),
     12     Discount(crate::pricing::DiscountError),
     13     Money(crate::money::Error),
     14     Percent(crate::percent::ParseError),
     15     Pricing(crate::pricing::Error),
     16     Quantity(crate::quantity::Error),
     17     UnitParse(crate::unit::ParseError),
     18     UnitConversion(crate::unit::ConvertError),
     19 }
     20 
     21 impl fmt::Display for Error {
     22     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     23         match self {
     24             Self::Currency(error) => write!(f, "currency: {error}"),
     25             Self::Decimal(error) => write!(f, "decimal: {error}"),
     26             Self::Discount(error) => write!(f, "discount: {error}"),
     27             Self::Money(error) => write!(f, "money: {error}"),
     28             Self::Percent(error) => write!(f, "percent: {error}"),
     29             Self::Pricing(error) => write!(f, "pricing: {error}"),
     30             Self::Quantity(error) => write!(f, "quantity: {error}"),
     31             Self::UnitParse(error) => write!(f, "unit parse: {error}"),
     32             Self::UnitConversion(error) => write!(f, "unit conversion: {error}"),
     33         }
     34     }
     35 }
     36 
     37 #[cfg(feature = "std")]
     38 impl std::error::Error for Error {
     39     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
     40         match self {
     41             Self::Currency(error) => Some(error),
     42             Self::Decimal(error) => Some(error),
     43             Self::Discount(error) => Some(error),
     44             Self::Money(error) => Some(error),
     45             Self::Percent(error) => Some(error),
     46             Self::Pricing(error) => Some(error),
     47             Self::Quantity(error) => Some(error),
     48             Self::UnitParse(error) => Some(error),
     49             Self::UnitConversion(error) => Some(error),
     50         }
     51     }
     52 }
     53 
     54 impl From<crate::currency::ParseError> for Error {
     55     fn from(error: crate::currency::ParseError) -> Self {
     56         Self::Currency(error)
     57     }
     58 }
     59 
     60 impl From<crate::decimal::Error> for Error {
     61     fn from(error: crate::decimal::Error) -> Self {
     62         Self::Decimal(error)
     63     }
     64 }
     65 
     66 impl From<crate::discount::Error> for Error {
     67     fn from(error: crate::discount::Error) -> Self {
     68         Self::Discount(error)
     69     }
     70 }
     71 
     72 impl From<crate::money::Error> for Error {
     73     fn from(error: crate::money::Error) -> Self {
     74         Self::Money(error)
     75     }
     76 }
     77 
     78 impl From<crate::percent::ParseError> for Error {
     79     fn from(error: crate::percent::ParseError) -> Self {
     80         Self::Percent(error)
     81     }
     82 }
     83 
     84 impl From<crate::quantity_price::Error> for Error {
     85     fn from(error: crate::quantity_price::Error) -> Self {
     86         Self::Pricing(error)
     87     }
     88 }
     89 
     90 impl From<crate::quantity::Error> for Error {
     91     fn from(error: crate::quantity::Error) -> Self {
     92         Self::Quantity(error)
     93     }
     94 }
     95 
     96 impl From<crate::unit::ParseError> for Error {
     97     fn from(error: crate::unit::ParseError) -> Self {
     98         Self::UnitParse(error)
     99     }
    100 }
    101 
    102 impl From<crate::unit::ConvertError> for Error {
    103     fn from(error: crate::unit::ConvertError) -> Self {
    104         Self::UnitConversion(error)
    105     }
    106 }