From e347b50614ca8813cc562939d548ce7fb311b8bc Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 1 Nov 2024 17:54:19 +1100 Subject: [PATCH] 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") } --- units/src/parse.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/units/src/parse.rs b/units/src/parse.rs index 52ea0e74b..9e670c5bb 100644 --- a/units/src/parse.rs +++ b/units/src/parse.rs @@ -2,7 +2,6 @@ //! Parsing utilities. -use alloc::string::String; use core::fmt; 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") { Ok(checked) } 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. pub fn hex_check_unprefixed(s: &str) -> Result<&str, UnprefixedHexError> { if s.starts_with("0x") || s.starts_with("0X") { - return Err(ContainsPrefixError::new(s.into()).into()); + return Err(ContainsPrefixError::new(s).into()); } Ok(s) } @@ -375,17 +374,17 @@ impl From for UnprefixedHexError { /// Error returned when a hex string is missing a prefix (e.g. `0x`). #[derive(Debug, Clone, Eq, PartialEq)] pub struct MissingPrefixError { - hex: String, + hex: InputString, } impl MissingPrefixError { /// 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 { 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). #[derive(Debug, Clone, Eq, PartialEq)] pub struct ContainsPrefixError { - hex: String, + hex: InputString, } impl ContainsPrefixError { /// 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 { 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")) } }