add parser for all formats #13

Merged
PlexSheep merged 11 commits from feat/parse-formats into devel 2024-05-13 15:54:23 +02:00
2 changed files with 15 additions and 8 deletions
Showing only changes of commit 6649470ff7 - Show all commits

View File

@ -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"
PlexSheep marked this conversation as resolved
Review

remove num if it's not needed

remove num if it's not needed
Review

yup we need that

yup we need that

View File

@ -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>
PlexSheep marked this conversation as resolved Outdated

Make this function generic for all unsigned integer types

Make this function generic for all unsigned integer types
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),
PlexSheep marked this conversation as resolved Outdated

add functionalits for base64

add functionalits for base64
Err(e) => { Err(e) => {
let e = format!("{e}"); let e = format!("{e}");