From be34b72a0ba9678349a207252d8546edf6d650fd Mon Sep 17 00:00:00 2001 From: Devin R Date: Sat, 11 Apr 2020 13:15:36 -0400 Subject: [PATCH] state_store: WIP trait layout and default json impl --- src/lib.rs | 2 ++ src/state/mod.rs | 67 ++++++++++++++++++++++++++++++++++++++++ src/state/state_store.rs | 12 +++++++ 3 files changed, 81 insertions(+) create mode 100644 src/state/mod.rs create mode 100644 src/state/state_store.rs diff --git a/src/lib.rs b/src/lib.rs index 28bf9375..f7dfe7ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,7 @@ mod error; mod event_emitter; mod models; mod session; +mod state; #[cfg(test)] pub mod test_builder; @@ -50,5 +51,6 @@ pub use async_client::{AsyncClient, AsyncClientConfig, SyncSettings}; pub use base_client::Client; pub use event_emitter::EventEmitter; pub use models::Room; +pub use state::{StateStore, JsonStore}; pub(crate) const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/src/state/mod.rs b/src/state/mod.rs new file mode 100644 index 00000000..f315bc25 --- /dev/null +++ b/src/state/mod.rs @@ -0,0 +1,67 @@ +// Copyright 2020 Damir Jelić +// Copyright 2020 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub mod state_store; +pub use state_store::JsonStore; + +use crate::api; +use crate::events; +use api::r0::message::create_message_event; +use api::r0::session::login; +use api::r0::sync::sync_events; +use events::collections::all::{Event as NonRoomEvent, RoomEvent, StateEvent}; + +use std::collections::HashMap; +use std::convert::{TryFrom, TryInto}; +use std::result::Result as StdResult; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +use uuid::Uuid; + +use futures::future::Future; +use tokio::sync::RwLock; +use tokio::time::delay_for as sleep; +#[cfg(feature = "encryption")] +use tracing::debug; +use tracing::{info, instrument, trace}; + +use http::Method as HttpMethod; +use http::Response as HttpResponse; +use reqwest::header::{HeaderValue, InvalidHeaderValue}; +use url::Url; + +use ruma_api::{Endpoint, Outgoing}; +use ruma_events::room::message::MessageEventContent; +use ruma_events::EventResult; +pub use ruma_events::EventType; +use ruma_identifiers::RoomId; + +use crate::base_client::Client as BaseClient; +use crate::models::Room; +use crate::session::Session; +use crate::VERSION; +use crate::{Error, EventEmitter, Result}; +/// Abstraction around the data store to avoid unnecessary request on client initialization. +/// +pub trait StateStore { + fn load_state(&self) -> sync_events::IncomingResponse; + + fn save_state_events(&mut self, events: Vec) -> Result<()>; + fn save_room_events(&mut self, events: Vec) -> Result<()>; + fn save_non_room_events(&mut self, events: Vec) -> Result<()>; + + +} diff --git a/src/state/state_store.rs b/src/state/state_store.rs new file mode 100644 index 00000000..4f1f4d02 --- /dev/null +++ b/src/state/state_store.rs @@ -0,0 +1,12 @@ +use super::StateStore; + +/// A default `StateStore` implementation that serializes state as json +/// and saves it to disk. +pub struct JsonStore { + +} + +impl StateStore for JsonStore { + +} +