Skip to content

Commit

Permalink
refactor: swap position of tuple values returned by Store::load
Browse files Browse the repository at this point in the history
  • Loading branch information
nymius committed Dec 23, 2024
1 parent 91b5cdb commit 3b695ce
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
18 changes: 9 additions & 9 deletions crates/file_store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
///
/// [`create`]: Store::create
/// [`load`]: Store::load
pub fn load<P>(magic: &[u8], file_path: P) -> Result<(Option<C>, Self), StoreErrorWithDump<C>>
pub fn load<P>(magic: &[u8], file_path: P) -> Result<(Self, Option<C>), StoreErrorWithDump<C>>
where
P: AsRef<Path>,
{
Expand All @@ -92,7 +92,7 @@ where
// Get aggregated changeset
let aggregated_changeset = store.dump()?;

Ok((aggregated_changeset, store))
Ok((store, aggregated_changeset))
}

/// Aggregate [`Store`] changesets and return them as a single changeset.
Expand Down Expand Up @@ -133,15 +133,15 @@ where
pub fn load_or_create<P>(
magic: &[u8],
file_path: P,
) -> Result<(Option<C>, Self), StoreErrorWithDump<C>>
) -> Result<(Self, Option<C>), StoreErrorWithDump<C>>
where
P: AsRef<Path>,
{
if file_path.as_ref().exists() {
Self::load(magic, file_path)
} else {
Self::create(magic, file_path)
.map(|store| (Option::<C>::None, store))
.map(|store| (store, Option::<C>::None))
.map_err(|err: StoreError| StoreErrorWithDump {
changeset: Option::<C>::None,
error: err,
Expand Down Expand Up @@ -371,15 +371,15 @@ mod test {
let changeset = BTreeSet::from(["hello".to_string(), "world".to_string()]);

{
let (_, mut store) =
let (mut store, _) =
Store::<TestChangeSet>::load_or_create(&TEST_MAGIC_BYTES, &file_path)
.expect("must create");
assert!(file_path.exists());
store.append(&changeset).expect("must succeed");
}

{
let (recovered_changeset, _) =
let (_, recovered_changeset) =
Store::<TestChangeSet>::load_or_create(&TEST_MAGIC_BYTES, &file_path)
.expect("must load");
assert_eq!(recovered_changeset, Some(changeset));
Expand Down Expand Up @@ -444,7 +444,7 @@ mod test {

// load file again - this time we should successfully aggregate all changesets
{
let (aggregated_changeset, _) =
let (_, aggregated_changeset) =
Store::<TestChangeSet>::load(&TEST_MAGIC_BYTES, &file_path).unwrap();
assert_eq!(
aggregated_changeset,
Expand Down Expand Up @@ -480,7 +480,7 @@ mod test {

{
// open store
let (_, mut store) = Store::<TestChangeSet>::load(&TEST_MAGIC_BYTES, &file_path)
let (mut store, _) = Store::<TestChangeSet>::load(&TEST_MAGIC_BYTES, &file_path)
.expect("failed to load store");

// now append the second changeset
Expand All @@ -502,7 +502,7 @@ mod test {
}

// Open the store again to verify file pointer position at the end of the file
let (_, mut store) = Store::<TestChangeSet>::load(&TEST_MAGIC_BYTES, &file_path)
let (mut store, _) = Store::<TestChangeSet>::load(&TEST_MAGIC_BYTES, &file_path)
.expect("should load correctly");

// get the current position of file pointer just after loading store
Expand Down
2 changes: 1 addition & 1 deletion crates/wallet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ To persist `Wallet` state data use a data store crate that reads and writes [`Ch
use bdk_wallet::{bitcoin::Network, KeychainKind, ChangeSet, Wallet};
// Open or create a new file store for wallet data.
let (_, mut db) =
let (mut db, _) =
bdk_file_store::Store::<ChangeSet>::load_or_create(b"magic_bytes", "/tmp/my_wallet.db")
.expect("create store");
Expand Down
4 changes: 2 additions & 2 deletions crates/wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn wallet_is_persisted() -> anyhow::Result<()> {
run(
"store.db",
|path| Ok(bdk_file_store::Store::create(DB_MAGIC, path)?),
|path| Ok(bdk_file_store::Store::load(DB_MAGIC, path)?.1),
|path| Ok(bdk_file_store::Store::load(DB_MAGIC, path)?.0),
)?;
run::<bdk_chain::rusqlite::Connection, _, _>(
"store.sqlite",
Expand Down Expand Up @@ -208,7 +208,7 @@ fn wallet_load_checks() -> anyhow::Result<()> {
run(
"store.db",
|path| Ok(bdk_file_store::Store::<ChangeSet>::create(DB_MAGIC, path)?),
|path| Ok(bdk_file_store::Store::load(DB_MAGIC, path)?.1),
|path| Ok(bdk_file_store::Store::load(DB_MAGIC, path)?.0),
)?;
run(
"store.sqlite",
Expand Down
2 changes: 1 addition & 1 deletion example-crates/example_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ pub fn init_or_load<CS: clap::Subcommand, S: clap::Args>(
Commands::Generate { network } => generate_bip86_helper(network).map(|_| None),
// try load
_ => {
let (changeset, db) =
let (db, changeset) =
Store::<ChangeSet>::load(db_magic, db_path).context("could not open file store")?;

let changeset = changeset.expect("should not be empty");
Expand Down
2 changes: 1 addition & 1 deletion example-crates/example_wallet_electrum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ELECTRUM_URL: &str = "ssl://electrum.blockstream.info:60002";
fn main() -> Result<(), anyhow::Error> {
let db_path = "bdk-electrum-example.db";

let (_, mut db) = Store::<bdk_wallet::ChangeSet>::load_or_create(DB_MAGIC.as_bytes(), db_path)?;
let (mut db, _) = Store::<bdk_wallet::ChangeSet>::load_or_create(DB_MAGIC.as_bytes(), db_path)?;

let wallet_opt = Wallet::load()
.descriptor(KeychainKind::External, Some(EXTERNAL_DESC))
Expand Down
2 changes: 1 addition & 1 deletion example-crates/example_wallet_esplora_blocking/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const INTERNAL_DESC: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7
const ESPLORA_URL: &str = "http://signet.bitcoindevkit.net";

fn main() -> Result<(), anyhow::Error> {
let (_, mut db) = Store::<bdk_wallet::ChangeSet>::load_or_create(DB_MAGIC.as_bytes(), DB_PATH)?;
let (mut db, _) = Store::<bdk_wallet::ChangeSet>::load_or_create(DB_MAGIC.as_bytes(), DB_PATH)?;

let wallet_opt = Wallet::load()
.descriptor(KeychainKind::External, Some(EXTERNAL_DESC))
Expand Down
2 changes: 1 addition & 1 deletion example-crates/example_wallet_rpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn main() -> anyhow::Result<()> {
);

let start_load_wallet = Instant::now();
let (_, mut db) =
let (mut db, _) =
Store::<bdk_wallet::ChangeSet>::load_or_create(DB_MAGIC.as_bytes(), args.db_path)?;
let wallet_opt = Wallet::load()
.descriptor(KeychainKind::External, Some(args.descriptor.clone()))
Expand Down

0 comments on commit 3b695ce

Please sign in to comment.