From 54c8b4f8bb2bbc897f9cb55eb5b8ffb12689df0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Thu, 10 Mar 2022 14:00:32 +0100 Subject: [PATCH] sdk-base: Remove BaseClientConfig --- crates/matrix-sdk-base/src/client.rs | 93 ++++++-------------------- crates/matrix-sdk-base/src/lib.rs | 2 +- crates/matrix-sdk/src/client.rs | 2 +- crates/matrix-sdk/src/config/client.rs | 13 ++-- 4 files changed, 26 insertions(+), 84 deletions(-) diff --git a/crates/matrix-sdk-base/src/client.rs b/crates/matrix-sdk-base/src/client.rs index c2f6c452c..5c2d5a8d9 100644 --- a/crates/matrix-sdk-base/src/client.rs +++ b/crates/matrix-sdk-base/src/client.rs @@ -13,15 +13,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -#[allow(unused_imports)] -#[cfg(feature = "encryption")] -use std::ops::Deref; use std::{ collections::{BTreeMap, BTreeSet}, convert::TryFrom, fmt, sync::Arc, }; +#[allow(unused_imports)] +#[cfg(feature = "encryption")] +use std::{ops::Deref, result::Result as StdResult}; #[cfg(feature = "encryption")] use matrix_sdk_common::locks::Mutex; @@ -69,8 +69,7 @@ use crate::{ rooms::{Room, RoomInfo, RoomType}, session::Session, store::{ - ambiguity_map::AmbiguityCache, Result as StoreResult, StateChanges, StateStore, Store, - StoreConfig, + ambiguity_map::AmbiguityCache, Result as StoreResult, StateChanges, Store, StoreConfig, }, }; @@ -103,62 +102,6 @@ impl fmt::Debug for BaseClient { } } -/// Configuration for the creation of the `BaseClient`. -/// -/// # Example -/// -/// ``` -/// # use matrix_sdk_base::BaseClientConfig; -/// -/// let client_config = BaseClientConfig::new(); -/// ``` -#[derive(Default)] -pub struct BaseClientConfig { - store_config: StoreConfig, -} - -#[cfg(not(tarpaulin_include))] -impl std::fmt::Debug for BaseClientConfig { - fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - fmt.debug_struct("BaseClientConfig").finish() - } -} - -impl BaseClientConfig { - /// Create a new default `BaseClientConfig`. - #[must_use] - pub fn new() -> Self { - Default::default() - } - - /// Create a new `BaseClientConfig` with the given `StoreConfig`. - #[must_use] - pub fn with_store_config(store_config: StoreConfig) -> Self { - Self { store_config } - } - - /// Get the `StoreConfig` used by this `BaseClientConfig`. - #[cfg(feature = "encryption")] - pub fn get_store_config(&self) -> &StoreConfig { - &self.store_config - } - - /// Set a custom implementation of a `CryptoStore`. - /// - /// The crypto store should be opened before being set. - #[cfg(feature = "encryption")] - pub fn crypto_store(mut self, store: Box) -> Self { - self.store_config = self.store_config.crypto_store(store); - self - } - - /// Set a custom implementation of a `StateStore`. - pub fn state_store(mut self, store: Box) -> Self { - self.store_config = self.store_config.state_store(store); - self - } -} - #[cfg(feature = "encryption")] enum CryptoHolder { PreSetupStore(Option>), @@ -210,29 +153,25 @@ impl BaseClient { /// /// * `config` - An optional session if the user already has one from a /// previous login call. - pub async fn new_with_config(config: BaseClientConfig) -> Result { - let store = config - .store_config - .state_store - .map(Store::new) - .unwrap_or_else(Store::open_memory_store); + pub fn new_with_store_config(config: StoreConfig) -> Self { + let store = config.state_store.map(Store::new).unwrap_or_else(Store::open_memory_store); #[cfg(feature = "encryption")] - let holder = config.store_config.crypto_store.map(CryptoHolder::new).unwrap_or_default(); + let holder = config.crypto_store.map(CryptoHolder::new).unwrap_or_default(); - Ok(BaseClient { + BaseClient { session: store.session.clone(), sync_token: store.sync_token.clone(), store, #[cfg(feature = "encryption")] olm: Mutex::new(holder).into(), - }) + } } } impl BaseClient { /// Create a new default client. - pub async fn new() -> Result { - BaseClient::new_with_config(BaseClientConfig::default()).await + pub fn new() -> Self { + BaseClient::new_with_store_config(StoreConfig::default()) } /// The current client session containing our user id, device id and access /// token. @@ -1122,7 +1061,7 @@ impl BaseClient { /// # use futures::executor::block_on; /// # let alice = user_id!("@alice:example.org").to_owned(); /// # block_on(async { - /// # let client = BaseClient::new().await.unwrap(); + /// # let client = BaseClient::new(); /// let device = client.get_device(&alice, device_id!("DEVICEID")).await; /// /// println!("{:?}", device); @@ -1176,7 +1115,7 @@ impl BaseClient { /// # use futures::executor::block_on; /// # let alice = user_id!("@alice:example.org"); /// # block_on(async { - /// # let client = BaseClient::new().await.unwrap(); + /// # let client = BaseClient::new(); /// let devices = client.get_user_devices(alice).await.unwrap(); /// /// for device in devices.devices() { @@ -1324,5 +1263,11 @@ impl BaseClient { } } +impl Default for BaseClient { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] mod test {} diff --git a/crates/matrix-sdk-base/src/lib.rs b/crates/matrix-sdk-base/src/lib.rs index 428a1c558..c02ca6af3 100644 --- a/crates/matrix-sdk-base/src/lib.rs +++ b/crates/matrix-sdk-base/src/lib.rs @@ -33,7 +33,7 @@ mod session; pub mod store; mod timeline_stream; -pub use client::{BaseClient, BaseClientConfig}; +pub use client::BaseClient; #[cfg(any(test, feature = "testing"))] pub use http; #[cfg(feature = "encryption")] diff --git a/crates/matrix-sdk/src/client.rs b/crates/matrix-sdk/src/client.rs index 33888dc54..d036a47af 100644 --- a/crates/matrix-sdk/src/client.rs +++ b/crates/matrix-sdk/src/client.rs @@ -183,7 +183,7 @@ impl Client { Arc::new(client_with_config(&config)?) }; - let base_client = BaseClient::new_with_config(config.base_config).await?; + let base_client = BaseClient::new_with_store_config(config.store_config); let session = base_client.session().clone(); let http_client = diff --git a/crates/matrix-sdk/src/config/client.rs b/crates/matrix-sdk/src/config/client.rs index dae650f2b..a67b03ad9 100644 --- a/crates/matrix-sdk/src/config/client.rs +++ b/crates/matrix-sdk/src/config/client.rs @@ -20,7 +20,7 @@ use std::{ }; use http::header::InvalidHeaderValue; -use matrix_sdk_base::{BaseClientConfig, StateStore}; +use matrix_sdk_base::StateStore; use crate::{ config::{RequestConfig, StoreConfig}, @@ -75,7 +75,7 @@ pub struct ClientConfig { pub(crate) proxy: Option, pub(crate) user_agent: Option, pub(crate) disable_ssl_verification: bool, - pub(crate) base_config: BaseClientConfig, + pub(crate) store_config: StoreConfig, pub(crate) request_config: RequestConfig, pub(crate) client: Option>, pub(crate) appservice_mode: bool, @@ -131,10 +131,7 @@ impl ClientConfig { /// [`make_config`]: crate::store::make_config /// [`store`]: crate::store pub fn with_store_config(store_config: StoreConfig) -> Self { - Self { - base_config: BaseClientConfig::with_store_config(store_config), - ..Default::default() - } + Self { store_config, ..Default::default() } } /// Set the proxy through which all the HTTP requests should go. @@ -180,7 +177,7 @@ impl ClientConfig { /// /// The state store should be opened before being set. pub fn state_store(mut self, store: Box) -> Self { - self.base_config = self.base_config.state_store(store); + self.store_config = self.store_config.state_store(store); self } @@ -226,7 +223,7 @@ impl ClientConfig { mut self, store: Box, ) -> Self { - self.base_config = self.base_config.crypto_store(store); + self.store_config = self.store_config.crypto_store(store); self }