Skip to content

Commit 60b0dd7

Browse files
author
Gabriel Comte
committed
Implement Display and serde_json::Value for BlockStatsFields
1 parent 826e01a commit 60b0dd7

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

client/src/client.rs

-2
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,6 @@ pub trait RpcApi: Sized {
471471
height: u64,
472472
fields: &[json::BlockStatsFields],
473473
) -> Result<json::GetBlockStatsResultPartial> {
474-
let fields: Vec<&str> = fields.iter().map(|field| field.get_rpc_keyword()).collect();
475-
476474
self.call("getblockstats", &[height.into(), fields.into()])
477475
}
478476

json/src/lib.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use bitcoin::util::{bip158, bip32};
3131
use bitcoin::{Address, Amount, PrivateKey, PublicKey, Script, SignedAmount, Transaction};
3232
use serde::de::Error as SerdeError;
3333
use serde::{Deserialize, Serialize};
34+
use std::fmt;
3435

3536
//TODO(stevenroose) consider using a Time type
3637

@@ -402,6 +403,7 @@ pub struct FeeRatePercentiles {
402403
pub fr_90th: Amount,
403404
}
404405

406+
#[derive(Clone)]
405407
pub enum BlockStatsFields {
406408
AverageFee,
407409
AverageFeeRate,
@@ -435,7 +437,7 @@ pub enum BlockStatsFields {
435437
}
436438

437439
impl BlockStatsFields {
438-
pub fn get_rpc_keyword(&self) -> &str {
440+
fn get_rpc_keyword(&self) -> &str {
439441
match *self {
440442
BlockStatsFields::AverageFee => "avgfee",
441443
BlockStatsFields::AverageFeeRate => "avgfeerate",
@@ -470,6 +472,18 @@ impl BlockStatsFields {
470472
}
471473
}
472474

475+
impl fmt::Display for BlockStatsFields {
476+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
477+
write!(f, "{}", self.get_rpc_keyword())
478+
}
479+
}
480+
481+
impl From<BlockStatsFields> for serde_json::Value {
482+
fn from(bsf: BlockStatsFields) -> Self {
483+
Self::from(bsf.to_string())
484+
}
485+
}
486+
473487
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
474488
#[serde(rename_all = "camelCase")]
475489
pub struct GetMiningInfoResult {
@@ -1157,7 +1171,6 @@ impl<'de> serde::Deserialize<'de> for ImportMultiRescanSince {
11571171
D: serde::Deserializer<'de>,
11581172
{
11591173
use serde::de;
1160-
use std::fmt;
11611174
struct Visitor;
11621175
impl<'de> de::Visitor<'de> for Visitor {
11631176
type Value = ImportMultiRescanSince;

0 commit comments

Comments
 (0)