Skip to content

Commit

Permalink
Rename TinyStrError to ParseError (#5405)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Aug 21, 2024
1 parent 8279aa1 commit 2e50092
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
36 changes: 18 additions & 18 deletions utils/tinystr/src/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -17,21 +17,21 @@ pub struct TinyAsciiStr<const N: usize> {

impl<const N: usize> TinyAsciiStr<N> {
#[inline]
pub const fn try_from_str(s: &str) -> Result<Self, TinyStrError> {
pub const fn try_from_str(s: &str) -> Result<Self, ParseError> {
Self::try_from_utf8(s.as_bytes())
}

/// Creates a `TinyAsciiStr<N>` 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<Self, TinyStrError> {
pub const fn try_from_utf8(code_units: &[u8]) -> Result<Self, ParseError> {
Self::try_from_utf8_inner(code_units, false)
}

/// Creates a `TinyAsciiStr<N>` 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<Self, TinyStrError> {
pub const fn try_from_utf16(code_units: &[u16]) -> Result<Self, ParseError> {
Self::try_from_utf16_inner(code_units, 0, code_units.len(), false)
}

Expand Down Expand Up @@ -125,16 +125,16 @@ impl<const N: usize> TinyAsciiStr<N> {
/// );
/// assert!(matches!(TinyAsciiStr::<3>::try_from_raw(*b"\0A\0"), Err(_)));
/// ```
pub const fn try_from_raw(raw: [u8; N]) -> Result<Self, TinyStrError> {
pub const fn try_from_raw(raw: [u8; N]) -> Result<Self, ParseError> {
Self::try_from_utf8_inner(&raw, true)
}

pub(crate) const fn try_from_utf8_inner(
code_units: &[u8],
allow_trailing_null: bool,
) -> Result<Self, TinyStrError> {
) -> Result<Self, ParseError> {
if code_units.len() > N {
return Err(TinyStrError::TooLarge {
return Err(ParseError::TooLong {
max: N,
len: code_units.len(),
});
Expand All @@ -151,10 +151,10 @@ impl<const N: usize> TinyAsciiStr<N> {
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;

Expand All @@ -163,7 +163,7 @@ impl<const N: usize> TinyAsciiStr<N> {

if !allow_trailing_null && found_null {
// We found some trailing nulls, error
return Err(TinyStrError::ContainsNull);
return Err(ParseError::ContainsNull);
}

Ok(Self {
Expand All @@ -177,10 +177,10 @@ impl<const N: usize> TinyAsciiStr<N> {
start: usize,
end: usize,
allow_trailing_null: bool,
) -> Result<Self, TinyStrError> {
) -> Result<Self, ParseError> {
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];
Expand All @@ -194,10 +194,10 @@ impl<const N: usize> TinyAsciiStr<N> {
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;

Expand All @@ -206,7 +206,7 @@ impl<const N: usize> TinyAsciiStr<N> {

if !allow_trailing_null && found_null {
// We found some trailing nulls, error
return Err(TinyStrError::ContainsNull);
return Err(ParseError::ContainsNull);
}

Ok(Self {
Expand Down Expand Up @@ -732,7 +732,7 @@ impl<const N: usize> Deref for TinyAsciiStr<N> {
}

impl<const N: usize> FromStr for TinyAsciiStr<N> {
type Err = TinyStrError;
type Err = ParseError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from_str(s)
Expand Down Expand Up @@ -835,7 +835,7 @@ mod test {
{
let t = match TinyAsciiStr::<N>::from_str(&s) {
Ok(t) => t,
Err(TinyStrError::TooLarge { .. }) => continue,
Err(ParseError::TooLong { .. }) => continue,
Err(e) => panic!("{}", e),
};
let expected = reference_f(&s);
Expand All @@ -845,7 +845,7 @@ mod test {
let s_utf16: Vec<u16> = s.encode_utf16().collect();
let t = match TinyAsciiStr::<N>::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);
Expand Down
8 changes: 4 additions & 4 deletions utils/tinystr/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
7 changes: 1 addition & 6 deletions utils/tinystr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -111,8 +111,3 @@ fn test_size() {
core::mem::size_of::<Option<TinyStr8>>()
);
}
// /// Allows unit tests to use the macro
// #[cfg(test)]
// mod tinystr {
// pub use super::{TinyAsciiStr, TinyStrError};
// }
4 changes: 2 additions & 2 deletions utils/tinystr/src/unvalidated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,7 +29,7 @@ impl<const N: usize> fmt::Debug for UnvalidatedTinyAsciiStr<N> {
impl<const N: usize> UnvalidatedTinyAsciiStr<N> {
#[inline]
/// Converts into a [`TinyAsciiStr`]. Fails if the bytes are not valid ASCII.
pub fn try_into_tinystr(&self) -> Result<TinyAsciiStr<N>, TinyStrError> {
pub fn try_into_tinystr(&self) -> Result<TinyAsciiStr<N>, ParseError> {
TinyAsciiStr::try_from_raw(self.0)
}

Expand Down

0 comments on commit 2e50092

Please sign in to comment.