From dee753b42cb1a25d2a596d7350b6a799412e349f Mon Sep 17 00:00:00 2001 From: Anton Yemelyanov Date: Fri, 15 Sep 2023 23:26:23 +0300 Subject: [PATCH] wallet - rename settings fns to have try_ prefixes --- wallet/core/src/settings.rs | 2 +- wallet/core/src/storage/local/interface.rs | 6 +++--- wallet/core/src/storage/local/storage.rs | 24 ++++++++++++++++++---- wallet/core/src/storage/mod.rs | 2 +- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/wallet/core/src/settings.rs b/wallet/core/src/settings.rs index 632814c91..4543d76d8 100644 --- a/wallet/core/src/settings.rs +++ b/wallet/core/src/settings.rs @@ -47,7 +47,7 @@ where K: DefaultSettings + Clone + Serialize + DeserializeOwned + Send + Sync + 'static, { pub fn try_new(filename: &str) -> Result { - Ok(Self { map: DashMap::default(), storage: Storage::new(&format!("{filename}.settings"))?, phantom: PhantomData }) + Ok(Self { map: DashMap::default(), storage: Storage::try_new(&format!("{filename}.settings"))?, phantom: PhantomData }) } pub fn new_with_storage(storage: Storage) -> Self { diff --git a/wallet/core/src/storage/local/interface.rs b/wallet/core/src/storage/local/interface.rs index 69436a7e1..a8accbe5d 100644 --- a/wallet/core/src/storage/local/interface.rs +++ b/wallet/core/src/storage/local/interface.rs @@ -41,7 +41,7 @@ impl LocalStoreInner { let filename = args.filename.clone().unwrap_or(super::DEFAULT_WALLET_FILE.to_string()); - let storage = Storage::new_with_folder(folder, &format!("{filename}.wallet"))?; + let storage = Storage::try_new_with_folder(folder, &format!("{filename}.wallet"))?; if storage.exists().await? && !args.overwrite_wallet { return Err(Error::WalletAlreadyExists); } @@ -63,7 +63,7 @@ impl LocalStoreInner { pub async fn try_load(ctx: &Arc, folder: &str, args: OpenArgs) -> Result { let filename = args.filename.unwrap_or(super::DEFAULT_WALLET_FILE.to_string()); - let storage = Storage::new_with_folder(folder, &format!("{filename}.wallet"))?; + let storage = Storage::try_new_with_folder(folder, &format!("{filename}.wallet"))?; let secret = ctx.wallet_secret().await; let wallet = Wallet::try_load(&storage).await?; @@ -211,7 +211,7 @@ impl Interface for LocalStore { async fn exists(&self, name: Option<&str>) -> Result { let location = self.location.lock().unwrap().clone().unwrap(); - let store = Storage::new_with_folder(&location.folder, name.unwrap_or(super::DEFAULT_WALLET_FILE))?; + let store = Storage::try_new_with_folder(&location.folder, name.unwrap_or(super::DEFAULT_WALLET_FILE))?; store.exists().await } diff --git a/wallet/core/src/storage/local/storage.rs b/wallet/core/src/storage/local/storage.rs index dc40ab384..0139238a1 100644 --- a/wallet/core/src/storage/local/storage.rs +++ b/wallet/core/src/storage/local/storage.rs @@ -21,14 +21,14 @@ impl Storage { impl Storage { pub fn default_wallet_store() -> Self { - Self::new(&format!("{}.wallet", super::DEFAULT_WALLET_FILE)).unwrap() + Self::try_new(&format!("{}.wallet", super::DEFAULT_WALLET_FILE)).unwrap() } pub fn default_settings_store() -> Self { - Self::new(&format!("{}.settings", super::DEFAULT_SETTINGS_FILE)).unwrap() + Self::try_new(&format!("{}.settings", super::DEFAULT_SETTINGS_FILE)).unwrap() } - pub fn new(name: &str) -> Result { + pub fn try_new(name: &str) -> Result { let filename = if runtime::is_web() { PathBuf::from(name) } else { @@ -39,7 +39,7 @@ impl Storage { Ok(Storage { filename }) } - pub fn new_with_folder(folder: &str, name: &str) -> Result { + pub fn try_new_with_folder(folder: &str, name: &str) -> Result { let filename = if runtime::is_web() { PathBuf::from(name) } else { @@ -63,6 +63,10 @@ impl Storage { Ok(workflow_store::fs::exists(self.filename()).await?) } + pub fn exists_sync(&self) -> Result { + Ok(workflow_store::fs::exists_sync(self.filename())?) + } + pub async fn ensure_dir(&self) -> Result<()> { let file = self.filename(); if file.exists() { @@ -74,4 +78,16 @@ impl Storage { } Ok(()) } + + pub fn ensure_dir_sync(&self) -> Result<()> { + let file = self.filename(); + if file.exists() { + return Ok(()); + } + + if let Some(dir) = file.parent() { + fs::create_dir_all_sync(dir)?; + } + Ok(()) + } } diff --git a/wallet/core/src/storage/mod.rs b/wallet/core/src/storage/mod.rs index 2faae1855..5a665e347 100644 --- a/wallet/core/src/storage/mod.rs +++ b/wallet/core/src/storage/mod.rs @@ -40,7 +40,7 @@ mod tests { // loading of account references and a wallet instance and confirms // that the serialized data is as expected. - let store = local::Storage::new("test-wallet-store")?; + let store = local::Storage::try_new("test-wallet-store")?; let mut payload = Payload::default();