Skip to content

Commit

Permalink
Split ZeroVecError into UleError and VarZeroVecFormatError (uni…
Browse files Browse the repository at this point in the history
…code-org#5404)

`UleError` is returned by `ULE`/`VarULE` implementations
(`validate_byte_slice`) and is basically the old error.

`VarZeroVecFormatError` is returned by
`VarZeroVecComponents::parse_byte_slice` and has variants `Metadata` and
`Values(UleError)`.
  • Loading branch information
robertbastian authored Aug 21, 2024
1 parent af38030 commit 96224c3
Show file tree
Hide file tree
Showing 50 changed files with 266 additions and 304 deletions.
9 changes: 4 additions & 5 deletions components/casemap/src/provider/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use alloc::collections::BTreeMap;
use core::num::TryFromIntError;
use icu_collections::codepointtrie::TrieValue;
use zerovec::ule::{AsULE, RawBytesULE, ULE};
use zerovec::ZeroVecError;
use zerovec::ule::{AsULE, RawBytesULE, UleError, ULE};

/// The case of a Unicode character
///
Expand Down Expand Up @@ -270,7 +269,7 @@ impl CaseMapData {

/// Attempt to construct from ICU-format integer
#[cfg(any(feature = "datagen", test))]
pub(crate) fn try_from_icu_integer(int: u16) -> Result<Self, ZeroVecError> {
pub(crate) fn try_from_icu_integer(int: u16) -> Result<Self, UleError> {
let raw = int.to_unaligned();
CaseMapDataULE::validate_byte_slice(raw.as_bytes())?;

Expand Down Expand Up @@ -367,7 +366,7 @@ impl CaseMapDataULE {
/// their respective safety guidelines: They have been
/// 6. The equality invariant is satisfied
unsafe impl ULE for CaseMapDataULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
let sixteens = RawBytesULE::<2>::parse_byte_slice(bytes)?;

for sixteen in sixteens {
Expand All @@ -380,7 +379,7 @@ unsafe impl ULE for CaseMapDataULE {
// uncased
if sixteen >> Self::DELTA_SHIFT != 0 {
// We have some used bits in the reserved zone!
return Err(ZeroVecError::parse::<Self>());
return Err(UleError::parse::<Self>());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions components/collections/src/codepointtrie/cptrie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use core::num::TryFromIntError;
use core::ops::RangeInclusive;
use yoke::Yokeable;
use zerofrom::ZeroFrom;
use zerovec::ule::UleError;
use zerovec::ZeroVec;
use zerovec::ZeroVecError;

/// The type of trie represents whether the trie has an optimization that
/// would make it smaller or faster.
Expand Down Expand Up @@ -431,7 +431,7 @@ impl<'trie, T: TrieValue> CodePointTrie<'trie, T> {
///
/// assert_eq!(planes_trie_i8.get32(0x30000), 3);
/// ```
pub fn try_into_converted<P>(self) -> Result<CodePointTrie<'trie, P>, ZeroVecError>
pub fn try_into_converted<P>(self) -> Result<CodePointTrie<'trie, P>, UleError>
where
P: TrieValue,
{
Expand Down
8 changes: 4 additions & 4 deletions components/datetime/src/fields/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use core::cmp::{Ord, PartialOrd};
use core::fmt;
use displaydoc::Display;
use zerovec::ule::{AsULE, ZeroVecError, ULE};
use zerovec::ule::{AsULE, UleError, ULE};

/// An error relating to the length of a field within a date pattern.
#[derive(Display, Debug, PartialEq, Copy, Clone)]
Expand Down Expand Up @@ -164,10 +164,10 @@ impl AsULE for FieldLength {

impl FieldLengthULE {
#[inline]
pub(crate) fn validate_byte(byte: u8) -> Result<(), ZeroVecError> {
pub(crate) fn validate_byte(byte: u8) -> Result<(), UleError> {
FieldLength::from_idx(byte)
.map(|_| ())
.map_err(|_| ZeroVecError::parse::<FieldLength>())
.map_err(|_| UleError::parse::<FieldLength>())
}
}

Expand All @@ -181,7 +181,7 @@ impl FieldLengthULE {
// 5. All other methods must be left with their default impl.
// 6. Byte equality is semantic equality.
unsafe impl ULE for FieldLengthULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
for byte in bytes {
Self::validate_byte(*byte)?;
}
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Field {

impl FieldULE {
#[inline]
pub(crate) fn validate_bytes(bytes: (u8, u8)) -> Result<(), zerovec::ZeroVecError> {
pub(crate) fn validate_bytes(bytes: (u8, u8)) -> Result<(), zerovec::ule::UleError> {
symbols::FieldSymbolULE::validate_byte(bytes.0)?;
length::FieldLengthULE::validate_byte(bytes.1)?;
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions components/datetime/src/fields/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::fields::FieldLength;
use core::{cmp::Ordering, convert::TryFrom};
use displaydoc::Display;
use icu_provider::prelude::*;
use zerovec::ule::{AsULE, ZeroVecError, ULE};
use zerovec::ule::{AsULE, UleError, ULE};

/// An error relating to the field symbol for a date pattern field.
#[derive(Display, Debug, PartialEq, Copy, Clone)]
Expand Down Expand Up @@ -198,10 +198,10 @@ impl AsULE for FieldSymbol {

impl FieldSymbolULE {
#[inline]
pub(crate) fn validate_byte(byte: u8) -> Result<(), ZeroVecError> {
pub(crate) fn validate_byte(byte: u8) -> Result<(), UleError> {
FieldSymbol::from_idx(byte)
.map(|_| ())
.map_err(|_| ZeroVecError::parse::<FieldSymbol>())
.map_err(|_| UleError::parse::<FieldSymbol>())
}
}

Expand All @@ -215,7 +215,7 @@ impl FieldSymbolULE {
// 5. All other methods must be left with their default impl.
// 6. Byte equality is semantic equality.
unsafe impl ULE for FieldSymbolULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
for byte in bytes {
Self::validate_byte(*byte)?;
}
Expand Down
14 changes: 7 additions & 7 deletions components/datetime/src/pattern/item/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use super::{GenericPatternItem, PatternItem};
use crate::fields;
use core::convert::TryFrom;
use zerovec::ule::{AsULE, ZeroVecError, ULE};
use zerovec::ule::{AsULE, UleError, ULE};

/// `PatternItemULE` is a type optimized for efficient storing and
/// deserialization of `TypedDateTimeFormatter` `PatternItem` elements using
Expand Down Expand Up @@ -93,17 +93,17 @@ impl PatternItemULE {
// 5. The other ULE methods use the default impl.
// 6. PatternItemULE byte equality is semantic equality.
unsafe impl ULE for PatternItemULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
if bytes.len() % 3 != 0 {
return Err(ZeroVecError::length::<Self>(bytes.len()));
return Err(UleError::length::<Self>(bytes.len()));
}

#[allow(clippy::indexing_slicing)] // chunks
if !bytes
.chunks(3)
.all(|c| Self::bytes_in_range((&c[0], &c[1], &c[2])))
{
return Err(ZeroVecError::parse::<Self>());
return Err(UleError::parse::<Self>());
}
Ok(())
}
Expand Down Expand Up @@ -251,16 +251,16 @@ impl GenericPatternItemULE {
// 5. The other ULE methods use the default impl.
// 6. GenericPatternItemULE byte equality is semantic equality.
unsafe impl ULE for GenericPatternItemULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
if bytes.len() % 3 != 0 {
return Err(ZeroVecError::length::<Self>(bytes.len()));
return Err(UleError::length::<Self>(bytes.len()));
}
#[allow(clippy::indexing_slicing)] // chunks
if !bytes
.chunks_exact(3)
.all(|c| Self::bytes_in_range((&c[0], &c[1], &c[2])))
{
return Err(ZeroVecError::parse::<Self>());
return Err(UleError::parse::<Self>());
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::dimension::provider::currency_compact::CompactCount;
use icu_plurals::PluralCategory;
use zerovec::ule::{AsULE, ZeroVecError, ULE};
use zerovec::ule::{AsULE, UleError, ULE};

/// [`CompactCountULE`] is a type optimized for efficient storing and
/// deserialization of [`CompactCount`] using the `ZeroVec` model.
Expand Down Expand Up @@ -38,14 +38,14 @@ pub struct CompactCountULE(u8);
// 5. The other ULE methods use the default impl.
// 6. CompactCountULE byte equality is semantic equality.
unsafe impl ULE for CompactCountULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
for byte in bytes {
if byte & 0b0111_1000 != 0 {
return Err(ZeroVecError::parse::<Self>());
return Err(UleError::parse::<Self>());
}

if byte & 0b0000_0111 > 5 {
return Err(ZeroVecError::parse::<Self>());
return Err(UleError::parse::<Self>());
}
}

Expand Down
17 changes: 8 additions & 9 deletions components/experimental/src/dimension/provider/pattern_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

use zerovec::{
maps::ZeroMapKV,
ule::{AsULE, ULE},
ZeroVecError,
ule::{AsULE, UleError, ULE},
};

use crate::dimension::provider::units_essentials::CompoundCount;
Expand Down Expand Up @@ -81,11 +80,11 @@ pub struct PatternKeyULE(u8);
// 5. The other ULE methods use the default impl.
// 6. PatternKeyULE byte equality is semantic equality.
unsafe impl ULE for PatternKeyULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), zerovec::ZeroVecError> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), zerovec::ule::UleError> {
for &byte in bytes.iter() {
// Ensure the first two bits (b7 & b6) are not 11.
if (byte & 0b1100_0000) == 0b1100_0000 {
return Err(ZeroVecError::parse::<Self>());
return Err(UleError::parse::<Self>());
}

// For the `Power` variant:
Expand All @@ -95,17 +94,17 @@ unsafe impl ULE for PatternKeyULE {
if (byte & 0b1100_0000) == 0b1000_0000 {
// b5 must be 1
if (byte & 0b0010_0000) == 0 {
return Err(ZeroVecError::parse::<Self>());
return Err(UleError::parse::<Self>());
}

// b3 must be 0
if (byte & 0b0000_1000) != 0 {
return Err(ZeroVecError::parse::<Self>());
return Err(UleError::parse::<Self>());
}

// If b2 is 1, b1 must be 0
if (byte & 0b0000_0100) != 0 && (byte & 0b0000_0010) != 0 {
return Err(ZeroVecError::parse::<Self>());
return Err(UleError::parse::<Self>());
}
}
}
Expand Down Expand Up @@ -242,12 +241,12 @@ fn test_pattern_key_ule() {
let unvalidated_bytes = [0b1100_0000];
assert_eq!(
PatternKeyULE::validate_byte_slice(&unvalidated_bytes),
Err(ZeroVecError::parse::<PatternKeyULE>())
Err(UleError::parse::<PatternKeyULE>())
);

let unvalidated_bytes = [0b1000_0000];
assert_eq!(
PatternKeyULE::validate_byte_slice(&unvalidated_bytes),
Err(ZeroVecError::parse::<PatternKeyULE>())
Err(UleError::parse::<PatternKeyULE>())
);
}
6 changes: 3 additions & 3 deletions components/experimental/src/dimension/provider/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use zerovec::{
maps::ZeroMapKV,
ule::{AsULE, ZeroVecError, ULE},
ule::{AsULE, UleError, ULE},
};

use crate::dimension::provider::currency::{
Expand Down Expand Up @@ -44,9 +44,9 @@ pub struct CurrencyPatternConfigULE([u8; 3]);
// 5. The other ULE methods use the default impl.
// 6. CurrencyPatternConfigULE byte equality is semantic equality.
unsafe impl ULE for CurrencyPatternConfigULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
if bytes.len() % 3 != 0 {
return Err(ZeroVecError::length::<Self>(bytes.len()));
return Err(UleError::length::<Self>(bytes.len()));
}

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions components/locale_core/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,17 @@ macro_rules! impl_tinystr_subtag {
// 6. Byte equality is semantic equality.
#[cfg(feature = "zerovec")]
unsafe impl zerovec::ule::ULE for $name {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), zerovec::ZeroVecError> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), zerovec::ule::UleError> {
let it = bytes.chunks_exact(core::mem::size_of::<Self>());
if !it.remainder().is_empty() {
return Err(zerovec::ZeroVecError::length::<Self>(bytes.len()));
return Err(zerovec::ule::UleError::length::<Self>(bytes.len()));
}
for v in it {
// The following can be removed once `array_chunks` is stabilized.
let mut a = [0; core::mem::size_of::<Self>()];
a.copy_from_slice(v);
if Self::try_from_raw(a).is_err() {
return Err(zerovec::ZeroVecError::parse::<Self>());
return Err(zerovec::ule::UleError::parse::<Self>());
}
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions components/plurals/src/rules/runtime/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::{
};
use icu_provider::prelude::*;
use zerovec::{
ule::{tuple::Tuple2ULE, AsULE, ZeroVecError, ULE},
ule::{tuple::Tuple2ULE, AsULE, UleError, ULE},
{VarZeroVec, ZeroVec},
};

Expand Down Expand Up @@ -297,9 +297,9 @@ pub(crate) struct AndOrPolarityOperandULE(u8);
// 5 The other ULE methods use the default impl.
// 6. AndOrPolarityOperandULE byte equality is semantic equality.
unsafe impl ULE for AndOrPolarityOperandULE {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), ZeroVecError> {
fn validate_byte_slice(bytes: &[u8]) -> Result<(), UleError> {
for byte in bytes {
Operand::new_from_u8(byte & 0b0011_1111).ok_or_else(ZeroVecError::parse::<Self>)?;
Operand::new_from_u8(byte & 0b0011_1111).ok_or_else(UleError::parse::<Self>)?;
}
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions components/properties/src/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use core::marker::PhantomData;
use core::ops::RangeInclusive;
use icu_collections::codepointtrie::{CodePointMapRange, CodePointTrie, TrieValue};
use icu_provider::prelude::*;
use zerovec::ZeroVecError;
use zerovec::ule::UleError;

/// A wrapper around code point map data. It is returned by APIs that return Unicode
/// property data in a map-like form, ex: enumerated property value data keyed
Expand Down Expand Up @@ -72,7 +72,7 @@ impl<T: TrieValue> CodePointMapData<T> {
/// assert_eq!(gc.get('木'), GeneralCategory::OtherLetter as u8); // U+6728
/// assert_eq!(gc.get('🎃'), GeneralCategory::OtherSymbol as u8); // U+1F383 JACK-O-LANTERN
/// ```
pub fn try_into_converted<P>(self) -> Result<CodePointMapData<P>, ZeroVecError>
pub fn try_into_converted<P>(self) -> Result<CodePointMapData<P>, UleError>
where
P: TrieValue,
{
Expand Down
6 changes: 2 additions & 4 deletions components/properties/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use icu_collections::codepointtrie::{CodePointMapRange, CodePointTrie, TrieValue
use icu_provider::prelude::*;
use zerofrom::ZeroFrom;

use zerovec::{VarZeroVec, ZeroSlice, ZeroVecError};
use zerovec::{ule::UleError, VarZeroVec, ZeroSlice};

#[cfg(feature = "compiled_data")]
#[derive(Debug)]
Expand Down Expand Up @@ -674,9 +674,7 @@ impl<'data, T: TrieValue> PropertyCodePointMapV1<'data, T> {
}

#[inline]
pub(crate) fn try_into_converted<P>(
self,
) -> Result<PropertyCodePointMapV1<'data, P>, ZeroVecError>
pub(crate) fn try_into_converted<P>(self) -> Result<PropertyCodePointMapV1<'data, P>, UleError>
where
P: TrieValue,
{
Expand Down
Loading

0 comments on commit 96224c3

Please sign in to comment.