Skip to content

Commit a14599f

Browse files
authored
Allow specifying different output formats (ordinals#3424)
1 parent c9a9377 commit a14599f

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use {
2626
into_usize::IntoUsize,
2727
representation::Representation,
2828
settings::Settings,
29-
subcommand::{Subcommand, SubcommandResult},
29+
subcommand::{OutputFormat, Subcommand, SubcommandResult},
3030
tally::Tally,
3131
},
3232
anyhow::{anyhow, bail, ensure, Context, Error},
@@ -252,7 +252,7 @@ pub fn main() {
252252

253253
let args = Arguments::parse();
254254

255-
let minify = args.options.minify;
255+
let format = args.options.format;
256256

257257
match args.run() {
258258
Err(err) => {
@@ -274,7 +274,7 @@ pub fn main() {
274274
}
275275
Ok(output) => {
276276
if let Some(output) = output {
277-
output.print_json(minify);
277+
output.print(format.unwrap_or_default());
278278
}
279279
gracefully_shutdown_indexer();
280280
}

src/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ pub struct Options {
6565
pub(crate) index_transactions: bool,
6666
#[arg(long, help = "Run in integration test mode.")]
6767
pub(crate) integration_test: bool,
68-
#[arg(long, help = "Minify JSON output.")]
69-
pub(crate) minify: bool,
68+
#[clap(long, short, long, help = "Specify output format. [default: json]")]
69+
pub(crate) format: Option<OutputFormat>,
7070
#[arg(
7171
long,
7272
short,

src/subcommand.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,28 @@ impl Subcommand {
8181
}
8282
}
8383

84+
#[derive(clap::ValueEnum, Debug, Clone, Copy, Serialize, Deserialize, Default)]
85+
pub enum OutputFormat {
86+
#[default]
87+
Json,
88+
Yaml,
89+
Minify,
90+
}
91+
8492
pub trait Output: Send {
85-
fn print_json(&self, minify: bool);
93+
fn print(&self, format: OutputFormat);
8694
}
8795

8896
impl<T> Output for T
8997
where
9098
T: Serialize + Send,
9199
{
92-
fn print_json(&self, minify: bool) {
93-
if minify {
94-
serde_json::to_writer(io::stdout(), self).ok();
95-
} else {
96-
serde_json::to_writer_pretty(io::stdout(), self).ok();
97-
}
100+
fn print(&self, format: OutputFormat) {
101+
match format {
102+
OutputFormat::Json => serde_json::to_writer_pretty(io::stdout(), self).ok(),
103+
OutputFormat::Yaml => serde_yaml::to_writer(io::stdout(), self).ok(),
104+
OutputFormat::Minify => serde_json::to_writer(io::stdout(), self).ok(),
105+
};
98106
println!();
99107
}
100108
}

tests/wallet/dump.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn dump_and_restore_descriptors_with_minify() {
5555

5656
create_wallet(&core, &ord);
5757

58-
let output = CommandBuilder::new("--minify wallet dump")
58+
let output = CommandBuilder::new("--format minify wallet dump")
5959
.core(&core)
6060
.ord(&ord)
6161
.stderr_regex(".*")

0 commit comments

Comments
 (0)