ref(example_cli): Add new struct Init

for holding the items returned from `example_cli::init`
This commit is contained in:
vmammal 2024-01-30 17:56:51 -05:00
parent 89a7ddca7f
commit 1c15cb2f91
No known key found for this signature in database
4 changed files with 42 additions and 19 deletions

View File

@ -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()

View File

@ -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,
{
@ -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,
))
})
}

View File

@ -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);

View File

@ -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();