lib

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

error.rs (3695B)


      1 use crate::{Admission, SignerRejection};
      2 
      3 /// Pure command-parser failures retained across the native boundary.
      4 #[derive(Clone, Copy, Debug, Eq, PartialEq, uniffi::Enum)]
      5 pub enum InputFailure {
      6     UnsupportedCommand,
      7     UnsupportedProfile,
      8     LimitExceeded,
      9 }
     10 
     11 /// Input-free failures; native details never become dependency/debug prose.
     12 #[derive(Clone, Debug, thiserror::Error, uniffi::Error)]
     13 pub enum Error {
     14     #[error("invalid {field}")]
     15     InvalidInput { field: String },
     16     #[error("unsupported or bounded {field}: {kind:?}")]
     17     CommandInput { field: String, kind: InputFailure },
     18     #[error("operation {code}")]
     19     Operation {
     20         code: String,
     21         retry: String,
     22         operation_id: Option<String>,
     23         admission: Admission,
     24         signer_rejection: Option<Box<SignerRejection>>,
     25     },
     26     #[error("unsupported boundary state {axis}")]
     27     UnsupportedState {
     28         axis: String,
     29         operation_id: Option<String>,
     30         signer_rejection: Option<Box<SignerRejection>>,
     31     },
     32 }
     33 const _: () = assert!(core::mem::size_of::<Error>() <= 128);
     34 impl Error {
     35     pub(crate) fn command(field: &'static str, error: radroots::CommandError) -> Self {
     36         let kind = match error {
     37             radroots::CommandError::InvalidInput => return Self::input(field),
     38             radroots::CommandError::UnsupportedCommand => InputFailure::UnsupportedCommand,
     39             radroots::CommandError::UnsupportedProfile => InputFailure::UnsupportedProfile,
     40             radroots::CommandError::LimitExceeded => InputFailure::LimitExceeded,
     41             _ => {
     42                 return Self::UnsupportedState {
     43                     axis: "command_input".to_owned(),
     44                     operation_id: None,
     45                     signer_rejection: None,
     46                 };
     47             }
     48         };
     49         Self::CommandInput {
     50             field: field.to_owned(),
     51             kind,
     52         }
     53     }
     54     pub(crate) fn input(field: &'static str) -> Self {
     55         Self::InvalidInput {
     56             field: field.to_owned(),
     57         }
     58     }
     59     pub(crate) fn with_operation(self, id: String) -> Self {
     60         match self {
     61             Self::UnsupportedState {
     62                 axis,
     63                 signer_rejection,
     64                 ..
     65             } => Self::UnsupportedState {
     66                 axis,
     67                 operation_id: Some(id),
     68                 signer_rejection,
     69             },
     70             value => value,
     71         }
     72     }
     73     fn operation(value: radroots::Error, signer_rejection: Option<Box<SignerRejection>>) -> Self {
     74         let operation_id = value.context().operation_id().map(|v| v.to_string());
     75         match Admission::try_from(value.context().admission()) {
     76             Ok(admission) => Self::Operation {
     77                 code: value.code().as_str().to_owned(),
     78                 retry: value.retry().as_str().to_owned(),
     79                 operation_id,
     80                 admission,
     81                 signer_rejection,
     82             },
     83             Err(Self::UnsupportedState { axis, .. }) => Self::UnsupportedState {
     84                 axis,
     85                 operation_id,
     86                 signer_rejection,
     87             },
     88             Err(_) => Self::UnsupportedState {
     89                 axis: "admission".to_owned(),
     90                 operation_id,
     91                 signer_rejection,
     92             },
     93         }
     94     }
     95 }
     96 impl From<radroots::Error> for Error {
     97     fn from(value: radroots::Error) -> Self {
     98         Self::operation(value, None)
     99     }
    100 }
    101 impl From<radroots::ExecutionFailure> for Error {
    102     fn from(value: radroots::ExecutionFailure) -> Self {
    103         Self::operation(
    104             value.error(),
    105             value.signer_rejection().map(|v| Box::new(v.into())),
    106         )
    107     }
    108 }