Skip to content

Commit

Permalink
Adding ErasedDataProvider; renaming DataKey, DataEntry, and other traits
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Jan 5, 2021
1 parent 03ae831 commit 8a8893b
Show file tree
Hide file tree
Showing 57 changed files with 2,931 additions and 1,522 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/datetime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ icu_provider = { version = "0.1", path = "../provider" }

[dev-dependencies]
criterion = "0.3"
icu_provider = { version = "0.1", path = "../provider", features = ["invariant"] }
icu_provider = { version = "0.1", path = "../provider" }
icu_testdata = { version = "0.1", path = "../../resources/testdata" }
icu_locid_macros = { version = "0.1", path = "../locid/macros" }
serde = { version = "1.0", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::fmt;
/// # use icu_locid_macros::langid;
/// # use icu_datetime::{DateTimeFormat, DateTimeFormatOptions};
/// # use icu_datetime::date::MockDateTime;
/// # use icu_provider::InvariantDataProvider;
/// # use icu_provider::inv::InvariantDataProvider;
/// # let lid = langid!("en");
/// # let provider = InvariantDataProvider;
/// # let options = DateTimeFormatOptions::default();
Expand Down
34 changes: 16 additions & 18 deletions components/datetime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ use std::borrow::Cow;
/// use icu_locid_macros::langid;
/// use icu_datetime::{DateTimeFormat, options::style};
/// use icu_datetime::date::MockDateTime;
/// use icu_provider::InvariantDataProvider;
/// use icu_provider::inv::InvariantDataProvider;
///
/// let lid = langid!("en");
///
Expand Down Expand Up @@ -122,7 +122,7 @@ impl<'d> DateTimeFormat<'d> {
/// use icu_locid_macros::langid;
/// use icu_datetime::{DateTimeFormat, DateTimeFormatOptions};
/// use icu_datetime::date::MockDateTime;
/// use icu_provider::InvariantDataProvider;
/// use icu_provider::inv::InvariantDataProvider;
///
/// let lid = langid!("en");
///
Expand All @@ -134,24 +134,22 @@ impl<'d> DateTimeFormat<'d> {
///
/// assert_eq!(dtf.is_ok(), true);
/// ```
pub fn try_new<D: DataProvider<'d>>(
pub fn try_new<D: DataProvider<'d, structs::dates::gregory::DatesV1> + ?Sized>(
langid: LanguageIdentifier,
data_provider: &D,
options: &DateTimeFormatOptions,
) -> Result<Self, DateTimeFormatError> {
let data_key = structs::dates::key::GREGORY_V1;
let mut receiver = DataReceiverForType::<structs::dates::gregory::DatesV1>::new();
data_provider.load_to_receiver(
&DataRequest {
data_key,
data_entry: DataEntry {
variant: None,
langid: langid.clone(),
let data = data_provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: structs::dates::key::GREGORY_V1,
options: ResourceOptions {
variant: None,
langid: Some(langid.clone()),
},
},
},
&mut receiver,
)?;
let data = receiver.payload.expect("Load was successful");
})?
.take_payload()?;

let pattern = data.get_pattern_for_options(options)?.unwrap_or_default();

Expand All @@ -171,7 +169,7 @@ impl<'d> DateTimeFormat<'d> {
/// # use icu_locid_macros::langid;
/// # use icu_datetime::{DateTimeFormat, DateTimeFormatOptions};
/// # use icu_datetime::date::MockDateTime;
/// # use icu_provider::InvariantDataProvider;
/// # use icu_provider::inv::InvariantDataProvider;
/// # let lid = langid!("en");
/// # let provider = InvariantDataProvider;
/// # let options = DateTimeFormatOptions::default();
Expand Down Expand Up @@ -209,7 +207,7 @@ impl<'d> DateTimeFormat<'d> {
/// # use icu_locid_macros::langid;
/// # use icu_datetime::{DateTimeFormat, DateTimeFormatOptions};
/// # use icu_datetime::date::MockDateTime;
/// # use icu_provider::InvariantDataProvider;
/// # use icu_provider::inv::InvariantDataProvider;
/// # let lid = langid!("en");
/// # let provider = InvariantDataProvider;
/// # let options = DateTimeFormatOptions::default();
Expand Down Expand Up @@ -241,7 +239,7 @@ impl<'d> DateTimeFormat<'d> {
/// # use icu_locid_macros::langid;
/// # use icu_datetime::{DateTimeFormat, DateTimeFormatOptions};
/// # use icu_datetime::date::MockDateTime;
/// # use icu_provider::InvariantDataProvider;
/// # use icu_provider::inv::InvariantDataProvider;
/// # let lid = langid!("en");
/// # let provider = InvariantDataProvider;
/// # let options = DateTimeFormatOptions::default();
Expand Down
4 changes: 2 additions & 2 deletions components/ecma402/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ include = [
[dependencies]
ecma402_traits = { version = "0.2.0" }
icu = { version = "0.1", path = "../icu" }
icu_provider = { version = "0.1", path = "../provider", features = ["invariant"] }
icu_provider = { version = "0.1", path = "../provider" }


[dev-dependencies]
criterion = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = {version = "1.0" }
icu_locid = { version = "0.1", path = "../locid", features = ["serde"] }
icu_provider = { version = "0.1", path = "../provider", features = ["invariant"] }
icu_provider = { version = "0.1", path = "../provider" }
icu_testdata = { version = "0.1", path = "../../resources/testdata" }
6 changes: 3 additions & 3 deletions components/ecma402/src/pluralrules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl ecma402_traits::pluralrules::PluralRules for PluralRules {
Self: Sized,
{
// TODO: introduce a global data provider here.
let dp = icu_provider::InvariantDataProvider;
let dp = icu_provider::inv::InvariantDataProvider;
Self::try_new_with_provider(l, opts, &dp)
}

Expand All @@ -254,14 +254,14 @@ impl ecma402_traits::pluralrules::PluralRules for PluralRules {

impl PluralRules {
/// Creates a new [`PluralRules`], using the specified data provider.
pub fn try_new_with_provider<L, P>(
pub fn try_new_with_provider<'d, L, P>(
l: L,
opts: ecma402_traits::pluralrules::Options,
provider: &P,
) -> Result<Self, PluralRulesError>
where
L: ecma402_traits::Locale,
P: icu_provider::DataProvider<'static>,
P: icu_provider::DataProvider<'d, icu_provider::structs::plurals::PluralRuleStringsV1<'d>>,
Self: Sized,
{
let locale: String = format!("{}", l);
Expand Down
2 changes: 1 addition & 1 deletion components/plurals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ icu_locid = { version = "0.1", path = "../locid" }

[dev-dependencies]
criterion = "0.3"
icu_provider = { version = "0.1", path = "../provider", features = ["invariant"] }
icu_provider = { version = "0.1", path = "../provider" }
icu_locid = { version = "0.1", path = "../locid", features = ["serde"] }
icu_locid_macros = { version = "0.1", path = "../locid/macros" }
icu_testdata = { version = "0.1", path = "../../resources/testdata" }
Expand Down
19 changes: 12 additions & 7 deletions components/plurals/benches/parser.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/master/LICENSE ).

mod fixtures;
mod helpers;

use criterion::{black_box, criterion_group, criterion_main, Criterion};

use icu_provider::{structs, DataEntry, DataProvider, DataRequest};
use icu_provider::prelude::*;
use icu_provider::structs;
use std::borrow::Cow;

fn parser(c: &mut Criterion) {
Expand All @@ -19,16 +21,19 @@ fn parser(c: &mut Criterion) {
let mut rules = vec![];

for langid in &plurals_data.langs {
let response = (&provider as &dyn DataProvider)
let plurals_data: Cow<structs::plurals::PluralRuleStringsV1> = provider
.load_payload(&DataRequest {
data_key: structs::plurals::key::CARDINAL_V1,
data_entry: DataEntry {
variant: None,
langid: langid.clone(),
resource_path: ResourcePath {
key: structs::plurals::key::CARDINAL_V1,
options: ResourceOptions {
variant: None,
langid: Some(langid.clone()),
},
},
})
.unwrap()
.take_payload()
.unwrap();
let plurals_data: Cow<structs::plurals::PluralRuleStringsV1> = response.payload.unwrap();

let r = &[
&plurals_data.zero,
Expand Down
2 changes: 1 addition & 1 deletion components/plurals/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn parse_rule(input: &Option<Cow<str>>) -> Result<Option<ast::Condition>, Plural
})
}

impl TryInto<PluralRuleList> for &PluralRuleStringsV1 {
impl<'s> TryInto<PluralRuleList> for &PluralRuleStringsV1<'s> {
type Error = PluralRulesError;
fn try_into(self) -> Result<PluralRuleList, Self::Error> {
Ok(PluralRuleList {
Expand Down
35 changes: 17 additions & 18 deletions components/plurals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub enum PluralRuleType {
/// ```
/// use icu_locid_macros::langid;
/// use icu_plurals::{PluralRules, PluralRuleType, PluralCategory};
/// use icu_provider::InvariantDataProvider;
/// use icu_provider::inv::InvariantDataProvider;
///
/// let lid = langid!("en");
///
Expand Down Expand Up @@ -225,7 +225,7 @@ impl PluralCategory {
/// ```
/// use icu_locid_macros::langid;
/// use icu_plurals::{PluralRules, PluralRuleType, PluralCategory};
/// use icu_provider::InvariantDataProvider;
/// use icu_provider::inv::InvariantDataProvider;
///
/// let lid = langid!("en");
///
Expand Down Expand Up @@ -255,7 +255,7 @@ impl PluralRules {
/// ```
/// use icu_locid_macros::langid;
/// use icu_plurals::{PluralRules, PluralRuleType};
/// use icu_provider::InvariantDataProvider;
/// use icu_provider::inv::InvariantDataProvider;
///
/// let lid = langid!("en");
///
Expand All @@ -266,27 +266,26 @@ impl PluralRules {
///
/// [`type`]: PluralRuleType
/// [`data provider`]: icu_provider::DataProvider
pub fn try_new<'d, D: DataProvider<'d>>(
pub fn try_new<'d, D: DataProvider<'d, structs::plurals::PluralRuleStringsV1<'d>> + ?Sized>(
langid: LanguageIdentifier,
data_provider: &D,
type_: PluralRuleType,
) -> Result<Self, PluralRulesError> {
let data_key = match type_ {
let key = match type_ {
PluralRuleType::Cardinal => structs::plurals::key::CARDINAL_V1,
PluralRuleType::Ordinal => structs::plurals::key::ORDINAL_V1,
};
let mut receiver = DataReceiverForType::<structs::plurals::PluralRuleStringsV1>::new();
data_provider.load_to_receiver(
&DataRequest {
data_key,
data_entry: DataEntry {
variant: None,
langid: langid.clone(),
let plurals_data = data_provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key,
options: ResourceOptions {
variant: None,
langid: Some(langid.clone()),
},
},
},
&mut receiver,
)?;
let plurals_data = receiver.payload.expect("Load was successful");
})?
.take_payload()?;

let list: data::PluralRuleList = (&*plurals_data).try_into()?;

Expand All @@ -303,7 +302,7 @@ impl PluralRules {
/// ```
/// use icu_locid_macros::langid;
/// use icu_plurals::{PluralRules, PluralRuleType, PluralCategory};
/// use icu_provider::InvariantDataProvider;
/// use icu_provider::inv::InvariantDataProvider;
///
/// let lid = langid!("en");
///
Expand Down Expand Up @@ -333,7 +332,7 @@ impl PluralRules {
/// # use icu_locid_macros::langid;
/// # use icu_plurals::{PluralRules, PluralRuleType};
/// use icu_plurals::{PluralCategory, PluralOperands};
/// # use icu_provider::InvariantDataProvider;
/// # use icu_provider::inv::InvariantDataProvider;
/// #
/// # let lid = langid!("en");
/// #
Expand Down
27 changes: 27 additions & 0 deletions components/plurals/tests/plurals.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/master/LICENSE ).

use icu_locid_macros::langid;
use icu_plurals::{PluralCategory, PluralRuleType, PluralRules};
use icu_provider::struct_provider::StructProvider;
use icu_provider::structs::{self, plurals::PluralRuleStringsV1};
use std::borrow::Cow;

#[test]
fn test_plural_rules() {
Expand Down Expand Up @@ -32,3 +36,26 @@ fn test_plural_category_all() {

assert_eq!(categories.get(0), Some(&&PluralCategory::Zero));
}

#[test]
fn test_plural_rules_non_static_lifetime() {
let local_string = "v = 0 and i % 10 = 1".to_string();
let local_data = PluralRuleStringsV1 {
zero: None,
one: Some(Cow::Borrowed(&local_string)),
two: None,
few: None,
many: None,
};
let provider = StructProvider {
key: structs::plurals::key::CARDINAL_V1,
data: &local_data,
};

let lid = langid!("und");
let pr = PluralRules::try_new(lid, &provider, PluralRuleType::Cardinal).unwrap();

assert_eq!(pr.select(1_usize), PluralCategory::One);
assert_eq!(pr.select(5_usize), PluralCategory::Other);
assert_eq!(pr.select(11_usize), PluralCategory::One);
}
17 changes: 14 additions & 3 deletions components/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ include = [
"README.md"
]

[package.metadata.cargo-all-features]
skip_optional_dependencies = true

[features]
# Enable InvariantDataProvider, a locale-agnostic data provider.
invariant = []
# Serialize None values when exporting. Required for Bincode.
serialize_none = []

# Include the HelloWorldProvider, useful for unit testing.
hello_world = ["icu_locid_macros"]

[dependencies]
icu_locid = { version = "0.1", path = "../locid" }
tinystr = "0.4"
Expand All @@ -33,7 +37,14 @@ smallstr = { version = "0.2", features = ["serde"] }
downcast-rs = "1.2"
serde = { version = "1.0", features = ["derive"] }

# For feature "hello_world"
icu_locid_macros = { version = "0.1", path = "../locid/macros", optional = true }

[dev-dependencies]
serde_json = "1.0"
icu_locid_macros = { version = "0.1", path = "../locid/macros" }
static_assertions = "1.1"
icu_locid_macros = { version = "0.1", path = "../locid/macros" }

[[test]]
name = "iteration"
required-features = ["hello_world"]
Loading

0 comments on commit 8a8893b

Please sign in to comment.