[client cli] generic blocknumber (#4376)

* rewrite me

* [cli]: make `BlockNumber` generic

* cleanup
This commit is contained in:
Niklas Adolfsson
2019-12-17 11:45:20 +01:00
committed by Bastian Köcher
parent e19f5adfb7
commit 7c6ad9dd1c
5 changed files with 54 additions and 14 deletions
+39 -4
View File
@@ -16,7 +16,7 @@
use crate::traits::{AugmentClap, GetLogFilter};
use std::path::PathBuf;
use std::{str::FromStr, path::PathBuf};
use structopt::{StructOpt, clap::{arg_enum, App, AppSettings, SubCommand, Arg}};
pub use crate::execution_strategy::ExecutionStrategy;
@@ -734,6 +734,41 @@ pub struct BuildSpecCmd {
impl_get_log_filter!(BuildSpecCmd);
/// Wrapper type of `String` which holds an arbitary sized unsigned integer formatted as decimal.
#[derive(Debug, Clone)]
pub struct BlockNumber(String);
impl FromStr for BlockNumber {
type Err = String;
fn from_str(block_number: &str) -> Result<Self, Self::Err> {
if block_number.chars().any(|d| !d.is_digit(10)) {
Err(format!(
"Invalid block number: {}, expected decimal formatted unsigned integer",
block_number
))
} else {
Ok(Self(block_number.to_owned()))
}
}
}
impl BlockNumber {
/// Wrapper on top of `std::str::parse<N>` but with `Error` as a `String`
///
/// See `https://doc.rust-lang.org/std/primitive.str.html#method.parse` for more elaborate
/// documentation.
pub fn parse<N>(&self) -> Result<N, String>
where
N: FromStr,
N::Err: std::fmt::Debug,
{
self.0
.parse()
.map_err(|e| format!("BlockNumber: {} parsing failed because of {:?}", self.0, e))
}
}
/// The `export-blocks` command used to export blocks.
#[derive(Debug, StructOpt, Clone)]
pub struct ExportBlocksCmd {
@@ -745,13 +780,13 @@ pub struct ExportBlocksCmd {
///
/// Default is 1.
#[structopt(long = "from", value_name = "BLOCK")]
pub from: Option<u32>,
pub from: Option<BlockNumber>,
/// Specify last block number.
///
/// Default is best block.
#[structopt(long = "to", value_name = "BLOCK")]
pub to: Option<u32>,
pub to: Option<BlockNumber>,
/// Use JSON output rather than binary.
#[structopt(long = "json")]
@@ -817,7 +852,7 @@ impl_get_log_filter!(CheckBlockCmd);
pub struct RevertCmd {
/// Number of blocks to revert.
#[structopt(default_value = "256")]
pub num: u32,
pub num: BlockNumber,
#[allow(missing_docs)]
#[structopt(flatten)]