From 1bb0ffd6f89331b1cd85b83994f5e3deeb62522a Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 13 May 2024 15:45:38 +0200 Subject: [PATCH] refactor: use libpt to join the fast32 vecs into the integer type #13 #5 --- src/format.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/format.rs b/src/format.rs index 352d1af..a2fced6 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] // this is exported to lib.rs use anyhow::anyhow; use clap::{ArgGroup, Parser}; -use libpt::bintols::split; +use libpt::bintols::{join, split}; use num::traits::Pow; pub type NumberType = u128; @@ -240,11 +240,17 @@ impl Format { /// ``` pub fn numf_parser(s: &str) -> anyhow::Result where - T: std::str::FromStr, + T: std::str::FromStr + std::convert::TryFrom, ::Err: std::fmt::Display, T: num::Num, ::FromStrRadixErr: std::fmt::Display, ::Err: std::fmt::Debug, + u128: std::convert::From, + ::Err: std::error::Error, + >::Error: std::error::Error, + >::Error: std::marker::Send, + >::Error: std::marker::Sync, + >::Error: 'static, { if s.starts_with(&Format::Dec.prefix()) || s.parse::().is_ok() { let s = match s.strip_prefix(&Format::Dec.prefix()) { @@ -323,18 +329,7 @@ where None => s, }; match fast32::base32::RFC4648.decode_str(s) { - Ok(r) => { - if r.len() > 16 { - panic!("boom"); - } - let mut ri: u128 = 0; - for (i, e) in r.iter().rev().enumerate() { - ri += (*e as u128) * 256.pow(i as u32) as u128; - } - dbg!(ri); - dbg!(format!("{ri:#x}")); - Ok(ri.to_string().parse().unwrap()) - } + Ok(r) => Ok(join::array_to_unsigned::(&r)?), Err(e) => { let e = format!("{e}"); Err(anyhow!(e))