diff --git a/Cargo.lock b/Cargo.lock index d7ad5f3d2..6d6fc1099 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3034,7 +3034,6 @@ dependencies = [ "async-stream", "async-trait", "dashmap", - "derive_builder", "fs_extra", "futures-core", "futures-util", diff --git a/crates/matrix-sdk-sled/Cargo.toml b/crates/matrix-sdk-sled/Cargo.toml index 0c65abbbf..8de085944 100644 --- a/crates/matrix-sdk-sled/Cargo.toml +++ b/crates/matrix-sdk-sled/Cargo.toml @@ -27,7 +27,6 @@ crypto-store = [ async-stream = { workspace = true } async-trait = { workspace = true } dashmap = { workspace = true } -derive_builder = "0.11.2" fs_extra = "1.2.0" futures-core = "0.3.21" futures-util = { version = "0.3.21", default-features = false } diff --git a/crates/matrix-sdk-sled/src/lib.rs b/crates/matrix-sdk-sled/src/lib.rs index 9eb38fbe8..8dc8e293d 100644 --- a/crates/matrix-sdk-sled/src/lib.rs +++ b/crates/matrix-sdk-sled/src/lib.rs @@ -63,12 +63,11 @@ pub async fn make_store_config( #[cfg(not(feature = "crypto-store"))] { - let mut store_builder = SledStateStore::builder(); - store_builder.path(path.as_ref().to_path_buf()); + let mut store_builder = SledStateStore::builder().path(path.as_ref().to_path_buf()); if let Some(passphrase) = passphrase { - store_builder.passphrase(passphrase.to_owned()); - }; + store_builder = store_builder.passphrase(passphrase.to_owned()); + } let state_store = store_builder.build().map_err(StoreError::backend)?; Ok(StoreConfig::new().state_store(state_store)) @@ -82,11 +81,9 @@ async fn open_stores_with_path( path: impl AsRef, passphrase: Option<&str>, ) -> Result<(SledStateStore, SledCryptoStore), OpenStoreError> { - let mut store_builder = SledStateStore::builder(); - store_builder.path(path.as_ref().to_path_buf()); - + let mut store_builder = SledStateStore::builder().path(path.as_ref().to_path_buf()); if let Some(passphrase) = passphrase { - store_builder.passphrase(passphrase.to_owned()); + store_builder = store_builder.passphrase(passphrase.to_owned()); } let state_store = store_builder.build().map_err(StoreError::backend)?; diff --git a/crates/matrix-sdk-sled/src/state_store.rs b/crates/matrix-sdk-sled/src/state_store.rs index f4dcd08d0..0347dc194 100644 --- a/crates/matrix-sdk-sled/src/state_store.rs +++ b/crates/matrix-sdk-sled/src/state_store.rs @@ -20,7 +20,6 @@ use std::{ }; use async_trait::async_trait; -use derive_builder::Builder; use futures_core::stream::Stream; use futures_util::stream::{self, StreamExt, TryStreamExt}; use matrix_sdk_base::{ @@ -177,25 +176,27 @@ enum DbOrPath { Path(PathBuf), } -#[derive(Builder, Debug)] -#[builder(name = "SledStateStoreBuilder", build_fn(skip))] -#[allow(dead_code)] -pub struct SledStateStoreBuilderConfig { - #[builder(setter(custom))] - db_or_path: DbOrPath, - /// Set the password the sled store is encrypted with (if any) - passphrase: String, - /// The strategy to use when a merge conflict is found, see - /// [`MigrationConflictStrategy`] for details - #[builder(default = "MigrationConflictStrategy::BackupAndDrop")] +/// Builder for [`SledStateStore`]. +#[derive(Debug)] +pub struct SledStateStoreBuilder { + db_or_path: Option, + passphrase: Option, migration_conflict_strategy: MigrationConflictStrategy, } impl SledStateStoreBuilder { + fn new() -> Self { + Self { + db_or_path: None, + passphrase: None, + migration_conflict_strategy: MigrationConflictStrategy::BackupAndDrop, + } + } + /// Path to the sled store files, created if not it doesn't exist yet. /// /// Mutually exclusive with [`db`][Self::db], whichever is called last wins. - pub fn path(&mut self, path: PathBuf) -> &mut SledStateStoreBuilder { + pub fn path(mut self, path: PathBuf) -> Self { self.db_or_path = Some(DbOrPath::Path(path)); self } @@ -204,11 +205,25 @@ impl SledStateStoreBuilder { /// /// Mutually exclusive with [`path`][Self::path], whichever is called last /// wins. - pub fn db(&mut self, db: Db) -> &mut SledStateStoreBuilder { + pub fn db(mut self, db: Db) -> Self { self.db_or_path = Some(DbOrPath::Db(db)); self } + /// Set the password the sled store is encrypted with (if any). + pub fn passphrase(mut self, value: String) -> Self { + self.passphrase = Some(value); + self + } + + /// Set the strategy to use when a merge conflict is found. + /// + /// See [`MigrationConflictStrategy`] for details. + pub fn migration_conflict_strategy(mut self, value: MigrationConflictStrategy) -> Self { + self.migration_conflict_strategy = value; + self + } + /// Create a [`SledStateStore`] with the options set on this builder. /// /// # Errors @@ -219,7 +234,7 @@ impl SledStateStoreBuilder { /// path. /// * Migration error: The migration to a newer version of the schema /// failed, see `SledStoreError::MigrationConflict`. - pub fn build(&mut self) -> Result { + pub fn build(self) -> Result { let (db, path) = match &self.db_or_path { None => { let db = Config::new().temporary(true).open().map_err(StoreError::backend)?; @@ -254,11 +269,7 @@ impl SledStateStoreBuilder { let migration_res = store.upgrade(); if let Err(SledStoreError::MigrationConflict { path, .. }) = &migration_res { // how are supposed to react about this? - match self - .migration_conflict_strategy - .as_ref() - .unwrap_or(&MigrationConflictStrategy::BackupAndDrop) - { + match self.migration_conflict_strategy { MigrationConflictStrategy::BackupAndDrop => { let mut new_path = path.clone(); new_path.set_extension(format!( @@ -397,9 +408,9 @@ impl SledStateStore { }) } - /// Generate a SledStateStoreBuilder with default parameters + /// Create a [`SledStateStoreBuilder`] with default parameters. pub fn builder() -> SledStateStoreBuilder { - SledStateStoreBuilder::default() + SledStateStoreBuilder::new() } fn drop_tables(self) -> StoreResult<()> {