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,
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");
@ -87,7 +88,9 @@ async fn login_and_sync(
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();
// 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.

View File

@ -30,7 +30,7 @@ impl JsonStore {
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
let p = path.as_ref();
if !p.exists() {
std::fs::create_dir_all(p)?;
fs::create_dir_all(p)?;
}
Ok(Self {
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 let Some(user) = &state.user_id {
self.user_path_set.swap(true, Ordering::SeqCst);
self.path
.write()
.await
.push(format!("{}", user.localpart()))
self.path.write().await.push(user.localpart())
}
}
let mut path = self.path.read().await.clone();
@ -95,7 +92,7 @@ impl StateStore for JsonStore {
if !Path::new(&path).exists() {
let mut dir = path.clone();
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)?;
@ -122,7 +119,7 @@ impl StateStore for JsonStore {
if !Path::new(&path).exists() {
let mut dir = path.clone();
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)?;

View File

@ -13,7 +13,7 @@ use crate::events::{
EventJson, TryFromRaw,
};
use crate::identifiers::{RoomId, UserId};
use crate::AsyncClient;
use crate::{AsyncClient, Error, SyncSettings};
use mockito::{self, mock, Matcher, Mock};
@ -399,11 +399,11 @@ impl MockTestRunner {
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
.as_mut()
.unwrap()
.sync(crate::SyncSettings::default())
.sync(SyncSettings::default())
.await?;
Ok(self.client.as_mut().unwrap())