use thiserror::Error;
pub type StorageResult<T> = Result<T, StorageError>;
#[derive(Debug, Error, uniffi::Error)]
pub enum StorageError {
#[error("keystore error: {0}")]
Keystore(String),
#[error("blob store error: {0}")]
BlobStore(String),
#[error("storage lock error: {0}")]
Lock(String),
#[error("serialization error: {0}")]
Serialization(String),
#[error("crypto error: {0}")]
Crypto(String),
#[error("invalid envelope: {0}")]
InvalidEnvelope(String),
#[error("unsupported envelope version: {0}")]
UnsupportedEnvelopeVersion(u32),
#[error("vault db error: {0}")]
VaultDb(String),
#[error("cache db error: {0}")]
CacheDb(String),
#[error("leaf index mismatch: expected {expected}, got {provided}")]
InvalidLeafIndex {
expected: u64,
provided: u64,
},
#[error("vault integrity check failed: {0}")]
CorruptedVault(String),
#[error("storage not initialized")]
NotInitialized,
#[error("nullifier already disclosed")]
NullifierAlreadyDisclosed,
#[error("credential not found")]
CredentialNotFound,
#[error("credential id not found: {credential_id}")]
CredentialIdNotFound {
credential_id: u64,
},
#[error("corrupted cache entry at {key_prefix}")]
CorruptedCacheEntry {
key_prefix: u8,
},
#[error("unexpected uniffi callback error: {0}")]
UnexpectedUniFFICallbackError(String),
}
impl From<uniffi::UnexpectedUniFFICallbackError> for StorageError {
fn from(error: uniffi::UnexpectedUniFFICallbackError) -> Self {
Self::UnexpectedUniFFICallbackError(error.reason)
}
}
impl From<walletkit_db::StoreError> for StorageError {
fn from(err: walletkit_db::StoreError) -> Self {
match err {
walletkit_db::StoreError::Keystore(s) => Self::Keystore(s),
walletkit_db::StoreError::BlobStore(s) => Self::BlobStore(s),
walletkit_db::StoreError::Lock(s) => Self::Lock(s),
walletkit_db::StoreError::Serialization(s) => Self::Serialization(s),
walletkit_db::StoreError::Crypto(s) => Self::Crypto(s),
walletkit_db::StoreError::InvalidEnvelope(s) => Self::InvalidEnvelope(s),
walletkit_db::StoreError::UnsupportedEnvelopeVersion(v) => {
Self::UnsupportedEnvelopeVersion(v)
}
walletkit_db::StoreError::Db(e) => Self::VaultDb(e.to_string()),
walletkit_db::StoreError::IntegrityCheckFailed(s) => {
Self::CorruptedVault(s)
}
}
}
}