Skip to content

Commit

Permalink
Allow specifying different output formats (#3424)
Browse files Browse the repository at this point in the history
  • Loading branch information
bingryan committed May 6, 2024
1 parent c9a9377 commit a14599f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use {
into_usize::IntoUsize,
representation::Representation,
settings::Settings,
subcommand::{Subcommand, SubcommandResult},
subcommand::{OutputFormat, Subcommand, SubcommandResult},
tally::Tally,
},
anyhow::{anyhow, bail, ensure, Context, Error},
Expand Down Expand Up @@ -252,7 +252,7 @@ pub fn main() {

let args = Arguments::parse();

let minify = args.options.minify;
let format = args.options.format;

match args.run() {
Err(err) => {
Expand All @@ -274,7 +274,7 @@ pub fn main() {
}
Ok(output) => {
if let Some(output) = output {
output.print_json(minify);
output.print(format.unwrap_or_default());
}
gracefully_shutdown_indexer();
}
Expand Down
4 changes: 2 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ pub struct Options {
pub(crate) index_transactions: bool,
#[arg(long, help = "Run in integration test mode.")]
pub(crate) integration_test: bool,
#[arg(long, help = "Minify JSON output.")]
pub(crate) minify: bool,
#[clap(long, short, long, help = "Specify output format. [default: json]")]
pub(crate) format: Option<OutputFormat>,
#[arg(
long,
short,
Expand Down
22 changes: 15 additions & 7 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,28 @@ impl Subcommand {
}
}

#[derive(clap::ValueEnum, Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub enum OutputFormat {
#[default]
Json,
Yaml,
Minify,
}

pub trait Output: Send {
fn print_json(&self, minify: bool);
fn print(&self, format: OutputFormat);
}

impl<T> Output for T
where
T: Serialize + Send,
{
fn print_json(&self, minify: bool) {
if minify {
serde_json::to_writer(io::stdout(), self).ok();
} else {
serde_json::to_writer_pretty(io::stdout(), self).ok();
}
fn print(&self, format: OutputFormat) {
match format {
OutputFormat::Json => serde_json::to_writer_pretty(io::stdout(), self).ok(),
OutputFormat::Yaml => serde_yaml::to_writer(io::stdout(), self).ok(),
OutputFormat::Minify => serde_json::to_writer(io::stdout(), self).ok(),
};
println!();
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/wallet/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn dump_and_restore_descriptors_with_minify() {

create_wallet(&core, &ord);

let output = CommandBuilder::new("--minify wallet dump")
let output = CommandBuilder::new("--format minify wallet dump")
.core(&core)
.ord(&ord)
.stderr_regex(".*")
Expand Down

0 comments on commit a14599f

Please sign in to comment.