state_store: remove associated Store type

This commit is contained in:
Devin R 2020-04-21 09:47:36 -04:00
parent 267453de4d
commit ad7a18d50a
11 changed files with 37 additions and 44 deletions

View file

@ -15,11 +15,11 @@ struct CommandBot {
///
/// The type parameter is for the `StateStore` trait specifying the `Store`
/// type for state storage, here we don't care.
client: AsyncClient<()>,
client: AsyncClient,
}
impl CommandBot {
pub fn new(client: AsyncClient<()>) -> Self {
pub fn new(client: AsyncClient) -> Self {
Self { client }
}
}

View file

@ -45,8 +45,7 @@ async fn login(
.proxy("http://localhost:8080")?
.disable_ssl_verification();
let homeserver_url = Url::parse(&homeserver_url)?;
let mut client =
AsyncClient::<()>::new_with_config(homeserver_url, None, client_config).unwrap();
let mut client = AsyncClient::new_with_config(homeserver_url, None, client_config).unwrap();
client.add_event_emitter(Box::new(EventCallback)).await;

View file

@ -55,16 +55,16 @@ const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30);
/// An async/await enabled Matrix client.
///
/// All of the state is held in an `Arc` so the `AsyncClient` can be cloned freely.
pub struct AsyncClient<State> {
pub struct AsyncClient {
/// The URL of the homeserver to connect to.
homeserver: Url,
/// The underlying HTTP client.
http_client: reqwest::Client,
/// User session data.
pub(crate) base_client: Arc<RwLock<BaseClient<State>>>,
pub(crate) base_client: Arc<RwLock<BaseClient>>,
}
impl<State> std::fmt::Debug for AsyncClient<State> {
impl std::fmt::Debug for AsyncClient {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> StdResult<(), std::fmt::Error> {
write!(fmt, "AsyncClient {{ homeserver: {} }}", self.homeserver)
}
@ -197,7 +197,7 @@ use api::r0::room::create_room;
use api::r0::session::login;
use api::r0::sync::sync_events;
impl<State> AsyncClient<State> {
impl AsyncClient {
/// Creates a new client for making HTTP requests to the given homeserver.
///
/// # Arguments
@ -1119,7 +1119,7 @@ mod test {
device_id: "DEVICEID".to_owned(),
};
let homeserver = url::Url::parse(&mockito::server_url()).unwrap();
let client = AsyncClient::<()>::new(homeserver, Some(session)).unwrap();
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
let rid = RoomId::try_from("!roomid:room.com").unwrap();
let uid = UserId::try_from("@example:localhost").unwrap();
@ -1151,7 +1151,7 @@ mod test {
};
let homeserver = url::Url::parse(&mockito::server_url()).unwrap();
let client = AsyncClient::<()>::new(homeserver, Some(session)).unwrap();
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
let mut bld = EventBuilder::default()
.add_room_event_from_file("./tests/data/events/member.json", RoomEvent::RoomMember)
@ -1181,7 +1181,7 @@ mod test {
.with_body_from_file("tests/data/login_response_error.json")
.create();
let client = AsyncClient::<()>::new(homeserver, None).unwrap();
let client = AsyncClient::new(homeserver, None).unwrap();
if let Err(err) = client.login("example", "wordpass", None, None).await {
if let crate::Error::RumaResponse(ruma_api::error::FromHttpResponseError::Http(

View file

@ -61,7 +61,7 @@ pub type Token = String;
///
/// This Client is a state machine that receives responses and events and
/// accordingly updates it's state.
pub struct Client<S> {
pub struct Client {
/// The current client session containing our user id, device id and access
/// token.
pub session: Option<Session>,
@ -77,13 +77,13 @@ pub struct Client<S> {
/// events.
pub event_emitter: Option<Box<dyn EventEmitter>>,
///
pub state_store: Option<Box<dyn StateStore<Store = S>>>,
pub state_store: Option<Box<dyn StateStore>>,
#[cfg(feature = "encryption")]
olm: Arc<Mutex<Option<OlmMachine>>>,
}
impl<S> fmt::Debug for Client<S> {
impl fmt::Debug for Client {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Client")
.field("session", &self.session)
@ -96,7 +96,7 @@ impl<S> fmt::Debug for Client<S> {
}
}
impl<S> Client<S> {
impl Client {
/// Create a new client.
///
/// # Arguments
@ -815,7 +815,7 @@ mod test {
.with_body_from_file("tests/data/sync.json")
.create();
let client = AsyncClient::<()>::new(homeserver, Some(session)).unwrap();
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

View file

@ -238,7 +238,7 @@ mod test {
let vec = Arc::new(Mutex::new(Vec::new()));
let test_vec = Arc::clone(&vec);
let emitter = Box::new(EvEmitterTest(vec)) as Box<(dyn EventEmitter)>;
let mut client = AsyncClient::<()>::new(homeserver, Some(session)).unwrap();
let mut client = AsyncClient::new(homeserver, Some(session)).unwrap();
client.add_event_emitter(emitter).await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

View file

@ -439,7 +439,7 @@ mod test {
.with_body_from_file("tests/data/sync.json")
.create();
let client = AsyncClient::<()>::new(homeserver, Some(session)).unwrap();
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));

View file

@ -341,7 +341,7 @@ mod test {
.room_alias_name("room_alias")
.topic("room topic")
.visibility(Visibility::Private);
let cli = AsyncClient::<()>::new(homeserver, Some(session)).unwrap();
let cli = AsyncClient::new(homeserver, Some(session)).unwrap();
assert!(cli.create_room(builder).await.is_ok());
}
@ -373,7 +373,7 @@ mod test {
// TODO this makes ruma error `Err(IntoHttp(IntoHttpError(Query(Custom("unsupported value")))))`??
// .filter(RoomEventFilter::default());
let cli = AsyncClient::<()>::new(homeserver, Some(session)).unwrap();
let cli = AsyncClient::new(homeserver, Some(session)).unwrap();
assert!(cli.room_messages(builder).await.is_ok());
}
}

View file

@ -42,14 +42,10 @@ pub struct ClientState {
/// Abstraction around the data store to avoid unnecessary request on client initialization.
pub trait StateStore: Send + Sync {
/// The type of store to create. The default `JsonStore` uses `ClientState` as the store
/// to serialize and deserialize state to JSON files.
type Store;
/// Set up connections or open files to load/save state.
fn open(&self, path: &Path) -> Result<()>;
/// Loads the state of `BaseClient` through `StateStore::Store` type.
fn load_client_state(&self, path: &Path) -> Result<Self::Store>;
fn load_client_state(&self, path: &Path) -> Result<ClientState>;
/// Load the state of a single `Room` by `RoomId`.
fn load_room_state(&self, path: &Path, room_id: &RoomId) -> Result<Room>;
/// Load the state of all `Room`s.
@ -57,7 +53,7 @@ pub trait StateStore: Send + Sync {
/// This will be mapped over in the client in order to store `Room`s in an async safe way.
fn load_all_rooms(&self, path: &Path) -> Result<HashMap<RoomId, Room>>;
/// Save the current state of the `BaseClient` using the `StateStore::Store` type.
fn store_client_state(&self, path: &Path, _: Self::Store) -> Result<()>;
fn store_client_state(&self, path: &Path, _: ClientState) -> Result<()>;
/// Save the state a single `Room`.
fn store_room_state(&self, path: &Path, _: &Room) -> Result<()>;
}

View file

@ -11,8 +11,6 @@ use crate::{Error, Result, Room};
pub struct JsonStore;
impl StateStore for JsonStore {
type Store = ClientState;
fn open(&self, path: &Path) -> Result<()> {
if !path.exists() {
std::fs::create_dir_all(path)?;

View file

@ -49,9 +49,9 @@ pub struct RoomTestRunner {
state_events: Vec<StateEvent>,
}
pub struct ClientTestRunner<S> {
pub struct ClientTestRunner {
/// Used when testing the whole client
client: Option<AsyncClient<S>>,
client: Option<AsyncClient>,
/// RoomId and UserId to use for the events.
///
/// The RoomId must match the RoomId of the events to track.
@ -69,9 +69,9 @@ pub struct ClientTestRunner<S> {
}
#[allow(dead_code)]
pub struct MockTestRunner<S> {
pub struct MockTestRunner {
/// Used when testing the whole client
client: Option<AsyncClient<S>>,
client: Option<AsyncClient>,
/// The ephemeral room events that determine the state of a `Room`.
ephemeral: Vec<Event>,
/// The account data events that determine the state of a `Room`.
@ -169,11 +169,11 @@ impl EventBuilder {
///
/// The `TestRunner` streams the events to the client and holds methods to make assertions
/// about the state of the client.
pub fn build_mock_runner<S, P: Into<mockito::Matcher>>(
pub fn build_mock_runner<P: Into<mockito::Matcher>>(
mut self,
method: &str,
path: P,
) -> MockTestRunner<S> {
) -> MockTestRunner {
let body = serde_json::json! {
{
"device_one_time_keys_count": {},
@ -238,7 +238,7 @@ impl EventBuilder {
///
/// The `TestRunner` streams the events to the `AsyncClient` and holds methods to make assertions
/// about the state of the `AsyncClient`.
pub fn build_client_runner<S>(self, room_id: RoomId, user_id: UserId) -> ClientTestRunner<S> {
pub fn build_client_runner(self, room_id: RoomId, user_id: UserId) -> ClientTestRunner {
ClientTestRunner {
client: None,
room_user_id: (room_id, user_id),
@ -313,8 +313,8 @@ impl RoomTestRunner {
}
}
impl<S> ClientTestRunner<S> {
pub fn set_client(&mut self, client: AsyncClient<S>) -> &mut Self {
impl ClientTestRunner {
pub fn set_client(&mut self, client: AsyncClient) -> &mut Self {
self.client = Some(client);
self
}
@ -355,14 +355,14 @@ impl<S> ClientTestRunner<S> {
}
}
pub async fn to_client(&mut self) -> &mut AsyncClient<S> {
pub async fn to_client(&mut self) -> &mut AsyncClient {
self.stream_client_events().await;
self.client.as_mut().unwrap()
}
}
impl<S> MockTestRunner<S> {
pub fn set_client(&mut self, client: AsyncClient<S>) -> &mut Self {
impl MockTestRunner {
pub fn set_client(&mut self, client: AsyncClient) -> &mut Self {
self.client = Some(client);
self
}
@ -372,7 +372,7 @@ impl<S> MockTestRunner<S> {
self
}
pub async fn to_client(&mut self) -> Result<&mut AsyncClient<S>, crate::Error> {
pub async fn to_client(&mut self) -> Result<&mut AsyncClient, crate::Error> {
self.client
.as_mut()
.unwrap()

View file

@ -17,7 +17,7 @@ async fn login() {
.with_body_from_file("tests/data/login_response.json")
.create();
let client = AsyncClient::<()>::new(homeserver, None).unwrap();
let client = AsyncClient::new(homeserver, None).unwrap();
client
.login("example", "wordpass", None, None)
@ -46,7 +46,7 @@ async fn sync() {
.with_body_from_file("tests/data/sync.json")
.create();
let client = AsyncClient::<()>::new(homeserver, Some(session)).unwrap();
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
@ -75,7 +75,7 @@ async fn room_names() {
.with_body_from_file("tests/data/sync.json")
.create();
let client = AsyncClient::<()>::new(homeserver, Some(session)).unwrap();
let client = AsyncClient::new(homeserver, Some(session)).unwrap();
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));