cli-output: Minor refactor of CliFees

This commit is contained in:
Trent Nelson 2021-02-25 23:48:17 -07:00 committed by Trent Nelson
parent 3998807dcc
commit ebd56f7ff4
2 changed files with 50 additions and 10 deletions

View File

@ -1457,17 +1457,17 @@ impl fmt::Display for CliSupply {
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CliFees {
pub struct CliFeesInner {
pub slot: Slot,
pub blockhash: String,
pub lamports_per_signature: u64,
pub last_valid_slot: Slot,
pub last_valid_slot: Option<Slot>,
}
impl QuietDisplay for CliFees {}
impl VerboseDisplay for CliFees {}
impl QuietDisplay for CliFeesInner {}
impl VerboseDisplay for CliFeesInner {}
impl fmt::Display for CliFees {
impl fmt::Display for CliFeesInner {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln_name_value(f, "Blockhash:", &self.blockhash)?;
writeln_name_value(
@ -1480,6 +1480,46 @@ impl fmt::Display for CliFees {
}
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CliFees {
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub inner: Option<CliFeesInner>,
}
impl QuietDisplay for CliFees {}
impl VerboseDisplay for CliFees {}
impl fmt::Display for CliFees {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.inner.as_ref() {
Some(inner) => write!(f, "{}", inner),
None => write!(f, "Fees unavailable"),
}
}
}
impl CliFees {
pub fn some(
slot: Slot,
blockhash: Hash,
lamports_per_signature: u64,
last_valid_slot: Option<Slot>,
) -> Self {
Self {
inner: Some(CliFeesInner {
slot,
blockhash: blockhash.to_string(),
lamports_per_signature,
last_valid_slot,
}),
}
}
pub fn none() -> Self {
Self { inner: None }
}
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CliTokenAccount {

View File

@ -823,12 +823,12 @@ pub fn process_cluster_version(rpc_client: &RpcClient, config: &CliConfig) -> Pr
pub fn process_fees(rpc_client: &RpcClient, config: &CliConfig) -> ProcessResult {
let result = rpc_client.get_recent_blockhash_with_commitment(config.commitment)?;
let (recent_blockhash, fee_calculator, last_valid_slot) = result.value;
let fees = CliFees {
slot: result.context.slot,
blockhash: recent_blockhash.to_string(),
lamports_per_signature: fee_calculator.lamports_per_signature,
let fees = CliFees::some(
result.context.slot,
recent_blockhash,
fee_calculator.lamports_per_signature,
last_valid_slot,
};
);
Ok(config.output_format.formatted_string(&fees))
}