rpc_toolkit/
command_helpers.rs

1use std::fmt::Display;
2use std::io::Stdin;
3use std::str::FromStr;
4
5use clap::ArgMatches;
6pub use {clap, serde};
7
8pub fn default_arg_parser<T>(arg: &str, _: &ArgMatches) -> Result<T, clap::Error>
9where
10    T: FromStr,
11    T::Err: Display,
12{
13    arg.parse()
14        .map_err(|e| clap::Error::raw(clap::error::ErrorKind::ValueValidation, e))
15}
16
17pub fn default_stdin_parser<T>(stdin: &mut Stdin, _: &ArgMatches) -> Result<T, clap::Error>
18where
19    T: FromStr,
20    T::Err: Display,
21{
22    let mut s = String::new();
23    stdin
24        .read_line(&mut s)
25        .map_err(|e| clap::Error::raw(clap::error::ErrorKind::Io, e))?;
26    if let Some(s) = s.strip_suffix("\n") {
27        s
28    } else {
29        &s
30    }
31    .parse()
32    .map_err(|e| clap::Error::raw(clap::error::ErrorKind::ValueValidation, e))
33}