Function numf_parser

Source
pub fn numf_parser<T>(data: &[u8]) -> Result<T>
where T: FromStr + TryFrom<u128> + Num, <T as FromStr>::Err: Display + Debug + Error, <T as Num>::FromStrRadixErr: Display, u128: From<T>, <T as TryFrom<u128>>::Error: Error + Send + Sync + 'static,
Expand description

Converts any data (as bytes) into an unsigned integer value T (like u128), according to one of the Formats

If you only want to parse text data, use numf_parser_str instead.

The parser will first try to convert the data to a String.

Then, the number is assumed to be base-10 by default, it is parsed as a different Format if the number is prefixed with the prefix, for that Format. So if the user inputs 0b1100 then this is parsed as Binary and so on.

If none of the text Formats matches, the data will be assumed to be raw and converted to the ingeger type directly.

Note: Underscores will be completely ignored, as they are assumed to just be there for readability.

§Errors

If no text Format matches and the data is too long for the integer T.

§Returns

This parser will only output unsigned integers, it cannot be used with signed integers.

§Example

use numf::format::numf_parser;

let data = &[0x15, 0x92, 0xff];
let result: u64 = 0x1592ff;
assert_eq!(result, numf_parser(data).unwrap());

let data = b"0x1337";
let result: u64 = 0x1337;
assert_eq!(result, numf_parser(data).unwrap());

let data = b"0b110011";
let result: u64 = 0b110011;
assert_eq!(result, numf_parser(data).unwrap());