From 2e5009230c9d1234e68e03c63baf7ddc89aa15f8 Mon Sep 17 00:00:00 2001 From: Robert Bastian <4706271+robertbastian@users.noreply.github.com> Date: Wed, 21 Aug 2024 19:22:07 +0200 Subject: [PATCH] Rename `TinyStrError` to `ParseError` (#5405) --- utils/tinystr/src/ascii.rs | 36 ++++++++++++++++---------------- utils/tinystr/src/error.rs | 8 +++---- utils/tinystr/src/lib.rs | 7 +------ utils/tinystr/src/unvalidated.rs | 4 ++-- 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/utils/tinystr/src/ascii.rs b/utils/tinystr/src/ascii.rs index 0daf216ca5a..2b62427f149 100644 --- a/utils/tinystr/src/ascii.rs +++ b/utils/tinystr/src/ascii.rs @@ -4,7 +4,7 @@ use crate::asciibyte::AsciiByte; use crate::int_ops::{Aligned4, Aligned8}; -use crate::TinyStrError; +use crate::ParseError; use core::fmt; use core::ops::Deref; use core::str::{self, FromStr}; @@ -17,21 +17,21 @@ pub struct TinyAsciiStr { impl TinyAsciiStr { #[inline] - pub const fn try_from_str(s: &str) -> Result { + pub const fn try_from_str(s: &str) -> Result { Self::try_from_utf8(s.as_bytes()) } /// Creates a `TinyAsciiStr` from the given UTF-8 slice. /// `code_units` may contain at most `N` non-null ASCII code points. #[inline] - pub const fn try_from_utf8(code_units: &[u8]) -> Result { + pub const fn try_from_utf8(code_units: &[u8]) -> Result { Self::try_from_utf8_inner(code_units, false) } /// Creates a `TinyAsciiStr` from the given UTF-16 slice. /// `code_units` may contain at most `N` non-null ASCII code points. #[inline] - pub const fn try_from_utf16(code_units: &[u16]) -> Result { + pub const fn try_from_utf16(code_units: &[u16]) -> Result { Self::try_from_utf16_inner(code_units, 0, code_units.len(), false) } @@ -125,16 +125,16 @@ impl TinyAsciiStr { /// ); /// assert!(matches!(TinyAsciiStr::<3>::try_from_raw(*b"\0A\0"), Err(_))); /// ``` - pub const fn try_from_raw(raw: [u8; N]) -> Result { + pub const fn try_from_raw(raw: [u8; N]) -> Result { Self::try_from_utf8_inner(&raw, true) } pub(crate) const fn try_from_utf8_inner( code_units: &[u8], allow_trailing_null: bool, - ) -> Result { + ) -> Result { if code_units.len() > N { - return Err(TinyStrError::TooLarge { + return Err(ParseError::TooLong { max: N, len: code_units.len(), }); @@ -151,10 +151,10 @@ impl TinyAsciiStr { if b == 0 { found_null = true; } else if b >= 0x80 { - return Err(TinyStrError::NonAscii); + return Err(ParseError::NonAscii); } else if found_null { // Error if there are contentful bytes after null - return Err(TinyStrError::ContainsNull); + return Err(ParseError::ContainsNull); } out[i] = b; @@ -163,7 +163,7 @@ impl TinyAsciiStr { if !allow_trailing_null && found_null { // We found some trailing nulls, error - return Err(TinyStrError::ContainsNull); + return Err(ParseError::ContainsNull); } Ok(Self { @@ -177,10 +177,10 @@ impl TinyAsciiStr { start: usize, end: usize, allow_trailing_null: bool, - ) -> Result { + ) -> Result { let len = end - start; if len > N { - return Err(TinyStrError::TooLarge { max: N, len }); + return Err(ParseError::TooLong { max: N, len }); } let mut out = [0; N]; @@ -194,10 +194,10 @@ impl TinyAsciiStr { if b == 0 { found_null = true; } else if b >= 0x80 { - return Err(TinyStrError::NonAscii); + return Err(ParseError::NonAscii); } else if found_null { // Error if there are contentful bytes after null - return Err(TinyStrError::ContainsNull); + return Err(ParseError::ContainsNull); } out[i] = b as u8; @@ -206,7 +206,7 @@ impl TinyAsciiStr { if !allow_trailing_null && found_null { // We found some trailing nulls, error - return Err(TinyStrError::ContainsNull); + return Err(ParseError::ContainsNull); } Ok(Self { @@ -732,7 +732,7 @@ impl Deref for TinyAsciiStr { } impl FromStr for TinyAsciiStr { - type Err = TinyStrError; + type Err = ParseError; #[inline] fn from_str(s: &str) -> Result { Self::try_from_str(s) @@ -835,7 +835,7 @@ mod test { { let t = match TinyAsciiStr::::from_str(&s) { Ok(t) => t, - Err(TinyStrError::TooLarge { .. }) => continue, + Err(ParseError::TooLong { .. }) => continue, Err(e) => panic!("{}", e), }; let expected = reference_f(&s); @@ -845,7 +845,7 @@ mod test { let s_utf16: Vec = s.encode_utf16().collect(); let t = match TinyAsciiStr::::try_from_utf16(&s_utf16) { Ok(t) => t, - Err(TinyStrError::TooLarge { .. }) => continue, + Err(ParseError::TooLong { .. }) => continue, Err(e) => panic!("{}", e), }; let expected = reference_f(&s); diff --git a/utils/tinystr/src/error.rs b/utils/tinystr/src/error.rs index 7910f8b484e..9d8237b9bf0 100644 --- a/utils/tinystr/src/error.rs +++ b/utils/tinystr/src/error.rs @@ -5,15 +5,15 @@ use displaydoc::Display; #[cfg(feature = "std")] -impl std::error::Error for TinyStrError {} +impl std::error::Error for ParseError {} #[derive(Display, Debug, PartialEq, Eq)] #[non_exhaustive] -pub enum TinyStrError { +pub enum ParseError { #[displaydoc("found string of larger length {len} when constructing string of length {max}")] - TooLarge { max: usize, len: usize }, + TooLong { max: usize, len: usize }, #[displaydoc("tinystr types do not support strings with null bytes")] ContainsNull, - #[displaydoc("attempted to construct TinyStrAuto from a non-ascii string")] + #[displaydoc("attempted to construct TinyAsciiStr from a non-ASCII string")] NonAscii, } diff --git a/utils/tinystr/src/lib.rs b/utils/tinystr/src/lib.rs index 279d67ae772..ab1bfc4f0c6 100644 --- a/utils/tinystr/src/lib.rs +++ b/utils/tinystr/src/lib.rs @@ -87,7 +87,7 @@ mod ule; extern crate alloc; pub use ascii::TinyAsciiStr; -pub use error::TinyStrError; +pub use error::ParseError; pub use unvalidated::UnvalidatedTinyAsciiStr; /// These are temporary compatability reexports that will be removed @@ -111,8 +111,3 @@ fn test_size() { core::mem::size_of::>() ); } -// /// Allows unit tests to use the macro -// #[cfg(test)] -// mod tinystr { -// pub use super::{TinyAsciiStr, TinyStrError}; -// } diff --git a/utils/tinystr/src/unvalidated.rs b/utils/tinystr/src/unvalidated.rs index 691a6205fd0..6c9c9e8475d 100644 --- a/utils/tinystr/src/unvalidated.rs +++ b/utils/tinystr/src/unvalidated.rs @@ -2,8 +2,8 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). +use crate::ParseError; use crate::TinyAsciiStr; -use crate::TinyStrError; use core::fmt; /// A fixed-length bytes array that is expected to be an ASCII string but does not enforce that invariant. @@ -29,7 +29,7 @@ impl fmt::Debug for UnvalidatedTinyAsciiStr { impl UnvalidatedTinyAsciiStr { #[inline] /// Converts into a [`TinyAsciiStr`]. Fails if the bytes are not valid ASCII. - pub fn try_into_tinystr(&self) -> Result, TinyStrError> { + pub fn try_into_tinystr(&self) -> Result, ParseError> { TinyAsciiStr::try_from_raw(self.0) }