state_store: use as many async fs functions as possible

master
Devin R 2020-04-26 17:27:06 -04:00
parent bb2d531525
commit 55de913e08
3 changed files with 11 additions and 11 deletions

View File

@ -63,6 +63,7 @@ async fn login_and_sync(
username: String, username: String,
password: String, password: String,
) -> Result<(), matrix_sdk::Error> { ) -> Result<(), matrix_sdk::Error> {
// the location for `JsonStore` to save files to
let mut home = dirs::home_dir().expect("no home directory found"); let mut home = dirs::home_dir().expect("no home directory found");
home.push("party_bot"); home.push("party_bot");
@ -87,7 +88,9 @@ async fn login_and_sync(
println!("logged in as {}", username); println!("logged in as {}", username);
// initial sync to set up state and so our bot doesn't respond to old messages // An initial sync to set up state and so our bot doesn't respond to old messages.
// If the `StateStore` finds saved state in the location given the initial sync will
// be skipped in favor of loading state from the store
client.sync(SyncSettings::default()).await.unwrap(); client.sync(SyncSettings::default()).await.unwrap();
// add our CommandBot to be notified of incoming messages, we do this after the initial // add our CommandBot to be notified of incoming messages, we do this after the initial
// sync to avoid responding to messages before the bot was running. // sync to avoid responding to messages before the bot was running.

View File

@ -30,7 +30,7 @@ impl JsonStore {
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> { pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
let p = path.as_ref(); let p = path.as_ref();
if !p.exists() { if !p.exists() {
std::fs::create_dir_all(p)?; fs::create_dir_all(p)?;
} }
Ok(Self { Ok(Self {
path: Arc::new(RwLock::new(p.to_path_buf())), path: Arc::new(RwLock::new(p.to_path_buf())),
@ -83,10 +83,7 @@ impl StateStore for JsonStore {
if !self.user_path_set.load(Ordering::SeqCst) { if !self.user_path_set.load(Ordering::SeqCst) {
if let Some(user) = &state.user_id { if let Some(user) = &state.user_id {
self.user_path_set.swap(true, Ordering::SeqCst); self.user_path_set.swap(true, Ordering::SeqCst);
self.path self.path.write().await.push(user.localpart())
.write()
.await
.push(format!("{}", user.localpart()))
} }
} }
let mut path = self.path.read().await.clone(); let mut path = self.path.read().await.clone();
@ -95,7 +92,7 @@ impl StateStore for JsonStore {
if !Path::new(&path).exists() { if !Path::new(&path).exists() {
let mut dir = path.clone(); let mut dir = path.clone();
dir.pop(); dir.pop();
std::fs::create_dir_all(dir)?; async_fs::create_dir_all(dir).await?;
} }
let json = serde_json::to_string(&state).map_err(Error::from)?; let json = serde_json::to_string(&state).map_err(Error::from)?;
@ -122,7 +119,7 @@ impl StateStore for JsonStore {
if !Path::new(&path).exists() { if !Path::new(&path).exists() {
let mut dir = path.clone(); let mut dir = path.clone();
dir.pop(); dir.pop();
std::fs::create_dir_all(dir)?; async_fs::create_dir_all(dir).await?;
} }
let json = serde_json::to_string(&room).map_err(Error::from)?; let json = serde_json::to_string(&room).map_err(Error::from)?;

View File

@ -13,7 +13,7 @@ use crate::events::{
EventJson, TryFromRaw, EventJson, TryFromRaw,
}; };
use crate::identifiers::{RoomId, UserId}; use crate::identifiers::{RoomId, UserId};
use crate::AsyncClient; use crate::{AsyncClient, Error, SyncSettings};
use mockito::{self, mock, Matcher, Mock}; use mockito::{self, mock, Matcher, Mock};
@ -399,11 +399,11 @@ impl MockTestRunner {
self self
} }
pub async fn to_client(&mut self) -> Result<&mut AsyncClient, crate::Error> { pub async fn to_client(&mut self) -> Result<&mut AsyncClient, Error> {
self.client self.client
.as_mut() .as_mut()
.unwrap() .unwrap()
.sync(crate::SyncSettings::default()) .sync(SyncSettings::default())
.await?; .await?;
Ok(self.client.as_mut().unwrap()) Ok(self.client.as_mut().unwrap())