From d7ab847b9826f9e82098965c29fdc7b3ab59c74a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 18 Mar 2020 14:29:01 +0100 Subject: [PATCH] store: Loading an account can fail because there is no account. --- src/crypto/store/mod.rs | 2 +- src/crypto/store/sqlite.rs | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/crypto/store/mod.rs b/src/crypto/store/mod.rs index e9d7fb91..3ac0fe51 100644 --- a/src/crypto/store/mod.rs +++ b/src/crypto/store/mod.rs @@ -34,6 +34,6 @@ pub type Result = std::result::Result; #[async_trait] pub trait CryptoStore { - async fn load_account(&self) -> Result; + async fn load_account(&self) -> Result>; async fn save_account(&self, account: Arc>) -> Result<()>; } diff --git a/src/crypto/store/sqlite.rs b/src/crypto/store/sqlite.rs index b411ec0f..06fa3219 100644 --- a/src/crypto/store/sqlite.rs +++ b/src/crypto/store/sqlite.rs @@ -100,23 +100,28 @@ impl SqliteStore { #[async_trait] impl CryptoStore for SqliteStore { - async fn load_account(&self) -> Result { + async fn load_account(&self) -> Result> { 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 WHERE user_id = ? and device_id = ?", ) .bind(&*self.user_id) .bind(&*self.device_id) - .fetch_one(&mut *connection) + .fetch_optional(&mut *connection) .await?; - Ok(Account::from_pickle( - pickle, - self.get_pickle_mode(), - shared, - )?) + let result = match row { + Some((pickle, shared)) => Some(Account::from_pickle( + pickle, + self.get_pickle_mode(), + shared, + )?), + None => None, + }; + + Ok(result) } async fn save_account(&self, account: Arc>) -> Result<()> { @@ -216,6 +221,7 @@ mod test { let acc = account.lock().await; let loaded_account = store.load_account().await.expect("Can't load account"); + let loaded_account = loaded_account.unwrap(); assert_eq!(*acc, loaded_account); }