ledger-tool: Use OutputFormat printer in program subcommand (#34475)

Use OutputFormat printer
This commit is contained in:
Tyera 2023-12-15 10:38:44 -07:00 committed by GitHub
parent eaec42280a
commit 74d02acafd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 14 deletions

View File

@ -9,6 +9,7 @@ use {
syscalls::create_program_runtime_environment_v1, syscalls::create_program_runtime_environment_v1,
}, },
solana_clap_utils::input_parsers::pubkeys_of, solana_clap_utils::input_parsers::pubkeys_of,
solana_cli_output::{OutputFormat, QuietDisplay, VerboseDisplay},
solana_ledger::{ solana_ledger::{
blockstore_options::{AccessType, BlockstoreRecoveryMode}, blockstore_options::{AccessType, BlockstoreRecoveryMode},
blockstore_processor::ProcessOptions, blockstore_processor::ProcessOptions,
@ -33,7 +34,7 @@ use {
}, },
std::{ std::{
collections::HashSet, collections::HashSet,
fmt::{Debug, Formatter}, fmt::{self, Debug, Formatter},
fs::File, fs::File,
io::{Read, Seek, Write}, io::{Read, Seek, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -263,8 +264,9 @@ struct Output {
log: Vec<String>, log: Vec<String>,
} }
impl Debug for Output { impl fmt::Display for Output {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Program output:")?;
writeln!(f, "Result: {}", self.result)?; writeln!(f, "Result: {}", self.result)?;
writeln!(f, "Instruction Count: {}", self.instruction_count)?; writeln!(f, "Instruction Count: {}", self.instruction_count)?;
writeln!(f, "Execution time: {} us", self.execution_time.as_micros())?; writeln!(f, "Execution time: {} us", self.execution_time.as_micros())?;
@ -275,6 +277,9 @@ impl Debug for Output {
} }
} }
impl QuietDisplay for Output {}
impl VerboseDisplay for Output {}
// Replace with std::lazy::Lazy when stabilized. // Replace with std::lazy::Lazy when stabilized.
// https://github.com/rust-lang/rust/issues/74465 // https://github.com/rust-lang/rust/issues/74465
struct LazyAnalysis<'a, 'b> { struct LazyAnalysis<'a, 'b> {
@ -612,16 +617,6 @@ pub fn program(ledger_path: &Path, matches: &ArgMatches<'_>) {
.get_recorded_content() .get_recorded_content()
.to_vec(), .to_vec(),
}; };
match matches.value_of("output_format") { let output_format = OutputFormat::from_matches(matches, "output_format", false);
Some("json") => { println!("{}", output_format.formatted_string(&output));
println!("{}", serde_json::to_string_pretty(&output).unwrap());
}
Some("json-compact") => {
println!("{}", serde_json::to_string(&output).unwrap());
}
_ => {
println!("Program output:");
println!("{output:?}");
}
}
} }