diff --git a/crates/matrix-sdk-indexeddb/src/cryptostore.rs b/crates/matrix-sdk-indexeddb/src/cryptostore.rs index 3b96da8a0..ca7074d2e 100644 --- a/crates/matrix-sdk-indexeddb/src/cryptostore.rs +++ b/crates/matrix-sdk-indexeddb/src/cryptostore.rs @@ -928,6 +928,7 @@ impl IndexeddbStore { } } +#[cfg(target_arch = "wasm32")] #[async_trait(?Send)] impl CryptoStore for IndexeddbStore { async fn load_account(&self) -> Result, CryptoStoreError> { @@ -1072,7 +1073,7 @@ impl CryptoStore for IndexeddbStore { } } -#[cfg(test)] +#[cfg(all(test, target_arch = "wasm32"))] mod tests { use matrix_sdk_crypto::cryptostore_integration_tests; @@ -1093,7 +1094,7 @@ mod tests { cryptostore_integration_tests! { integration } } -#[cfg(test)] +#[cfg(all(test, target_arch = "wasm32"))] #[rustfmt::skip] mod encrypted_tests { use super::IndexeddbStore; @@ -1107,7 +1108,8 @@ mod encrypted_tests { .await .expect("Can't create a passphrase protected store") } -// FIXME: the tests pass, if run one by one, but run all together locally, -// as well as CI fails... see matrix-org/matrix-rust-sdk#661 -// cryptostore_integration_tests! { integration } + + // FIXME: the tests pass, if run one by one, but run all together locally, + // as well as CI fails... see matrix-org/matrix-rust-sdk#661 + // cryptostore_integration_tests! { integration } } diff --git a/crates/matrix-sdk-indexeddb/src/lib.rs b/crates/matrix-sdk-indexeddb/src/lib.rs index ffb31de69..f9380b84e 100644 --- a/crates/matrix-sdk-indexeddb/src/lib.rs +++ b/crates/matrix-sdk-indexeddb/src/lib.rs @@ -1,30 +1,22 @@ -#[cfg(target_arch = "wasm32")] +#![cfg_attr(not(target_arch = "wasm32"), allow(unused))] + use matrix_sdk_base::store::{StoreConfig, StoreError}; -#[cfg(target_arch = "wasm32")] use thiserror::Error; -mod safe_encode; - -#[cfg(target_arch = "wasm32")] -mod state_store; - -#[cfg(target_arch = "wasm32")] #[cfg(feature = "e2e-encryption")] mod cryptostore; +mod safe_encode; +mod state_store; -#[cfg(target_arch = "wasm32")] #[cfg(feature = "e2e-encryption")] pub use cryptostore::IndexeddbStore as CryptoStore; -#[cfg(target_arch = "wasm32")] #[cfg(feature = "e2e-encryption")] use cryptostore::IndexeddbStoreError; -#[cfg(target_arch = "wasm32")] pub use state_store::{IndexeddbStore as StateStore, IndexeddbStoreBuilder as StateStoreBuilder}; -#[cfg(target_arch = "wasm32")] -#[cfg(feature = "e2e-encryption")] /// Create a [`StateStore`] and a [`CryptoStore`] that use the same name and /// passphrase. +#[cfg(feature = "e2e-encryption")] async fn open_stores_with_name( name: impl Into, passphrase: Option<&str>, @@ -44,7 +36,6 @@ async fn open_stores_with_name( Ok((state_store, crypto_store)) } -#[cfg(target_arch = "wasm32")] /// Create a [`StoreConfig`] with an opened indexeddb [`StateStore`] that uses /// the given name and passphrase. If `encryption` is enabled, a [`CryptoStore`] /// with the same parameters is also opened. @@ -52,31 +43,36 @@ pub async fn make_store_config( name: impl Into, passphrase: Option<&str>, ) -> Result { - let name = name.into(); - - #[cfg(feature = "e2e-encryption")] + #[cfg(target_arch = "wasm32")] { - let (state_store, crypto_store) = open_stores_with_name(name, passphrase).await?; - Ok(StoreConfig::new().state_store(state_store).crypto_store(crypto_store)) - } + let name = name.into(); - #[cfg(not(feature = "e2e-encryption"))] - { - let mut builder = StateStore::builder(); - builder.name(name.clone()); - - if let Some(passphrase) = passphrase { - builder.passphrase(passphrase.to_owned()); + #[cfg(feature = "e2e-encryption")] + { + let (state_store, crypto_store) = open_stores_with_name(name, passphrase).await?; + Ok(StoreConfig::new().state_store(state_store).crypto_store(crypto_store)) } - let state_store = builder.build().await.map_err(StoreError::from)?; + #[cfg(not(feature = "e2e-encryption"))] + { + let mut builder = StateStore::builder(); + builder.name(name.clone()); - Ok(StoreConfig::new().state_store(state_store)) + if let Some(passphrase) = passphrase { + builder.passphrase(passphrase.to_owned()); + } + + let state_store = builder.build().await.map_err(StoreError::from)?; + + Ok(StoreConfig::new().state_store(state_store)) + } } + + #[cfg(not(target_arch = "wasm32"))] + panic!("the IndexedDB is only available on the 'wasm32' arch") } /// All the errors that can occur when opening an IndexedDB store. -#[cfg(target_arch = "wasm32")] #[derive(Error, Debug)] pub enum OpenStoreError { /// An error occurred with the state store implementation. diff --git a/crates/matrix-sdk-indexeddb/src/state_store.rs b/crates/matrix-sdk-indexeddb/src/state_store.rs index c6081fbaa..ff69fa667 100644 --- a/crates/matrix-sdk-indexeddb/src/state_store.rs +++ b/crates/matrix-sdk-indexeddb/src/state_store.rs @@ -1486,6 +1486,7 @@ impl IndexeddbStore { } } +#[cfg(target_arch = "wasm32")] #[async_trait(?Send)] impl StateStore for IndexeddbStore { async fn save_filter(&self, filter_name: &str, filter_id: &str) -> StoreResult<()> { @@ -1665,7 +1666,7 @@ struct TimelineMetadata { pub end_position: usize, } -#[cfg(test)] +#[cfg(all(test, target_arch = "wasm32"))] mod tests { #[cfg(target_arch = "wasm32")] wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); @@ -1683,7 +1684,7 @@ mod tests { statestore_integration_tests! { integration } } -#[cfg(test)] +#[cfg(all(test, target_arch = "wasm32"))] mod encrypted_tests { #[cfg(target_arch = "wasm32")] wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); @@ -1702,10 +1703,10 @@ mod encrypted_tests { statestore_integration_tests! { integration } } -#[cfg(test)] +#[cfg(all(test, target_arch = "wasm32"))] mod migration_tests { - #[cfg(target_arch = "wasm32")] wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + use indexed_db_futures::prelude::*; use matrix_sdk_test::async_test; use uuid::Uuid; @@ -1730,6 +1731,7 @@ mod migration_tests { db_req.into_future().await?; Ok(()) } + #[async_test] pub async fn test_no_upgrade() -> Result<()> { let name = format!("simple-1.1-no-cipher-{}", Uuid::new_v4().as_hyphenated().to_string());