Skip to content

Commit

Permalink
Fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Oct 12, 2021
1 parent 3a890f5 commit 888edc5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
29 changes: 22 additions & 7 deletions provider/uprops/src/enum_codepointtrie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,38 @@ impl<T: TrieValue> TryFrom<uprops_serde::enumerated::EnumeratedPropertyCodePoint
null_value: cpt_data.null_value,
trie_type: trie_type_enum,
};
let index: ZeroVec<u16> = ZeroVec::from_slice(&cpt_data.index);
let index: ZeroVec<u16> = ZeroVec::clone_from_slice(&cpt_data.index);
// TODO: make data have type ZeroVec<T>
let data = if let Some(data_8) = cpt_data.data_8 {
ZeroVec::from_slice(data_8.as_slice())
//
let data: Result<Vec<T::ULE>, String> = if let Some(data_8) = cpt_data.data_8 {
data_8
.iter()
.map(|i| *i as u32)
.map(|i| T::parse_from_u32(i).map(|i| i.as_unaligned()))
.collect()
} else if let Some(data_16) = cpt_data.data_16 {
ZeroVec::from_slice(data_16.as_slice())
data_16
.iter()
.map(|i| *i as u32)
.map(|i| T::parse_from_u32(i).map(|i| i.as_unaligned()))
.collect()
} else if let Some(data_32) = cpt_data.data_32 {
ZeroVec::from_slice(data_32.as_slice())
data_32
.iter()
.map(|i| *i as u32)
.map(|i| T::parse_from_u32(i).map(|i| i.as_unaligned()))
.collect()
} else {
return Err(DataError::new_resc_error(
icu_codepointtrie::error::Error::FromDeserialized {
reason: "Cannot deserialize data array for CodePointTrie in TOML",
},
));
};
let trie =
CodePointTrie::<T>::try_new(header, index, data).map_err(DataError::new_resc_error);

let data = ZeroVec::Owned(data.map_err(DataError::new_resc_error)?);
let trie = CodePointTrie::<T>::try_new(header, index, data)
.map_err(DataError::new_resc_error);
trie.map(|t| UnicodePropertyMapV1 { codepoint_trie: t })
}
}
Expand Down
12 changes: 11 additions & 1 deletion utils/codepointtrie/src/codepointtrie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::error::Error;
use crate::impl_const::*;

use core::convert::TryFrom;
use core::convert::{TryFrom, TryInto};
use icu_provider::yoke::ZeroCopyFrom;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -33,18 +33,28 @@ pub enum TrieType {
/// This trait is used as a type parameter in constructing a `CodePointTrie`.
pub trait TrieValue: Copy + zerovec::ule::AsULE + 'static {
const DATA_GET_ERROR_VALUE: Self;
fn parse_from_u32(i: u32) -> Result<Self, String>;
}

impl TrieValue for u8 {
const DATA_GET_ERROR_VALUE: u8 = u8::MAX;
fn parse_from_u32(i: u32) -> Result<Self, String> {
Self::try_from(i).map_err(|e| e.to_string())
}
}

impl TrieValue for u16 {
const DATA_GET_ERROR_VALUE: u16 = u16::MAX;
fn parse_from_u32(i: u32) -> Result<Self, String> {
Self::try_from(i).map_err(|e| e.to_string())
}
}

impl TrieValue for u32 {
const DATA_GET_ERROR_VALUE: u32 = u32::MAX;
fn parse_from_u32(i: u32) -> Result<Self, String> {
Self::try_from(i).map_err(|e| e.to_string())
}
}

/// This struct represents a de-serialized CodePointTrie that was exported from
Expand Down

0 comments on commit 888edc5

Please sign in to comment.