Skip to content

Commit

Permalink
ref(example_cli): Add new struct Init
Browse files Browse the repository at this point in the history
for holding the items returned from `example_cli::init`
  • Loading branch information
ValuedMammal committed Jan 31, 2024
1 parent 6edc07e commit 61cda41
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
10 changes: 7 additions & 3 deletions example-crates/example_bitcoind_rpc_polling/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,13 @@ enum RpcCommands {

fn main() -> anyhow::Result<()> {
let start = Instant::now();

let (args, keymap, index, db, init_changeset) =
example_cli::init::<RpcCommands, RpcArgs, ChangeSet>(DB_MAGIC, DB_PATH)?;
let example_cli::Init {
args,
keymap,
index,
db,
init_changeset,
} = example_cli::init::<RpcCommands, RpcArgs, ChangeSet>(DB_MAGIC, DB_PATH)?;
println!(
"[{:>10}s] loaded initial changeset from db",
start.elapsed().as_secs_f32()
Expand Down
31 changes: 19 additions & 12 deletions example-crates/example_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,19 +656,26 @@ where
}
}

// Alias the `Result` of `init`
pub type InitialState<CS, S, C> = (
Args<CS, S>,
KeyMap,
KeychainTxOutIndex<Keychain>,
Mutex<Database<C>>,
C,
);
/// The initial state returned by [`init`].
pub struct Init<CS: clap::Subcommand, S: clap::Args, C> {
/// Arguments parsed by the cli.
pub args: Args<CS, S>,
/// Descriptor keymap.
pub keymap: KeyMap,
/// Keychain-txout index.
pub index: KeychainTxOutIndex<Keychain>,
/// Persistence backend.
pub db: Mutex<Database<C>>,
/// Initial changeset.
pub init_changeset: C,
}

/// Parses command line arguments and initializes all components, creating
/// a file store with the given parameters, or loading one if it exists.
pub fn init<CS: clap::Subcommand, S: clap::Args, C>(
db_magic: &[u8],
db_default_path: &str,
) -> anyhow::Result<InitialState<CS, S, C>>
) -> anyhow::Result<Init<CS, S, C>>
where
C: Default + Append + Serialize + DeserializeOwned,
{
Expand Down Expand Up @@ -702,11 +709,11 @@ where

let init_changeset = db_backend.load_from_persistence()?.unwrap_or_default();

Ok((
Ok(Init {
args,
keymap,
index,
Mutex::new(Database::new(db_backend)),
db: Mutex::new(Database::new(db_backend)),
init_changeset,
))
})
}
11 changes: 9 additions & 2 deletions example-crates/example_electrum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,15 @@ type ChangeSet = (
);

fn main() -> anyhow::Result<()> {
let (args, keymap, index, db, (disk_local_chain, disk_tx_graph)) =
example_cli::init::<ElectrumCommands, ElectrumArgs, ChangeSet>(DB_MAGIC, DB_PATH)?;
let example_cli::Init {
args,
keymap,
index,
db,
init_changeset,
} = example_cli::init::<ElectrumCommands, ElectrumArgs, ChangeSet>(DB_MAGIC, DB_PATH)?;

let (disk_local_chain, disk_tx_graph) = init_changeset;

let graph = Mutex::new({
let mut graph = IndexedTxGraph::new(index);
Expand Down
9 changes: 7 additions & 2 deletions example-crates/example_esplora/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ pub struct ScanOptions {
}

fn main() -> anyhow::Result<()> {
let (args, keymap, index, db, init_changeset) =
example_cli::init::<EsploraCommands, EsploraArgs, ChangeSet>(DB_MAGIC, DB_PATH)?;
let example_cli::Init {
args,
keymap,
index,
db,
init_changeset,
} = example_cli::init::<EsploraCommands, EsploraArgs, ChangeSet>(DB_MAGIC, DB_PATH)?;

let genesis_hash = genesis_block(args.network).block_hash();

Expand Down

0 comments on commit 61cda41

Please sign in to comment.