Skip to content

Commit

Permalink
Error update part 4: Rename the remaining types for consistency
Browse files Browse the repository at this point in the history
* InvalidUtf16FirstUnit -> Utf16FirstUnitError
* InvalidUtf16Array -> Utf16ArrayError
* InvalidUtf16Slice -> Utf16SliceError
* InvalidUtf16Tuple -> Utf16TupleError

Mostly search & replace.

The naming is still a bit inconsistent in that Utf*Error has the
opposite meaning of NonAsciiError, NonBmpError and EmptyStrError.
But those are empty structs with no variant to tell what's wrong,
so it's not entirely arbitrary.

I also considered renaming CodepointError back to InvalidCodepont
to get consistency of the enums that way,
but there's already Utf16PairError so I decided to complete
what I started when I created that one.
  • Loading branch information
tormol committed Aug 7, 2022
1 parent c0593e9 commit 51c9dcb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 42 deletions.
8 changes: 4 additions & 4 deletions src/decoding_iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
//! well with other adaptors,
//! while the slice iterators yield both to make more advanced use cases easy.
use crate::errors::{InvalidUtf16FirstUnit, Utf16PairError, Utf8Error};
use crate::errors::InvalidUtf16Slice::*;
use crate::errors::{Utf16FirstUnitError, Utf16PairError, Utf8Error};
use crate::errors::Utf16SliceError::*;
use crate::errors::Utf16PairError::*;
use crate::errors::Utf8ErrorKind::*;
use crate::utf8_char::Utf8Char;
Expand Down Expand Up @@ -381,7 +381,7 @@ impl<B:Borrow<u16>, I:Iterator<Item=B>> Iterator for Utf16CharMerger<B,I> {
Ok(false) => Ok(Utf16Char::from_array_unchecked([*first.borrow(), 0])),
Ok(true) => match self.iter.next() {
Some(second) => match second.borrow().utf16_needs_extra_unit() {
Err(InvalidUtf16FirstUnit) => Ok(Utf16Char::from_tuple_unchecked((
Err(Utf16FirstUnitError) => Ok(Utf16Char::from_tuple_unchecked((
*first.borrow(),
Some(*second.borrow())
))),
Expand All @@ -392,7 +392,7 @@ impl<B:Borrow<u16>, I:Iterator<Item=B>> Iterator for Utf16CharMerger<B,I> {
},
None => Err(Utf16PairError::Incomplete)
},
Err(InvalidUtf16FirstUnit) => Err(Utf16PairError::UnexpectedTrailingSurrogate),
Err(Utf16FirstUnitError) => Err(Utf16PairError::UnexpectedTrailingSurrogate),
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ macro_rules! single_cause {($(#[$doc:meta])* $err:ident => $desc:expr) => {
single_cause!{
/// Error returned by [`U16UtfExt::utf16_needs_extra_unit()`](../trait.U16UtfExt.html#tymethod.utf16_needs_extra_unit)
/// when called on an `u16` that's a trailing surrogate.
InvalidUtf16FirstUnit => "is a trailing surrogate"
Utf16FirstUnitError => "is a trailing surrogate"
}

single_cause!{
Expand Down Expand Up @@ -109,7 +109,7 @@ impl CodepointError {

simple!{
/// Error returned when an `[u16; 2]` doesn't form a valid UTF-16 codepoint.
InvalidUtf16Array {
Utf16ArrayError {
/// The first element is a trailing / low surrogate, which is never valid.
FirstIsTrailingSurrogate => "the first element is a trailing surrogate",
/// The second element is needed, but is not a trailing surrogate.
Expand All @@ -122,7 +122,7 @@ simple!{
/// They are returned in sinking precedence;
/// The condition that causes the first variant to be returned is checked
/// for before the condition the next variant is returned for.
InvalidUtf16Tuple {
Utf16TupleError {
/// The first unit is a trailing / low surrogate, which is never valid.
FirstIsTrailingSurrogate => "the first unit is a trailing surrogate",
/// The provided second unit is not necessary.
Expand All @@ -136,7 +136,7 @@ simple!{

simple!{
/// Error returned when a slice of `u16`s doesn't start with valid UTF-16.
InvalidUtf16Slice {
Utf16SliceError {
/// The slice is empty.
EmptySlice => "the slice is empty",
/// The first unit is a trailing surrogate.
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ pub mod error {// keeping the public interface in one file
pub use crate::errors::{FromStrError, EmptyStrError};
pub use crate::errors::{CodepointError, NonAsciiError, NonBmpError};
pub use crate::errors::{Utf8Error, Utf8ErrorKind};
pub use crate::errors::{InvalidUtf16Slice, InvalidUtf16Array, InvalidUtf16Tuple};
pub use crate::errors::{InvalidUtf16FirstUnit, Utf16PairError};
pub use crate::errors::{Utf16SliceError, Utf16ArrayError, Utf16TupleError};
pub use crate::errors::{Utf16FirstUnitError, Utf16PairError};
}

pub mod iterator {
Expand Down
28 changes: 14 additions & 14 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub trait U16UtfExt {
///
/// Returns `Err` for trailing surrogates, `Ok(true)` for leading surrogates,
/// and `Ok(false)` for others.
fn utf16_needs_extra_unit(self) -> Result<bool,InvalidUtf16FirstUnit>;
fn utf16_needs_extra_unit(self) -> Result<bool,Utf16FirstUnitError>;

/// Does this `u16` need another `u16` to complete a codepoint?
/// Returns `(self & 0xfc00) == 0xd800`
Expand All @@ -134,12 +134,12 @@ pub trait U16UtfExt {
}
impl U16UtfExt for u16 {
#[inline]
fn utf16_needs_extra_unit(self) -> Result<bool,InvalidUtf16FirstUnit> {
fn utf16_needs_extra_unit(self) -> Result<bool,Utf16FirstUnitError> {
match self {
// https://en.wikipedia.org/wiki/UTF-16#U.2B10000_to_U.2B10FFFF
0x00_00..=0xd7_ff | 0xe0_00..=0xff_ff => Ok(false),
0xd8_00..=0xdb_ff => Ok(true),
_ => Err(InvalidUtf16FirstUnit)
_ => Err(Utf16FirstUnitError)
}
}
#[inline]
Expand Down Expand Up @@ -245,7 +245,7 @@ pub trait CharExt: Sized {
/// and also return how many units were used.
///
/// If you want to continue after an error, continue with the next `u16` unit.
fn from_utf16_slice_start(src: &[u16]) -> Result<(Self,usize), InvalidUtf16Slice>;
fn from_utf16_slice_start(src: &[u16]) -> Result<(Self,usize), Utf16SliceError>;


/// Convert an UTF-8 sequence as returned from `.to_utf8_array()` into a `char`
Expand Down Expand Up @@ -282,19 +282,19 @@ pub trait CharExt: Sized {
///
/// ```
/// use encode_unicode::CharExt;
/// use encode_unicode::error::InvalidUtf16Array;
/// use encode_unicode::error::Utf16ArrayError;
///
/// assert_eq!(char::from_utf16_array(['x' as u16, 'y' as u16]), Ok('x'));
/// assert_eq!(char::from_utf16_array(['睷' as u16, 0]), Ok('睷'));
/// assert_eq!(char::from_utf16_array([0xda6f, 0xdcde]), Ok('\u{abcde}'));
/// assert_eq!(char::from_utf16_array([0xf111, 0xdbad]), Ok('\u{f111}'));
/// assert_eq!(char::from_utf16_array([0xdaaf, 0xdaaf]), Err(InvalidUtf16Array::SecondIsNotTrailingSurrogate));
/// assert_eq!(char::from_utf16_array([0xdcac, 0x9000]), Err(InvalidUtf16Array::FirstIsTrailingSurrogate));
/// assert_eq!(char::from_utf16_array([0xdaaf, 0xdaaf]), Err(Utf16ArrayError::SecondIsNotTrailingSurrogate));
/// assert_eq!(char::from_utf16_array([0xdcac, 0x9000]), Err(Utf16ArrayError::FirstIsTrailingSurrogate));
/// ```
fn from_utf16_array(utf16: [u16; 2]) -> Result<Self, InvalidUtf16Array>;
fn from_utf16_array(utf16: [u16; 2]) -> Result<Self, Utf16ArrayError>;

/// Convert a UTF-16 pair as returned from `.to_utf16_tuple()` into a `char`.
fn from_utf16_tuple(utf16: (u16, Option<u16>)) -> Result<Self, InvalidUtf16Tuple>;
fn from_utf16_tuple(utf16: (u16, Option<u16>)) -> Result<Self, Utf16TupleError>;


/// Convert an UTF-8 sequence into a char.
Expand Down Expand Up @@ -497,8 +497,8 @@ impl CharExt for char {
}


fn from_utf16_slice_start(src: &[u16]) -> Result<(Self,usize), InvalidUtf16Slice> {
use crate::errors::InvalidUtf16Slice::*;
fn from_utf16_slice_start(src: &[u16]) -> Result<(Self,usize), Utf16SliceError> {
use crate::errors::Utf16SliceError::*;
unsafe {match (src.get(0), src.get(1)) {
(Some(&u @ 0x00_00..=0xd7_ff), _) |
(Some(&u @ 0xe0_00..=0xff_ff), _)
Expand All @@ -512,8 +512,8 @@ impl CharExt for char {
}}
}

fn from_utf16_array(utf16: [u16;2]) -> Result<Self, InvalidUtf16Array> {
use crate::errors::InvalidUtf16Array::*;
fn from_utf16_array(utf16: [u16;2]) -> Result<Self, Utf16ArrayError> {
use crate::errors::Utf16ArrayError::*;
if let Some(c) = char::from_u32(utf16[0] as u32) {
Ok(c) // single
} else if utf16[0] < 0xdc_00 && utf16[1] & 0xfc_00 == 0xdc_00 {
Expand All @@ -525,7 +525,7 @@ impl CharExt for char {
Err(FirstIsTrailingSurrogate)
}
}
fn from_utf16_tuple(utf16: (u16, Option<u16>)) -> Result<Self, InvalidUtf16Tuple> {
fn from_utf16_tuple(utf16: (u16, Option<u16>)) -> Result<Self, Utf16TupleError> {
unsafe {
match Utf16Char::validate_tuple(utf16) {
Ok(()) => Ok(Self::from_utf16_tuple_unchecked(utf16)),
Expand Down
29 changes: 14 additions & 15 deletions src/utf16_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use crate::utf16_iterators::Utf16Iterator;
use crate::traits::{CharExt, U16UtfExt};
use crate::utf8_char::Utf8Char;
use crate::errors::{InvalidUtf16Slice, InvalidUtf16Array, InvalidUtf16Tuple};
use crate::errors::{Utf16SliceError, Utf16ArrayError, Utf16TupleError};
use crate::errors::{NonBmpError, EmptyStrError, FromStrError};
extern crate core;
use core::{hash,fmt};
Expand Down Expand Up @@ -454,7 +454,7 @@ impl Utf16Char {
}
/// Validate and store the first UTF-16 codepoint in the slice.
/// Also return how many units were needed.
pub fn from_slice_start(src: &[u16]) -> Result<(Self,usize), InvalidUtf16Slice> {
pub fn from_slice_start(src: &[u16]) -> Result<(Self,usize), Utf16SliceError> {
char::from_utf16_slice_start(src).map(|(_,len)| {
let second = if len==2 {src[1]} else {0};
(Utf16Char{ units: [src[0], second] }, len)
Expand Down Expand Up @@ -482,24 +482,24 @@ impl Utf16Char {
///
/// ```
/// use encode_unicode::Utf16Char;
/// use encode_unicode::error::InvalidUtf16Array;
/// use encode_unicode::error::Utf16ArrayError;
///
/// assert_eq!(Utf16Char::from_array(['x' as u16, 'y' as u16]), Ok(Utf16Char::from('x')));
/// assert_eq!(Utf16Char::from_array(['睷' as u16, 0]), Ok(Utf16Char::from('睷')));
/// assert_eq!(Utf16Char::from_array([0xda6f, 0xdcde]), Ok(Utf16Char::from('\u{abcde}')));
/// assert_eq!(Utf16Char::from_array([0xf111, 0xdbad]), Ok(Utf16Char::from('\u{f111}')));
/// assert_eq!(Utf16Char::from_array([0xdaaf, 0xdaaf]), Err(InvalidUtf16Array::SecondIsNotTrailingSurrogate));
/// assert_eq!(Utf16Char::from_array([0xdcac, 0x9000]), Err(InvalidUtf16Array::FirstIsTrailingSurrogate));
/// assert_eq!(Utf16Char::from_array([0xdaaf, 0xdaaf]), Err(Utf16ArrayError::SecondIsNotTrailingSurrogate));
/// assert_eq!(Utf16Char::from_array([0xdcac, 0x9000]), Err(Utf16ArrayError::FirstIsTrailingSurrogate));
/// ```
pub const fn from_array(units: [u16; 2]) -> Result<Self,InvalidUtf16Array> {
pub const fn from_array(units: [u16; 2]) -> Result<Self,Utf16ArrayError> {
if (units[0] & 0xf8_00) != 0xd8_00 {
Ok(Utf16Char { units: [units[0], 0] })
} else if units[0] < 0xdc_00 && (units[1] & 0xfc_00) == 0xdc_00 {
Ok(Utf16Char { units })
} else if units[0] < 0xdc_00 {
Err(InvalidUtf16Array::SecondIsNotTrailingSurrogate)
Err(Utf16ArrayError::SecondIsNotTrailingSurrogate)
} else {
Err(InvalidUtf16Array::FirstIsTrailingSurrogate)
Err(Utf16ArrayError::FirstIsTrailingSurrogate)
}
}
/// Create an `Utf16Char` from an array as returned from `char.to_utf16_array()`.
Expand All @@ -513,21 +513,20 @@ impl Utf16Char {
pub const unsafe fn from_array_unchecked(units: [u16; 2]) -> Self {
Utf16Char { units }
}
pub(crate) const fn validate_tuple(utf16: (u16,Option<u16>)) -> Result<(),InvalidUtf16Tuple> {
use crate::errors::InvalidUtf16Tuple::*;
pub(crate) const fn validate_tuple(utf16: (u16,Option<u16>)) -> Result<(),Utf16TupleError> {
match utf16 {
(0x00_00..=0xd7_ff, None) | // single
(0xe0_00..=0xff_ff, None) | // single
(0xd8_00..=0xdb_ff, Some(0xdc_00..=0xdf_ff)) // correct surrogate
=> Ok(()),
(0xd8_00..=0xdb_ff, Some(_)) => Err(SecondIsNotTrailingSurrogate),
(0xd8_00..=0xdb_ff, None ) => Err(MissingSecond),
(0xdc_00..=0xdf_ff, _ ) => Err(FirstIsTrailingSurrogate),
( _ , Some(_)) => Err(SuperfluousSecond),
(0xd8_00..=0xdb_ff, Some(_)) => Err(Utf16TupleError::SecondIsNotTrailingSurrogate),
(0xd8_00..=0xdb_ff, None ) => Err(Utf16TupleError::MissingSecond),
(0xdc_00..=0xdf_ff, _ ) => Err(Utf16TupleError::FirstIsTrailingSurrogate),
( _ , Some(_)) => Err(Utf16TupleError::SuperfluousSecond),
}
}
/// Validate and store a UTF-16 pair as returned from `char.to_utf16_tuple()`.
pub const fn from_tuple(utf16: (u16,Option<u16>)) -> Result<Self,InvalidUtf16Tuple> {
pub const fn from_tuple(utf16: (u16,Option<u16>)) -> Result<Self,Utf16TupleError> {
unsafe {
match Self::validate_tuple(utf16) {
Ok(()) => Ok(Self::from_tuple_unchecked(utf16)),
Expand Down
6 changes: 3 additions & 3 deletions tests/errs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn utf16_extra_unit() {
assert_eq!( (c as u16).utf16_needs_extra_unit(), match c {
0b_0000_0000_0000_0000..=0b_1101_0111_1111_1111 => Ok(false),
0b_1101_1000_0000_0000..=0b_1101_1011_1111_1111 => Ok(true),
0b_1101_1100_0000_0000..=0b_1101_1111_1111_1111 => Err(InvalidUtf16FirstUnit),
0b_1101_1100_0000_0000..=0b_1101_1111_1111_1111 => Err(Utf16FirstUnitError),
0b_1110_0000_0000_0000..=0b_1111_1111_1111_1111 => Ok(false),
_ => unreachable!(),
});
Expand All @@ -85,7 +85,7 @@ fn utf16_extra_unit() {
#[test]
#[cfg_attr(miri, ignore)]
fn from_utf16_tuple() {
use encode_unicode::error::InvalidUtf16Tuple::*;
use encode_unicode::error::Utf16TupleError::*;
for u in 0xdc00..0xe000 {
let close = if u%3==0 {u-100} else {u+100};
let doesnt_matter = if u%2==0 {Some(close)} else {None};
Expand All @@ -104,7 +104,7 @@ fn from_utf16_tuple() {
}

#[test] fn from_utf16_slice_start() {
use encode_unicode::error::InvalidUtf16Slice::*;
use encode_unicode::error::Utf16SliceError::*;
assert_eq!(char::from_utf16_slice_start(&[]), Err(EmptySlice));
let mut buf = [0; 6];
for u in 0xd800..0xdc00 {
Expand Down

0 comments on commit 51c9dcb

Please sign in to comment.