store: Loading an account can fail because there is no account.

master
Damir Jelić 2020-03-18 14:29:01 +01:00
parent 114911f800
commit d7ab847b98
2 changed files with 15 additions and 9 deletions

View File

@ -34,6 +34,6 @@ pub type Result<T> = std::result::Result<T, CryptoStoreError>;
#[async_trait] #[async_trait]
pub trait CryptoStore { pub trait CryptoStore {
async fn load_account(&self) -> Result<Account>; async fn load_account(&self) -> Result<Option<Account>>;
async fn save_account(&self, account: Arc<Mutex<Account>>) -> Result<()>; async fn save_account(&self, account: Arc<Mutex<Account>>) -> Result<()>;
} }

View File

@ -100,23 +100,28 @@ impl SqliteStore {
#[async_trait] #[async_trait]
impl CryptoStore for SqliteStore { impl CryptoStore for SqliteStore {
async fn load_account(&self) -> Result<Account> { async fn load_account(&self) -> Result<Option<Account>> {
let mut connection = self.connection.lock().await; let mut connection = self.connection.lock().await;
let (pickle, shared): (String, bool) = query_as( let row: Option<(String, bool)> = query_as(
"SELECT pickle, shared FROM account "SELECT pickle, shared FROM account
WHERE user_id = ? and device_id = ?", WHERE user_id = ? and device_id = ?",
) )
.bind(&*self.user_id) .bind(&*self.user_id)
.bind(&*self.device_id) .bind(&*self.device_id)
.fetch_one(&mut *connection) .fetch_optional(&mut *connection)
.await?; .await?;
Ok(Account::from_pickle( let result = match row {
pickle, Some((pickle, shared)) => Some(Account::from_pickle(
self.get_pickle_mode(), pickle,
shared, self.get_pickle_mode(),
)?) shared,
)?),
None => None,
};
Ok(result)
} }
async fn save_account(&self, account: Arc<Mutex<Account>>) -> Result<()> { async fn save_account(&self, account: Arc<Mutex<Account>>) -> Result<()> {
@ -216,6 +221,7 @@ mod test {
let acc = account.lock().await; let acc = account.lock().await;
let loaded_account = store.load_account().await.expect("Can't load account"); let loaded_account = store.load_account().await.expect("Can't load account");
let loaded_account = loaded_account.unwrap();
assert_eq!(*acc, loaded_account); assert_eq!(*acc, loaded_account);
} }