From c3d5932a0ff0ef6f98cd834ff0106dc58d89cd46 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 7 Feb 2023 15:54:08 +0100 Subject: [PATCH] refactor(crypto): Move store::{CryptoStoreError, Result} into new module --- crates/matrix-sdk-crypto/src/store/error.rs | 80 +++++++++++++++ crates/matrix-sdk-crypto/src/store/mod.rs | 105 +++----------------- 2 files changed, 95 insertions(+), 90 deletions(-) create mode 100644 crates/matrix-sdk-crypto/src/store/error.rs diff --git a/crates/matrix-sdk-crypto/src/store/error.rs b/crates/matrix-sdk-crypto/src/store/error.rs new file mode 100644 index 000000000..0afa7ae35 --- /dev/null +++ b/crates/matrix-sdk-crypto/src/store/error.rs @@ -0,0 +1,80 @@ +use std::{fmt::Debug, io::Error as IoError}; + +use ruma::{IdParseError, OwnedDeviceId, OwnedUserId}; +use serde_json::Error as SerdeError; +use thiserror::Error; + +use crate::olm::SessionCreationError; + +/// A `CryptoStore` specific result type. +pub type Result = std::result::Result; + +/// The crypto store's error type. +#[derive(Debug, Error)] +pub enum CryptoStoreError { + /// The account that owns the sessions, group sessions, and devices wasn't + /// found. + #[error("can't save/load sessions or group sessions in the store before an account is stored")] + AccountUnset, + + /// The store doesn't support multiple accounts and data from another device + /// was discovered. + #[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 { + /// The expected user/device id pair. + expected: (OwnedUserId, OwnedDeviceId), + /// The user/device id pair that was loaded from the store. + got: (OwnedUserId, OwnedDeviceId), + }, + + /// An IO error occurred. + #[error(transparent)] + Io(#[from] IoError), + + /// Failed to decrypt an pickled object. + #[error("An object failed to be decrypted while unpickling")] + UnpicklingError, + + /// Failed to decrypt an pickled object. + #[error(transparent)] + Pickle(#[from] vodozemac::PickleError), + + /// The received room key couldn't be converted into a valid Megolm session. + #[error(transparent)] + SessionCreation(#[from] SessionCreationError), + + /// A Matrix identifier failed to be validated. + #[error(transparent)] + IdentifierValidation(#[from] IdParseError), + + /// The store failed to (de)serialize a data type. + #[error(transparent)] + Serialization(#[from] SerdeError), + + /// The database format has changed in a backwards incompatible way. + #[error( + "The database format changed in an incompatible way, current \ + version: {0}, latest version: {1}" + )] + UnsupportedDatabaseVersion(usize, usize), + + /// A problem with the underlying database backend + #[error(transparent)] + Backend(Box), +} + +impl CryptoStoreError { + /// Create a new [`Backend`][Self::Backend] error. + /// + /// Shorthand for `StoreError::Backend(Box::new(error))`. + #[inline] + pub fn backend(error: E) -> Self + where + E: std::error::Error + Send + Sync + 'static, + { + Self::Backend(Box::new(error)) + } +} diff --git a/crates/matrix-sdk-crypto/src/store/mod.rs b/crates/matrix-sdk-crypto/src/store/mod.rs index bf261895d..28e09f054 100644 --- a/crates/matrix-sdk-crypto/src/store/mod.rs +++ b/crates/matrix-sdk-crypto/src/store/mod.rs @@ -41,7 +41,6 @@ use std::{ collections::{HashMap, HashSet}, fmt::Debug, - io::Error as IoError, ops::Deref, sync::{atomic::AtomicBool, Arc}, }; @@ -49,28 +48,13 @@ use std::{ use atomic::Ordering; use dashmap::DashSet; use matrix_sdk_common::locks::Mutex; -use ruma::{ - events::secret::request::SecretName, DeviceId, IdParseError, OwnedDeviceId, OwnedUserId, UserId, -}; +use ruma::{events::secret::request::SecretName, DeviceId, OwnedDeviceId, OwnedUserId, UserId}; use serde::{Deserialize, Serialize}; -use serde_json::Error as SerdeError; use thiserror::Error; use tracing::{info, warn}; use vodozemac::{megolm::SessionOrdering, Curve25519PublicKey}; use zeroize::Zeroize; -pub mod caches; -mod memorystore; -mod traits; - -#[cfg(any(test, feature = "testing"))] -#[macro_use] -#[allow(missing_docs)] -pub mod integration_tests; - -pub use memorystore::MemoryStore; -pub use traits::{CryptoStore, IntoCryptoStore}; - use crate::{ identities::{ user::{OwnUserIdentity, UserIdentities, UserIdentity}, @@ -78,15 +62,26 @@ use crate::{ }, olm::{ InboundGroupSession, OlmMessageHash, OutboundGroupSession, PrivateCrossSigningIdentity, - ReadOnlyAccount, Session, SessionCreationError, + ReadOnlyAccount, Session, }, utilities::encode, verification::VerificationMachine, CrossSigningStatus, }; -/// A `CryptoStore` specific result type. -pub type Result = std::result::Result; +pub mod caches; +mod error; +mod memorystore; +mod traits; + +#[cfg(any(test, feature = "testing"))] +#[macro_use] +#[allow(missing_docs)] +pub mod integration_tests; + +pub use error::{CryptoStoreError, Result}; +pub use memorystore::MemoryStore; +pub use traits::{CryptoStore, IntoCryptoStore}; pub use crate::gossiping::{GossipRequest, SecretInfo}; @@ -709,73 +704,3 @@ impl Deref for Store { self.inner.deref() } } - -/// The crypto store's error type. -#[derive(Debug, Error)] -pub enum CryptoStoreError { - /// The account that owns the sessions, group sessions, and devices wasn't - /// found. - #[error("can't save/load sessions or group sessions in the store before an account is stored")] - AccountUnset, - - /// The store doesn't support multiple accounts and data from another device - /// was discovered. - #[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 { - /// The expected user/device id pair. - expected: (OwnedUserId, OwnedDeviceId), - /// The user/device id pair that was loaded from the store. - got: (OwnedUserId, OwnedDeviceId), - }, - - /// An IO error occurred. - #[error(transparent)] - Io(#[from] IoError), - - /// Failed to decrypt an pickled object. - #[error("An object failed to be decrypted while unpickling")] - UnpicklingError, - - /// Failed to decrypt an pickled object. - #[error(transparent)] - Pickle(#[from] vodozemac::PickleError), - - /// The received room key couldn't be converted into a valid Megolm session. - #[error(transparent)] - SessionCreation(#[from] SessionCreationError), - - /// A Matrix identifier failed to be validated. - #[error(transparent)] - IdentifierValidation(#[from] IdParseError), - - /// The store failed to (de)serialize a data type. - #[error(transparent)] - Serialization(#[from] SerdeError), - - /// The database format has changed in a backwards incompatible way. - #[error( - "The database format changed in an incompatible way, current \ - version: {0}, latest version: {1}" - )] - UnsupportedDatabaseVersion(usize, usize), - - /// A problem with the underlying database backend - #[error(transparent)] - Backend(Box), -} - -impl CryptoStoreError { - /// Create a new [`Backend`][Self::Backend] error. - /// - /// Shorthand for `StoreError::Backend(Box::new(error))`. - #[inline] - pub fn backend(error: E) -> Self - where - E: std::error::Error + Send + Sync + 'static, - { - Self::Backend(Box::new(error)) - } -}