use std::fmt::Display;
use thiserror::Error;
use wildland_corex::catlib_service::error::{CatlibError, CatlibResult};
use wildland_corex::{
CryptoError,
ErrContext,
ForestIdentityCreationError,
ForestRetrievalError,
LssError,
};
use crate::api::cargo_user::AutomountError;
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[repr(C)]
pub enum CreateMnemonicError {
#[error("Invalid Mnemonic words")]
InvalidMnemonicWords,
}
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[repr(C)]
pub enum UserCreationError {
#[error("User already exists")]
UserAlreadyExists,
#[error("Could not check if user already exists: {0}")]
UserRetrievalError(UserRetrievalError),
#[error("{0}: Crypto error: {1}")]
CryptoError(String, CryptoError),
#[error("Could not create a new forest identity: {0}")]
ForestIdentityCreationError(#[from] ForestIdentityCreationError),
#[error("{0}: LSS error: {1}")]
LssError(String, LssError),
#[error("{0}: CatLib error: {1}")]
CatlibError(String, CatlibError),
}
impl<T> ErrContext<UserCreationError, T> for Result<T, CryptoError> {
fn context(self, ctx: impl Display) -> Result<T, UserCreationError> {
self.map_err(|e| UserCreationError::CryptoError(ctx.to_string(), e))
}
}
impl<T> ErrContext<UserCreationError, T> for Result<T, LssError> {
fn context(self, ctx: impl Display) -> Result<T, UserCreationError> {
self.map_err(|e| UserCreationError::LssError(ctx.to_string(), e))
}
}
impl<T> ErrContext<UserCreationError, T> for CatlibResult<T> {
fn context(self, ctx: impl Display) -> Result<T, UserCreationError> {
self.map_err(|e| UserCreationError::CatlibError(ctx.to_string(), e))
}
}
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[repr(C)]
pub enum UserRetrievalError {
#[error(transparent)]
ForestRetrievalError(#[from] ForestRetrievalError),
#[error("Default forest not found in LSS: {0}")]
ForestNotFound(String),
#[error(transparent)]
LssError(#[from] LssError),
#[error(transparent)]
CatlibError(#[from] CatlibError),
#[error("Metadata of this device has not been found in Forest")]
DeviceMetadataNotFound,
#[error("User not found")]
UserNotFound,
#[error("{0}: CargoUser inner error: {1}")]
AutomountError(String, AutomountError),
}
impl<T> ErrContext<UserRetrievalError, T> for Result<T, AutomountError> {
fn context(self, ctx: impl Display) -> Result<T, UserRetrievalError> {
self.map_err(|e| UserRetrievalError::AutomountError(ctx.to_string(), e))
}
}