generated from PlexSheep/rs-base
refactor: numf_parser takes a generic unsigned int #13
cargo devel CI / cargo CI (push) Has been cancelled
Details
cargo devel CI / cargo CI (push) Has been cancelled
Details
This commit is contained in:
parent
c6ad45fb82
commit
6649470ff7
|
@ -19,4 +19,5 @@ clap = { version = "4.5.4", features = ["derive"] }
|
||||||
clap-num = "1.1.1"
|
clap-num = "1.1.1"
|
||||||
fast32 = "1.0.2"
|
fast32 = "1.0.2"
|
||||||
libpt = { version = "0.5.0", features = ["bintols"]}
|
libpt = { version = "0.5.0", features = ["bintols"]}
|
||||||
|
num = "0.4.3"
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)] // this is exported to lib.rs
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
// this is exported to lib.rs
|
|
||||||
use clap::{ArgGroup, Parser};
|
use clap::{ArgGroup, Parser};
|
||||||
use libpt::bintols::split;
|
use libpt::bintols::split;
|
||||||
|
use num;
|
||||||
|
|
||||||
pub type NumberType = u128;
|
pub type NumberType = u128;
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ pub struct FormatOptions {
|
||||||
#[arg(short = 'z', long)]
|
#[arg(short = 'z', long)]
|
||||||
/// format to base32
|
/// format to base32
|
||||||
base32: bool,
|
base32: bool,
|
||||||
#[clap(value_parser=numf_parser, required=true)]
|
#[clap(value_parser=numf_parser::<NumberType>, required=true)]
|
||||||
/// at least one number that should be formatted
|
/// at least one number that should be formatted
|
||||||
///
|
///
|
||||||
/// supports either base 10 or base 16 inputs (with 0xaaaa)
|
/// supports either base 10 or base 16 inputs (with 0xaaaa)
|
||||||
|
@ -238,8 +238,14 @@ impl Format {
|
||||||
/// let args = Args::parse_from(&["", "-a", "0x10"]);
|
/// let args = Args::parse_from(&["", "-a", "0x10"]);
|
||||||
/// assert_eq!(args.address, 16);
|
/// assert_eq!(args.address, 16);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn numf_parser(s: &str) -> anyhow::Result<NumberType> {
|
pub fn numf_parser<T>(s: &str) -> anyhow::Result<T>
|
||||||
if s.starts_with(&Format::Dec.prefix()) || s.parse::<NumberType>().is_ok() {
|
where
|
||||||
|
T: std::str::FromStr,
|
||||||
|
<T as std::str::FromStr>::Err: std::fmt::Display,
|
||||||
|
T: num::Num,
|
||||||
|
<T as num::Num>::FromStrRadixErr: std::fmt::Display,
|
||||||
|
{
|
||||||
|
if s.starts_with(&Format::Dec.prefix()) || s.parse::<T>().is_ok() {
|
||||||
let s = match s.strip_prefix(&Format::Dec.prefix()) {
|
let s = match s.strip_prefix(&Format::Dec.prefix()) {
|
||||||
Some(sr) => sr,
|
Some(sr) => sr,
|
||||||
None => s,
|
None => s,
|
||||||
|
@ -256,7 +262,7 @@ pub fn numf_parser(s: &str) -> anyhow::Result<NumberType> {
|
||||||
Some(sr) => sr,
|
Some(sr) => sr,
|
||||||
None => s,
|
None => s,
|
||||||
};
|
};
|
||||||
match NumberType::from_str_radix(s, 16) {
|
match T::from_str_radix(s, 16) {
|
||||||
Ok(r) => Ok(r),
|
Ok(r) => Ok(r),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let e = format!("{e}");
|
let e = format!("{e}");
|
||||||
|
@ -268,7 +274,7 @@ pub fn numf_parser(s: &str) -> anyhow::Result<NumberType> {
|
||||||
Some(sr) => sr,
|
Some(sr) => sr,
|
||||||
None => s,
|
None => s,
|
||||||
};
|
};
|
||||||
match NumberType::from_str_radix(s, 8) {
|
match T::from_str_radix(s, 8) {
|
||||||
Ok(r) => Ok(r),
|
Ok(r) => Ok(r),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let e = format!("{e}");
|
let e = format!("{e}");
|
||||||
|
@ -280,7 +286,7 @@ pub fn numf_parser(s: &str) -> anyhow::Result<NumberType> {
|
||||||
Some(sr) => sr,
|
Some(sr) => sr,
|
||||||
None => s,
|
None => s,
|
||||||
};
|
};
|
||||||
match NumberType::from_str_radix(s, 2) {
|
match T::from_str_radix(s, 2) {
|
||||||
Ok(r) => Ok(r),
|
Ok(r) => Ok(r),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let e = format!("{e}");
|
let e = format!("{e}");
|
||||||
|
|
Reference in New Issue