From b6c77e839af38bdfe8cf18f223975544d51dd4c1 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 9 Jan 2024 00:00:57 -0800 Subject: [PATCH] Suppress empty command output --- src/lib.rs | 6 +++++- src/subcommand.rs | 5 +---- src/subcommand/balances.rs | 4 ++-- src/subcommand/decode.rs | 6 +++--- src/subcommand/epochs.rs | 2 +- src/subcommand/find.rs | 4 ++-- src/subcommand/index/export.rs | 2 +- src/subcommand/index/info.rs | 4 ++-- src/subcommand/index/update.rs | 2 +- src/subcommand/list.rs | 2 +- src/subcommand/parse.rs | 4 ++-- src/subcommand/preview.rs | 2 +- src/subcommand/runes.rs | 4 ++-- src/subcommand/server.rs | 2 +- src/subcommand/subsidy.rs | 4 ++-- src/subcommand/supply.rs | 4 ++-- src/subcommand/teleburn.rs | 4 ++-- src/subcommand/traits.rs | 4 ++-- src/subcommand/wallet/balance.rs | 4 ++-- src/subcommand/wallet/cardinals.rs | 2 +- src/subcommand/wallet/create.rs | 4 ++-- src/subcommand/wallet/etch.rs | 4 ++-- src/subcommand/wallet/inscribe/batch.rs | 8 ++++---- src/subcommand/wallet/inscriptions.rs | 2 +- src/subcommand/wallet/outputs.rs | 2 +- src/subcommand/wallet/receive.rs | 2 +- src/subcommand/wallet/restore.rs | 2 +- src/subcommand/wallet/sats.rs | 4 ++-- src/subcommand/wallet/send.rs | 6 +++--- src/subcommand/wallet/transactions.rs | 2 +- tests/index.rs | 11 +++++------ tests/wallet/restore.rs | 6 +++--- 32 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9102594701..27662d0f9e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -248,7 +248,11 @@ pub fn main() { process::exit(1); } - Ok(output) => output.print_json(), + Ok(output) => { + if let Some(output) = output { + output.print_json(); + } + } } gracefully_shutdown_indexer(); diff --git a/src/subcommand.rs b/src/subcommand.rs index ff7b406079..bc158a1b4e 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -77,9 +77,6 @@ impl Subcommand { } } -#[derive(Serialize, Deserialize)] -pub struct Empty {} - pub(crate) trait Output: Send { fn print_json(&self); } @@ -94,4 +91,4 @@ where } } -pub(crate) type SubcommandResult = Result>; +pub(crate) type SubcommandResult = Result>>; diff --git a/src/subcommand/balances.rs b/src/subcommand/balances.rs index cc94155bbf..35424bc879 100644 --- a/src/subcommand/balances.rs +++ b/src/subcommand/balances.rs @@ -15,7 +15,7 @@ pub(crate) fn run(options: Options) -> SubcommandResult { index.update()?; - Ok(Box::new(Output { + Ok(Some(Box::new(Output { runes: index.get_rune_balance_map()?, - })) + }))) } diff --git a/src/subcommand/decode.rs b/src/subcommand/decode.rs index 38d2275524..3d3184ff86 100644 --- a/src/subcommand/decode.rs +++ b/src/subcommand/decode.rs @@ -88,15 +88,15 @@ impl Decode { let inscriptions = ParsedEnvelope::from_transaction(&transaction); if self.compact { - Ok(Box::new(CompactOutput { + Ok(Some(Box::new(CompactOutput { inscriptions: inscriptions .clone() .into_iter() .map(|inscription| inscription.payload.try_into()) .collect::>>()?, - })) + }))) } else { - Ok(Box::new(RawOutput { inscriptions })) + Ok(Some(Box::new(RawOutput { inscriptions }))) } } } diff --git a/src/subcommand/epochs.rs b/src/subcommand/epochs.rs index 39534a65ca..b5971f49f2 100644 --- a/src/subcommand/epochs.rs +++ b/src/subcommand/epochs.rs @@ -11,5 +11,5 @@ pub(crate) fn run() -> SubcommandResult { starting_sats.push(sat); } - Ok(Box::new(Output { starting_sats })) + Ok(Some(Box::new(Output { starting_sats }))) } diff --git a/src/subcommand/find.rs b/src/subcommand/find.rs index 68884679df..1f35bc518e 100644 --- a/src/subcommand/find.rs +++ b/src/subcommand/find.rs @@ -32,11 +32,11 @@ impl Find { match self.end { Some(end) => match index.find_range(self.sat, end)? { - Some(result) => Ok(Box::new(result)), + Some(result) => Ok(Some(Box::new(result))), None => Err(anyhow!("range has not been mined as of index height")), }, None => match index.find(self.sat)? { - Some(satpoint) => Ok(Box::new(Output { satpoint })), + Some(satpoint) => Ok(Some(Box::new(Output { satpoint }))), None => Err(anyhow!("sat has not been mined as of index height")), }, } diff --git a/src/subcommand/index/export.rs b/src/subcommand/index/export.rs index cddebe59bb..4cb45af531 100644 --- a/src/subcommand/index/export.rs +++ b/src/subcommand/index/export.rs @@ -15,6 +15,6 @@ impl Export { index.update()?; index.export(&self.tsv, self.include_addresses)?; - Ok(Box::new(Empty {})) + Ok(None) } } diff --git a/src/subcommand/index/info.rs b/src/subcommand/index/info.rs index 8b9a2deb07..3773e43eb3 100644 --- a/src/subcommand/index/info.rs +++ b/src/subcommand/index/info.rs @@ -34,9 +34,9 @@ impl Info { elapsed: (end.starting_timestamp - start.starting_timestamp) as f64 / 1000.0 / 60.0, }); } - Ok(Box::new(output)) + Ok(Some(Box::new(output))) } else { - Ok(Box::new(info)) + Ok(Some(Box::new(info))) } } } diff --git a/src/subcommand/index/update.rs b/src/subcommand/index/update.rs index cd3d7d45e8..62a82ff838 100644 --- a/src/subcommand/index/update.rs +++ b/src/subcommand/index/update.rs @@ -5,5 +5,5 @@ pub(crate) fn run(options: Options) -> SubcommandResult { index.update()?; - Ok(Box::new(Empty {})) + Ok(None) } diff --git a/src/subcommand/list.rs b/src/subcommand/list.rs index c0477407c0..8de7f8e0ea 100644 --- a/src/subcommand/list.rs +++ b/src/subcommand/list.rs @@ -51,7 +51,7 @@ impl List { }); } - Ok(Box::new(outputs)) + Ok(Some(Box::new(outputs))) } Some(crate::index::List::Spent) => Err(anyhow!("output spent.")), None => Err(anyhow!("output not found")), diff --git a/src/subcommand/parse.rs b/src/subcommand/parse.rs index 2430abd185..b3c1067a05 100644 --- a/src/subcommand/parse.rs +++ b/src/subcommand/parse.rs @@ -13,8 +13,8 @@ pub struct Output { impl Parse { pub(crate) fn run(self) -> SubcommandResult { - Ok(Box::new(Output { + Ok(Some(Box::new(Output { object: self.object, - })) + }))) } } diff --git a/src/subcommand/preview.rs b/src/subcommand/preview.rs index 845c0c90d6..effada817d 100644 --- a/src/subcommand/preview.rs +++ b/src/subcommand/preview.rs @@ -200,6 +200,6 @@ impl Preview { .run()?; } - Ok(Box::new(Empty {})) + Ok(None) } } diff --git a/src/subcommand/runes.rs b/src/subcommand/runes.rs index ed28f68bd8..854d04a37e 100644 --- a/src/subcommand/runes.rs +++ b/src/subcommand/runes.rs @@ -35,7 +35,7 @@ pub(crate) fn run(options: Options) -> SubcommandResult { index.update()?; - Ok(Box::new(Output { + Ok(Some(Box::new(Output { runes: index .runes()? .into_iter() @@ -82,5 +82,5 @@ pub(crate) fn run(options: Options) -> SubcommandResult { }, ) .collect::>(), - })) + }))) } diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index a9806f4181..cdcacab008 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -334,7 +334,7 @@ impl Server { (None, None) => unreachable!(), } - Ok(Box::new(Empty {}) as Box) + Ok(None) }) } diff --git a/src/subcommand/subsidy.rs b/src/subcommand/subsidy.rs index 9f0db8a981..52d2c7d6d1 100644 --- a/src/subcommand/subsidy.rs +++ b/src/subcommand/subsidy.rs @@ -23,10 +23,10 @@ impl Subsidy { bail!("block {} has no subsidy", self.height); } - Ok(Box::new(Output { + Ok(Some(Box::new(Output { first: first.0, subsidy, name: first.name(), - })) + }))) } } diff --git a/src/subcommand/supply.rs b/src/subcommand/supply.rs index 66622ef361..f6ca1d8876 100644 --- a/src/subcommand/supply.rs +++ b/src/subcommand/supply.rs @@ -18,10 +18,10 @@ pub(crate) fn run() -> SubcommandResult { last += 1; } - Ok(Box::new(Output { + Ok(Some(Box::new(Output { supply: Sat::SUPPLY, first: 0, last: Sat::SUPPLY - 1, last_mined_in_block: last, - })) + }))) } diff --git a/src/subcommand/teleburn.rs b/src/subcommand/teleburn.rs index e13d5d57e3..d0181021b9 100644 --- a/src/subcommand/teleburn.rs +++ b/src/subcommand/teleburn.rs @@ -13,8 +13,8 @@ pub struct Output { impl Teleburn { pub(crate) fn run(self) -> SubcommandResult { - Ok(Box::new(Output { + Ok(Some(Box::new(Output { ethereum: self.destination.into(), - })) + }))) } } diff --git a/src/subcommand/traits.rs b/src/subcommand/traits.rs index ce2bce499a..05f177f526 100644 --- a/src/subcommand/traits.rs +++ b/src/subcommand/traits.rs @@ -22,7 +22,7 @@ pub struct Output { impl Traits { pub(crate) fn run(self) -> SubcommandResult { - Ok(Box::new(Output { + Ok(Some(Box::new(Output { number: self.sat.n(), decimal: self.sat.decimal().to_string(), degree: self.sat.degree().to_string(), @@ -33,6 +33,6 @@ impl Traits { period: self.sat.period(), offset: self.sat.third(), rarity: self.sat.rarity(), - })) + }))) } } diff --git a/src/subcommand/wallet/balance.rs b/src/subcommand/wallet/balance.rs index 8a95985e44..ae88dc2aba 100644 --- a/src/subcommand/wallet/balance.rs +++ b/src/subcommand/wallet/balance.rs @@ -45,13 +45,13 @@ pub(crate) fn run(wallet: String, options: Options) -> SubcommandResult { } } - Ok(Box::new(Output { + Ok(Some(Box::new(Output { cardinal, ordinal, runes: index.has_rune_index().then_some(runes), runic: index.has_rune_index().then_some(runic), total: cardinal + ordinal + runic, - })) + }))) } #[cfg(test)] diff --git a/src/subcommand/wallet/cardinals.rs b/src/subcommand/wallet/cardinals.rs index ab6e932521..f809571e34 100644 --- a/src/subcommand/wallet/cardinals.rs +++ b/src/subcommand/wallet/cardinals.rs @@ -35,5 +35,5 @@ pub(crate) fn run(wallet: String, options: Options) -> SubcommandResult { }) .collect::>(); - Ok(Box::new(cardinal_utxos)) + Ok(Some(Box::new(cardinal_utxos))) } diff --git a/src/subcommand/wallet/create.rs b/src/subcommand/wallet/create.rs index 6b8fb9b0a0..69121d2746 100644 --- a/src/subcommand/wallet/create.rs +++ b/src/subcommand/wallet/create.rs @@ -25,9 +25,9 @@ impl Create { wallet::initialize(wallet, &options, mnemonic.to_seed(self.passphrase.clone()))?; - Ok(Box::new(Output { + Ok(Some(Box::new(Output { mnemonic, passphrase: Some(self.passphrase), - })) + }))) } } diff --git a/src/subcommand/wallet/etch.rs b/src/subcommand/wallet/etch.rs index 1c00a36370..2f863f7182 100644 --- a/src/subcommand/wallet/etch.rs +++ b/src/subcommand/wallet/etch.rs @@ -124,9 +124,9 @@ impl Etch { let transaction = client.send_raw_transaction(&signed_transaction)?; - Ok(Box::new(Output { + Ok(Some(Box::new(Output { rune: self.rune, transaction, - })) + }))) } } diff --git a/src/subcommand/wallet/inscribe/batch.rs b/src/subcommand/wallet/inscribe/batch.rs index 1024097825..31671effa0 100644 --- a/src/subcommand/wallet/inscribe/batch.rs +++ b/src/subcommand/wallet/inscribe/batch.rs @@ -62,12 +62,12 @@ impl Batch { )?; if self.dry_run { - return Ok(Box::new(self.output( + return Ok(Some(Box::new(self.output( commit_tx.txid(), reveal_tx.txid(), total_fees, self.inscriptions.clone(), - ))); + )))); } let signed_commit_tx = client @@ -114,12 +114,12 @@ impl Batch { } }; - Ok(Box::new(self.output( + Ok(Some(Box::new(self.output( commit, reveal, total_fees, self.inscriptions.clone(), - ))) + )))) } fn output( diff --git a/src/subcommand/wallet/inscriptions.rs b/src/subcommand/wallet/inscriptions.rs index 5cf2c682f8..0d58297fff 100644 --- a/src/subcommand/wallet/inscriptions.rs +++ b/src/subcommand/wallet/inscriptions.rs @@ -38,5 +38,5 @@ pub(crate) fn run(wallet: String, options: Options) -> SubcommandResult { } } - Ok(Box::new(output)) + Ok(Some(Box::new(output))) } diff --git a/src/subcommand/wallet/outputs.rs b/src/subcommand/wallet/outputs.rs index 9c4655b61a..b37faadbea 100644 --- a/src/subcommand/wallet/outputs.rs +++ b/src/subcommand/wallet/outputs.rs @@ -21,5 +21,5 @@ pub(crate) fn run(wallet: String, options: Options) -> SubcommandResult { }); } - Ok(Box::new(outputs)) + Ok(Some(Box::new(outputs))) } diff --git a/src/subcommand/wallet/receive.rs b/src/subcommand/wallet/receive.rs index 2ff8afc481..fe7d40ec2a 100644 --- a/src/subcommand/wallet/receive.rs +++ b/src/subcommand/wallet/receive.rs @@ -9,5 +9,5 @@ pub(crate) fn run(wallet: String, options: Options) -> SubcommandResult { let address = bitcoin_rpc_client_for_wallet_command(wallet, &options)? .get_new_address(None, Some(bitcoincore_rpc::json::AddressType::Bech32m))?; - Ok(Box::new(Output { address })) + Ok(Some(Box::new(Output { address }))) } diff --git a/src/subcommand/wallet/restore.rs b/src/subcommand/wallet/restore.rs index d4e48b6b78..54c2d64d4e 100644 --- a/src/subcommand/wallet/restore.rs +++ b/src/subcommand/wallet/restore.rs @@ -20,6 +20,6 @@ impl Restore { self.mnemonic.to_seed(self.passphrase), )?; - Ok(Box::new(Empty {})) + Ok(None) } } diff --git a/src/subcommand/wallet/sats.rs b/src/subcommand/wallet/sats.rs index 926610f9b8..65a0c2491d 100644 --- a/src/subcommand/wallet/sats.rs +++ b/src/subcommand/wallet/sats.rs @@ -49,7 +49,7 @@ impl Sats { output: outpoint, }); } - Ok(Box::new(output)) + Ok(Some(Box::new(output))) } else { let mut output = Vec::new(); for (outpoint, sat, offset, rarity) in rare_sats(utxos) { @@ -60,7 +60,7 @@ impl Sats { rarity, }); } - Ok(Box::new(output)) + Ok(Some(Box::new(output))) } } } diff --git a/src/subcommand/wallet/send.rs b/src/subcommand/wallet/send.rs index bf1d98b0c4..39eed8712f 100644 --- a/src/subcommand/wallet/send.rs +++ b/src/subcommand/wallet/send.rs @@ -46,7 +46,7 @@ impl Send { Outgoing::Amount(amount) => { Self::lock_non_cardinal_outputs(&client, &inscriptions, &runic_outputs, unspent_outputs)?; let transaction = Self::send_amount(&client, amount, address, self.fee_rate)?; - return Ok(Box::new(Output { transaction })); + return Ok(Some(Box::new(Output { transaction }))); } Outgoing::InscriptionId(id) => index .get_inscription_satpoint_by_id(id)? @@ -64,7 +64,7 @@ impl Send { runic_outputs, unspent_outputs, )?; - return Ok(Box::new(Output { transaction })); + return Ok(Some(Box::new(Output { transaction }))); } Outgoing::SatPoint(satpoint) => { for inscription_satpoint in inscriptions.keys() { @@ -112,7 +112,7 @@ impl Send { let txid = client.send_raw_transaction(&signed_tx)?; - Ok(Box::new(Output { transaction: txid })) + Ok(Some(Box::new(Output { transaction: txid }))) } fn lock_non_cardinal_outputs( diff --git a/src/subcommand/wallet/transactions.rs b/src/subcommand/wallet/transactions.rs index 8c11410127..d212f2200e 100644 --- a/src/subcommand/wallet/transactions.rs +++ b/src/subcommand/wallet/transactions.rs @@ -29,6 +29,6 @@ impl Transactions { }); } - Ok(Box::new(output)) + Ok(Some(Box::new(output))) } } diff --git a/tests/index.rs b/tests/index.rs index 72e3a20ecf..663acb9f8b 100644 --- a/tests/index.rs +++ b/tests/index.rs @@ -1,4 +1,4 @@ -use {super::*, crate::command_builder::ToArgs, ord::subcommand::Empty}; +use {super::*, crate::command_builder::ToArgs}; #[test] fn run_is_an_alias_for_update() { @@ -11,7 +11,7 @@ fn run_is_an_alias_for_update() { CommandBuilder::new(format!("--index {} index run", index_path.display())) .rpc_server(&rpc_server) - .run_and_deserialize_output::(); + .run_and_extract_stdout(); assert!(index_path.is_file()) } @@ -27,7 +27,7 @@ fn custom_index_path() { CommandBuilder::new(format!("--index {} index update", index_path.display())) .rpc_server(&rpc_server) - .run_and_deserialize_output::(); + .run_and_extract_stdout(); assert!(index_path.is_file()) } @@ -43,13 +43,13 @@ fn re_opening_database_does_not_trigger_schema_check() { CommandBuilder::new(format!("--index {} index update", index_path.display())) .rpc_server(&rpc_server) - .run_and_deserialize_output::(); + .run_and_extract_stdout(); assert!(index_path.is_file()); CommandBuilder::new(format!("--index {} index update", index_path.display())) .rpc_server(&rpc_server) - .run_and_deserialize_output::(); + .run_and_extract_stdout(); } #[test] @@ -96,7 +96,6 @@ fn export_inscription_number_to_id_tsv() { let tsv = CommandBuilder::new("index export --tsv foo.tsv") .rpc_server(&rpc_server) .temp_dir(Arc::new(temp_dir)) - .stdout_regex(r"\{\}\n") .run_and_extract_file("foo.tsv"); let entries: std::collections::BTreeMap = tsv diff --git a/tests/wallet/restore.rs b/tests/wallet/restore.rs index 5106c5eb33..f13e601a70 100644 --- a/tests/wallet/restore.rs +++ b/tests/wallet/restore.rs @@ -1,4 +1,4 @@ -use {super::*, ord::subcommand::wallet::create, ord::subcommand::Empty}; +use {super::*, ord::subcommand::wallet::create}; #[test] fn restore_generates_same_descriptors() { @@ -16,7 +16,7 @@ fn restore_generates_same_descriptors() { CommandBuilder::new(["wallet", "restore", &mnemonic.to_string()]) .rpc_server(&rpc_server) - .run_and_deserialize_output::(); + .run_and_extract_stdout(); assert_eq!(rpc_server.descriptors(), descriptors); } @@ -45,7 +45,7 @@ fn restore_generates_same_descriptors_with_passphrase() { &mnemonic.to_string(), ]) .rpc_server(&rpc_server) - .run_and_deserialize_output::(); + .run_and_extract_stdout(); assert_eq!(rpc_server.descriptors(), descriptors); }