use std::{convert::Infallible, fmt::Debug, io::Error as IoError};
use ruma::{IdParseError, OwnedDeviceId, OwnedUserId};
use serde_json::Error as SerdeError;
use thiserror::Error;
use crate::olm::SessionCreationError;
pub type Result<T, E = CryptoStoreError> = std::result::Result<T, E>;
#[derive(Debug, Error)]
pub enum CryptoStoreError {
#[error("can't save/load sessions or group sessions in the store before an account is stored")]
AccountUnset,
#[error(
"the account in the store doesn't match the account in the constructor: \
expected {}:{}, got {}:{}", .expected.0, .expected.1, .got.0, .got.1
)]
MismatchedAccount {
expected: (OwnedUserId, OwnedDeviceId),
got: (OwnedUserId, OwnedDeviceId),
},
#[error(transparent)]
Io(#[from] IoError),
#[error("An object failed to be decrypted while unpickling")]
UnpicklingError,
#[error(transparent)]
Pickle(#[from] vodozemac::PickleError),
#[error(transparent)]
SessionCreation(#[from] SessionCreationError),
#[error(transparent)]
IdentifierValidation(#[from] IdParseError),
#[error(transparent)]
Serialization(#[from] SerdeError),
#[error(
"The database format changed in an incompatible way, current \
version: {0}, latest version: {1}"
)]
UnsupportedDatabaseVersion(usize, usize),
#[error(transparent)]
Backend(Box<dyn std::error::Error + Send + Sync>),
#[error("invalid lock generation: {0}")]
InvalidLockGeneration(String),
}
impl CryptoStoreError {
#[inline]
pub fn backend<E>(error: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self::Backend(Box::new(error))
}
}
impl From<Infallible> for CryptoStoreError {
fn from(never: Infallible) -> Self {
match never {}
}
}