state_store: WIP trait layout and default json impl

master
Devin R 2020-04-11 13:15:36 -04:00
parent 7c20c79f32
commit be34b72a0b
3 changed files with 81 additions and 0 deletions

View File

@ -39,6 +39,7 @@ mod error;
mod event_emitter; mod event_emitter;
mod models; mod models;
mod session; mod session;
mod state;
#[cfg(test)] #[cfg(test)]
pub mod test_builder; pub mod test_builder;
@ -50,5 +51,6 @@ pub use async_client::{AsyncClient, AsyncClientConfig, SyncSettings};
pub use base_client::Client; pub use base_client::Client;
pub use event_emitter::EventEmitter; pub use event_emitter::EventEmitter;
pub use models::Room; pub use models::Room;
pub use state::{StateStore, JsonStore};
pub(crate) const VERSION: &str = env!("CARGO_PKG_VERSION"); pub(crate) const VERSION: &str = env!("CARGO_PKG_VERSION");

67
src/state/mod.rs Normal file
View File

@ -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<StateEvent>) -> Result<()>;
fn save_room_events(&mut self, events: Vec<RoomEvent>) -> Result<()>;
fn save_non_room_events(&mut self, events: Vec<NonRoomEvent>) -> Result<()>;
}

12
src/state/state_store.rs Normal file
View File

@ -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 {
}