|
| 1 | +use num_traits::Num; |
| 2 | +use tap::Pipe; |
| 3 | + |
| 4 | +/// Defines a prefix format. |
| 5 | +#[derive(Debug)] |
| 6 | +pub struct PrefixFmt<'a> { |
| 7 | + pub prefix: &'a str, |
| 8 | + pub radix: u32, |
| 9 | +} |
| 10 | + |
| 11 | +/// '0x' prefix for hexadecimal numbers |
| 12 | +pub const HEX: PrefixFmt = PrefixFmt { |
| 13 | + prefix: "0x", |
| 14 | + radix: 16, |
| 15 | +}; |
| 16 | +/// '0o' prefix for octal numbers |
| 17 | +pub const OCT: PrefixFmt = PrefixFmt { |
| 18 | + prefix: "0o", |
| 19 | + radix: 8, |
| 20 | +}; |
| 21 | + |
| 22 | +/// '0b' prefix for binary numbers |
| 23 | +pub const BIN: PrefixFmt = PrefixFmt { |
| 24 | + prefix: "0b", |
| 25 | + radix: 2, |
| 26 | +}; |
| 27 | + |
| 28 | +/// '' prefix for decimal numbers |
| 29 | +pub const DEC: PrefixFmt = PrefixFmt { |
| 30 | + prefix: "", |
| 31 | + radix: 10, |
| 32 | +}; |
| 33 | + |
| 34 | +/// Trait for parsing prefixed numbers |
| 35 | +pub trait PrefixParse { |
| 36 | + /// Parse a number prefixed with `0x`, `0o`, and `0b` |
| 37 | + /// |
| 38 | + /// # Example |
| 39 | + /// ```rust |
| 40 | + /// use prefix_parse::PrefixParse; |
| 41 | + /// |
| 42 | + /// assert_eq!(u32::parse("0x10"), Ok(16)); |
| 43 | + /// assert_eq!(u32::parse("0o10"), Ok(8)); |
| 44 | + /// assert_eq!(u32::parse("0b10"), Ok(2)); |
| 45 | + /// assert_eq!(u32::parse("10"), Ok(10)); |
| 46 | + /// ``` |
| 47 | + fn parse(src: &str) -> Result<Self, ParseError<Self>> |
| 48 | + where |
| 49 | + Self: Sized + Num, |
| 50 | + { |
| 51 | + // SAFETY: if src is a valid UTF-8 string, and we strip no multibyte characters from the start, |
| 52 | + // then the remaining string will be valid UTF-8 |
| 53 | + match src.as_bytes() { |
| 54 | + [b'0', b'x', rest @ ..] => { |
| 55 | + Self::from_str_radix(unsafe { str::from_utf8_unchecked(rest) }, 16) |
| 56 | + .map_err(ParseError::RadixParseFailed) |
| 57 | + } |
| 58 | + [b'0', b'o', rest @ ..] => { |
| 59 | + Self::from_str_radix(unsafe { str::from_utf8_unchecked(rest) }, 8) |
| 60 | + .map_err(ParseError::RadixParseFailed) |
| 61 | + } |
| 62 | + [b'0', b'b', rest @ ..] => { |
| 63 | + Self::from_str_radix(unsafe { str::from_utf8_unchecked(rest) }, 2) |
| 64 | + .map_err(ParseError::RadixParseFailed) |
| 65 | + } |
| 66 | + _ => Self::from_str_radix(src, 10).map_err(ParseError::RadixParseFailed), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Parse a number with a custom prefix |
| 71 | + /// |
| 72 | + /// # Example |
| 73 | + /// ``` |
| 74 | + /// use prefix_parse::{PrefixParse, ParseError, PrefixFmt, HEX}; |
| 75 | + /// |
| 76 | + /// assert_eq!(u32::parse_with(&HEX, "0x10"), Ok(16)); |
| 77 | + /// |
| 78 | + /// let custom_fmt = PrefixFmt { |
| 79 | + /// prefix: "0z", |
| 80 | + /// radix: 36, |
| 81 | + /// }; |
| 82 | + /// assert_eq!(u32::parse_with(&custom_fmt, "0z1jz"), Ok(2015)); |
| 83 | + /// ``` |
| 84 | + fn parse_with(fmt: &PrefixFmt, src: &str) -> Result<Self, ParseError<Self>> |
| 85 | + where |
| 86 | + Self: Sized + Num, |
| 87 | + { |
| 88 | + src.strip_prefix(fmt.prefix) |
| 89 | + .ok_or(ParseError::NoPrefixMatch)? |
| 90 | + .pipe(|rest| Self::from_str_radix(rest, fmt.radix)) |
| 91 | + .map_err(ParseError::RadixParseFailed) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// Implementation for all number types that implement the `Num` interface. |
| 96 | +impl<T: Num> PrefixParse for T {} |
| 97 | + |
| 98 | +/// Error type for `PrefixParse` |
| 99 | +#[derive(Debug, PartialEq, Eq, thiserror::Error)] |
| 100 | +pub enum ParseError<T: Num> { |
| 101 | + #[error("No Prefix Match")] |
| 102 | + NoPrefixMatch, |
| 103 | + #[error(transparent)] |
| 104 | + RadixParseFailed(T::FromStrRadixErr), |
| 105 | +} |
0 commit comments