Skip to content

Commit

Permalink
make file path related api's more flexible.
Browse files Browse the repository at this point in the history
 - Instead of taking file_path in form of `&PathBuf` -> take `impl AsRef<Path>` as file_path.
  • Loading branch information
KnowWhoami committed Dec 5, 2024
1 parent 5bfec4f commit 2e4f8e4
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/wallet/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
collections::HashMap,
fs::{self, File},
io::{BufReader, BufWriter},
path::PathBuf,
path::Path,
};

use super::{error::WalletError, fidelity::FidelityBond};
Expand Down Expand Up @@ -46,7 +46,7 @@ impl WalletStore {
/// Initialize a store at a path (if path already exists, it will overwrite it).
pub fn init(
file_name: String,
path: &PathBuf,
path: impl AsRef<Path>,
network: Network,
master_key: Xpriv,
wallet_birthday: Option<u64>,
Expand All @@ -65,8 +65,8 @@ impl WalletStore {
wallet_birthday,
};

std::fs::create_dir_all(path.parent().expect("Path should NOT be root!"))?;
// write: overwrites existing file.
std::fs::create_dir_all(path.as_ref().parent().expect("Path should NOT be root!"))?;
// write: overwrites existing. file.
// create: creates new file if doesn't exist.
let file = File::create(path)?;
let writer = BufWriter::new(file);
Expand All @@ -76,14 +76,14 @@ impl WalletStore {
}

/// Load existing file, updates it, writes it back (errors if path doesn't exist).
pub fn write_to_disk(&self, path: &PathBuf) -> Result<(), WalletError> {
pub fn write_to_disk(&self, path: impl AsRef<Path>) -> Result<(), WalletError> {
let wallet_file = fs::OpenOptions::new().write(true).open(path)?;
let writer = BufWriter::new(wallet_file);
Ok(serde_cbor::to_writer(writer, &self)?)
}

/// Reads from a path (errors if path doesn't exist).
pub fn read_from_disk(path: &PathBuf) -> Result<Self, WalletError> {
pub fn read_from_disk(path: impl AsRef<Path>) -> Result<Self, WalletError> {
let wallet_file = File::open(path)?;
let reader = BufReader::new(wallet_file);
let store: Self = serde_cbor::from_reader(reader)?;
Expand Down

0 comments on commit 2e4f8e4

Please sign in to comment.