sdk: Remove default store constructors

This commit is contained in:
Kévin Commaille
2022-03-09 12:26:34 +01:00
parent 4d41f94199
commit 28a83da421
5 changed files with 43 additions and 108 deletions

View File

@@ -45,12 +45,22 @@ async fn login_and_sync(
username: &str,
password: &str,
) -> Result<(), matrix_sdk::Error> {
let mut home = dirs::home_dir().expect("no home directory found");
home.push("autojoin_bot");
let mut client_config = ClientConfig::new();
let client_config =
ClientConfig::with_named_store(home.to_str().expect("home dir path must be utf-8"), None)
.await?;
#[cfg(feature = "sled_state_store")]
{
// The location to save files to
let mut home = dirs::home_dir().expect("no home directory found");
home.push("autojoin_bot");
let state_store = matrix_sdk_sled::StateStore::open_with_path(home)?;
client_config = client_config.state_store(Box::new(state_store));
}
#[cfg(feature = "indexeddb_stores")]
{
let state_store = matrix_sdk_indexeddb::StateStore::open();
client_config = client_config.state_store(Box::new(state_store));
}
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
let client = Client::new_with_config(homeserver_url, client_config).await.unwrap();

View File

@@ -37,13 +37,22 @@ async fn login_and_sync(
username: String,
password: String,
) -> Result<(), matrix_sdk::Error> {
// the location for `JsonStore` to save files to
let mut home = dirs::home_dir().expect("no home directory found");
home.push("party_bot");
let mut client_config = ClientConfig::new();
let client_config =
ClientConfig::with_named_store(home.to_str().expect("home dir path must be utf-8"), None)
.await?;
#[cfg(feature = "sled_state_store")]
{
// The location to save files to
let mut home = dirs::home_dir().expect("no home directory found");
home.push("party_bot");
let state_store = matrix_sdk_sled::StateStore::open_with_path(home)?;
client_config = client_config.state_store(Box::new(state_store));
}
#[cfg(feature = "indexeddb_stores")]
{
let state_store = matrix_sdk_indexeddb::StateStore::open();
client_config = client_config.state_store(Box::new(state_store));
}
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
// create a new Client with the given homeserver url and config

View File

@@ -162,7 +162,7 @@ impl Client {
///
/// * `homeserver_url` - The homeserver that the client should connect to.
pub async fn new(homeserver_url: Url) -> Result<Self> {
let config = ClientConfig::new().await?;
let config = ClientConfig::new();
Client::new_with_config(homeserver_url, config).await
}
@@ -241,7 +241,7 @@ impl Client {
///
/// [spec]: https://spec.matrix.org/unstable/client-server-api/#well-known-uri
pub async fn new_from_user_id(user_id: &UserId) -> Result<Self> {
let config = ClientConfig::new().await?;
let config = ClientConfig::new();
Client::new_from_user_id_with_config(user_id, config).await
}
@@ -2385,8 +2385,7 @@ pub(crate) mod test {
device_id: device_id!("DEVICEID").to_owned(),
};
let homeserver = url::Url::parse(&mockito::server_url()).unwrap();
let config =
ClientConfig::new().await.unwrap().request_config(RequestConfig::new().disable_retry());
let config = ClientConfig::new().request_config(RequestConfig::new().disable_retry());
let client = Client::new_with_config(homeserver, config).await.unwrap();
client.restore_login(session).await.unwrap();
@@ -2484,7 +2483,7 @@ pub(crate) mod test {
#[async_test]
async fn login_with_discovery() {
let homeserver = Url::from_str(&mockito::server_url()).unwrap();
let config = ClientConfig::new().await.unwrap().use_discovery_response();
let config = ClientConfig::new().use_discovery_response();
let client = Client::new_with_config(homeserver, config).await.unwrap();
@@ -2504,7 +2503,7 @@ pub(crate) mod test {
#[async_test]
async fn login_no_discovery() {
let homeserver = Url::from_str(&mockito::server_url()).unwrap();
let config = ClientConfig::new().await.unwrap().use_discovery_response();
let config = ClientConfig::new().use_discovery_response();
let client = Client::new_with_config(homeserver.clone(), config).await.unwrap();
@@ -2633,12 +2632,8 @@ pub(crate) mod test {
let room = client.get_joined_room(room_id);
assert!(room.is_some());
// test store reloads with correct room state from the sled store
let path = tempfile::tempdir().unwrap();
let config = ClientConfig::with_named_store(path.into_path().to_str().unwrap(), None)
.await
.unwrap()
.request_config(RequestConfig::new().disable_retry());
// test store reloads with correct room state from the state store
let config = ClientConfig::new().request_config(RequestConfig::new().disable_retry());
let joined_client = Client::new_with_config(homeserver, config).await.unwrap();
joined_client.restore_login(session).await.unwrap();

View File

@@ -37,7 +37,7 @@ use crate::{config::RequestConfig, HttpSend, Result};
/// // verification
///
/// # futures::executor::block_on(async {
/// let client_config = ClientConfig::new().await?
/// let client_config = ClientConfig::new()
/// .proxy("http://localhost:8080")?
/// .disable_ssl_verification();
/// # matrix_sdk::Result::<()>::Ok(())
@@ -61,7 +61,7 @@ use crate::{config::RequestConfig, HttpSend, Result};
/// .user_agent("MyApp/v3.0");
///
/// # futures::executor::block_on(async {
/// let client_config = ClientConfig::new().await?
/// let client_config = ClientConfig::new()
/// .client(Arc::new(builder.build()?));
/// # matrix_sdk::Result::<()>::Ok(())
/// # });
@@ -94,89 +94,10 @@ impl Debug for ClientConfig {
}
}
#[cfg(feature = "sled_state_store")]
mod store_helpers {
use matrix_sdk_sled::StateStore;
use super::Result;
/// Build the sled Store with the default settings - as a temporary storage
pub async fn default_store() -> Result<Box<StateStore>> {
Ok(Box::new(StateStore::open()?))
}
/// Build a sled store at `name` (being a relative or full path), and open
/// the store with the given passphrase (if given) for encryption
pub async fn default_store_with_name(
name: &str,
passphrase: Option<&str>,
) -> Result<Box<StateStore>> {
Ok(Box::new(match passphrase {
Some(pass) => StateStore::open_with_passphrase(name, pass)?,
_ => StateStore::open_with_path(&name)?,
}))
}
}
#[cfg(feature = "indexeddb_stores")]
mod store_helpers {
use matrix_sdk_indexeddb::StateStore;
use super::Result;
/// Open the IndexedDB store with the default name, unencrypted
pub async fn default_store() -> Result<Box<StateStore>> {
Ok(Box::new(StateStore::open().await?))
}
/// Open the indexeddb store at `name` (IndexedDB Database name), and open
/// the store with the given passphrase (if given) for encryption
pub async fn default_store_with_name(
name: &str,
passphrase: Option<&str>,
) -> Result<Box<StateStore>> {
Ok(Box::new(match passphrase {
Some(pass) => StateStore::open_with_passphrase(name.to_owned(), pass).await?,
_ => StateStore::open_with_name(name.to_owned()).await?,
}))
}
}
#[cfg(not(any(feature = "indexeddb_stores", feature = "sled_state_store")))]
mod store_helpers {
use matrix_sdk_base::store::MemoryStore as StateStore;
use super::Result;
/// Open a new in-memory StateStore
pub async fn default_store() -> Result<Box<StateStore>> {
Ok(Box::new(StateStore::new()))
}
/// Alias for `default_store` - in Memory Stores are never named
pub async fn default_store_with_name(
_name: &str,
_passphrase: Option<&str>,
) -> Result<Box<StateStore>> {
Ok(Box::new(StateStore::new()))
}
}
pub use store_helpers::{default_store, default_store_with_name};
impl ClientConfig {
/// Create a new default `ClientConfig`.
pub async fn new() -> Result<Self> {
let mut d = Self::default();
d.base_config = d.base_config.state_store(default_store().await?);
Ok(d)
}
/// Create a new ClientConfig with a named state store, encrypted with the
/// given passphrase (if any)
pub async fn with_named_store(name: &str, passphrase: Option<&str>) -> Result<Self> {
let mut d = Self::default();
d.base_config = d.base_config.state_store(default_store_with_name(name, passphrase).await?);
Ok(d)
pub fn new() -> Self {
Self::default()
}
/// Set the proxy through which all the HTTP requests should go.
@@ -193,7 +114,7 @@ impl ClientConfig {
/// # futures::executor::block_on(async {
/// use matrix_sdk::{Client, config::ClientConfig};
///
/// let client_config = ClientConfig::new().await?
/// let client_config = ClientConfig::new()
/// .proxy("http://localhost:8080")?;
///
/// # Result::<_, matrix_sdk::Error>::Ok(())

View File

@@ -20,6 +20,6 @@ mod client;
mod request;
mod sync;
pub use client::{default_store, default_store_with_name, ClientConfig};
pub use client::ClientConfig;
pub use request::RequestConfig;
pub use sync::SyncSettings;