lib

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

diagnostic.rs (2159B)


      1 use core::fmt;
      2 
      3 macro_rules! diagnostic {
      4     ($name:ident, $description:literal, {$($variant:ident => $value:literal),+ $(,)?}) => {
      5         #[doc = $description]
      6         #[derive(Clone, Copy, Debug, Eq, PartialEq)]
      7         #[non_exhaustive]
      8         pub enum $name {
      9             $(#[doc = $value] $variant,)+
     10         }
     11 
     12         impl $name {
     13             /// Every supported diagnostic value.
     14             pub const ALL: &'static [Self] = &[$(Self::$variant),+];
     15 
     16             /// Returns the fixed, input-free diagnostic string.
     17             pub const fn as_str(self) -> &'static str {
     18                 match self { $(Self::$variant => $value,)+ }
     19             }
     20         }
     21 
     22         impl fmt::Display for $name {
     23             fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
     24                 formatter.write_str(self.as_str())
     25             }
     26         }
     27     };
     28 }
     29 
     30 diagnostic!(Field, "A diagnostic field category, never a caller-supplied path.", {
     31     Input => "input",
     32     Currency => "currency",
     33     Unit => "unit",
     34     Source => "source",
     35     History => "history",
     36     Approval => "approval",
     37     Authority => "authority",
     38     Stock => "stock",
     39     Operation => "operation",
     40     Storage => "storage",
     41     Signer => "signer",
     42     Delivery => "delivery",
     43     Retention => "retention",
     44 });
     45 
     46 diagnostic!(Reason, "An input-free diagnostic reason.", {
     47     Missing => "missing",
     48     OutOfRange => "out_of_range",
     49     InvalidEncoding => "invalid_encoding",
     50     Unsupported => "unsupported",
     51     Conflict => "conflict",
     52     Unavailable => "unavailable",
     53     Unknown => "unknown",
     54 });
     55 
     56 /// Fixed-size redacted details with no arbitrary message, key or payload.
     57 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
     58 pub struct Detail {
     59     field: Field,
     60     reason: Reason,
     61 }
     62 
     63 impl Detail {
     64     /// Constructs one bounded diagnostic from supported categories.
     65     pub const fn new(field: Field, reason: Reason) -> Self {
     66         Self { field, reason }
     67     }
     68 
     69     /// Returns the field category.
     70     pub const fn field(self) -> Field {
     71         self.field
     72     }
     73 
     74     /// Returns the diagnostic reason.
     75     pub const fn reason(self) -> Reason {
     76         self.reason
     77     }
     78 }