Skip to content

Commit

Permalink
wallet - rename settings fns to have try_ prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Sep 15, 2023
1 parent ec50170 commit dee753b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion wallet/core/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ where
K: DefaultSettings + Clone + Serialize + DeserializeOwned + Send + Sync + 'static,
{
pub fn try_new(filename: &str) -> Result<Self> {
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 {
Expand Down
6 changes: 3 additions & 3 deletions wallet/core/src/storage/local/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -63,7 +63,7 @@ impl LocalStoreInner {

pub async fn try_load(ctx: &Arc<dyn AccessContextT>, folder: &str, args: OpenArgs) -> Result<Self> {
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?;
Expand Down Expand Up @@ -211,7 +211,7 @@ impl Interface for LocalStore {

async fn exists(&self, name: Option<&str>) -> Result<bool> {
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
}

Expand Down
24 changes: 20 additions & 4 deletions wallet/core/src/storage/local/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Storage> {
pub fn try_new(name: &str) -> Result<Storage> {
let filename = if runtime::is_web() {
PathBuf::from(name)
} else {
Expand All @@ -39,7 +39,7 @@ impl Storage {
Ok(Storage { filename })
}

pub fn new_with_folder(folder: &str, name: &str) -> Result<Storage> {
pub fn try_new_with_folder(folder: &str, name: &str) -> Result<Storage> {
let filename = if runtime::is_web() {
PathBuf::from(name)
} else {
Expand All @@ -63,6 +63,10 @@ impl Storage {
Ok(workflow_store::fs::exists(self.filename()).await?)
}

pub fn exists_sync(&self) -> Result<bool> {
Ok(workflow_store::fs::exists_sync(self.filename())?)
}

pub async fn ensure_dir(&self) -> Result<()> {
let file = self.filename();
if file.exists() {
Expand All @@ -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(())
}
}
2 changes: 1 addition & 1 deletion wallet/core/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit dee753b

Please sign in to comment.