Use InputString in hex prefix error types

In an effort to reduce requirement for `alloc`; remove the `String` and
use `InputString` in the hex prefix related error types.

Tested with:

(Note in test code we have to use `"cafe".to_owned()` before this patch
is applied.)

rust```
    #[test]
    fn hex_prefix_errors() {
        let err = hex_remove_prefix("cafe").unwrap_err();
        std::println!("{}", err);
        std::println!("{:?}", err);

        let err = hex_check_unprefixed("0xcafe").unwrap_err();
        std::println!("{}", err);
        std::println!("{:?}", err);
    }
```

Before:

hex string is missing a prefix (e.g. 0x): cafe
MissingPrefixError { hex: "cafe" }
hex string contains a prefix: 0xcafe
ContainsPrefixError { hex: "0xcafe" }

After:

failed to parse 'cafe' as hex because it is missing the '0x' prefix
MissingPrefixError { hex: InputString("cafe") }
failed to parse '0xcafe' as hex because it contains the '0x' prefix
ContainsPrefixError { hex: InputString("0xcafe") }
This commit is contained in:
Tobin C. Harding 2024-11-01 17:54:19 +11:00
parent ff20249bdc
commit e347b50614
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 8 additions and 9 deletions

View File

@ -2,7 +2,6 @@
//! Parsing utilities. //! Parsing utilities.
use alloc::string::String;
use core::fmt; use core::fmt;
use core::str::FromStr; use core::str::FromStr;
@ -167,7 +166,7 @@ pub fn hex_remove_prefix(s: &str) -> Result<&str, PrefixedHexError> {
} else if let Some(checked) = s.strip_prefix("0X") { } else if let Some(checked) = s.strip_prefix("0X") {
Ok(checked) Ok(checked)
} else { } else {
Err(MissingPrefixError::new(s.into()).into()) Err(MissingPrefixError::new(s).into())
} }
} }
@ -178,7 +177,7 @@ pub fn hex_remove_prefix(s: &str) -> Result<&str, PrefixedHexError> {
/// If the input string contains a prefix. /// If the input string contains a prefix.
pub fn hex_check_unprefixed(s: &str) -> Result<&str, UnprefixedHexError> { pub fn hex_check_unprefixed(s: &str) -> Result<&str, UnprefixedHexError> {
if s.starts_with("0x") || s.starts_with("0X") { if s.starts_with("0x") || s.starts_with("0X") {
return Err(ContainsPrefixError::new(s.into()).into()); return Err(ContainsPrefixError::new(s).into());
} }
Ok(s) Ok(s)
} }
@ -375,17 +374,17 @@ impl From<ParseIntError> for UnprefixedHexError {
/// Error returned when a hex string is missing a prefix (e.g. `0x`). /// Error returned when a hex string is missing a prefix (e.g. `0x`).
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct MissingPrefixError { pub struct MissingPrefixError {
hex: String, hex: InputString,
} }
impl MissingPrefixError { impl MissingPrefixError {
/// Creates an error from the string with the missing prefix. /// Creates an error from the string with the missing prefix.
pub(crate) fn new(hex: String) -> Self { Self { hex } } pub(crate) fn new(hex: &str) -> Self { Self { hex: hex.into() } }
} }
impl fmt::Display for MissingPrefixError { impl fmt::Display for MissingPrefixError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "hex string is missing a prefix (e.g. 0x): {}", self.hex) write!(f, "{} because it is missing the '0x' prefix", self.hex.display_cannot_parse("hex"))
} }
} }
@ -395,17 +394,17 @@ impl std::error::Error for MissingPrefixError {}
/// Error when hex string contains a prefix (e.g. 0x). /// Error when hex string contains a prefix (e.g. 0x).
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct ContainsPrefixError { pub struct ContainsPrefixError {
hex: String, hex: InputString,
} }
impl ContainsPrefixError { impl ContainsPrefixError {
/// Creates an error from the string that contains the prefix. /// Creates an error from the string that contains the prefix.
pub(crate) fn new(hex: String) -> Self { Self { hex } } pub(crate) fn new(hex: &str) -> Self { Self { hex: hex.into() } }
} }
impl fmt::Display for ContainsPrefixError { impl fmt::Display for ContainsPrefixError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "hex string contains a prefix: {}", self.hex) write!(f, "{} because it contains the '0x' prefix", self.hex.display_cannot_parse("hex"))
} }
} }