Skip to content

Commit

Permalink
Replace DataPayload Deref with .get() throughout ICU4X
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed May 27, 2021
1 parent 91e2fd6 commit 72c85fa
Show file tree
Hide file tree
Showing 27 changed files with 125 additions and 128 deletions.
5 changes: 3 additions & 2 deletions components/datetime/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl<'d> DateTimeFormat<'d> {
.take_payload()?;

let pattern = data
.get()
.patterns
.get_pattern_for_options(options)?
.unwrap_or_default();
Expand Down Expand Up @@ -191,7 +192,7 @@ impl<'d> DateTimeFormat<'d> {
{
FormattedDateTime {
pattern: &self.pattern,
symbols: &self.symbols,
symbols: self.symbols.get(),
datetime: value,
locale: &self.locale,
}
Expand Down Expand Up @@ -228,7 +229,7 @@ impl<'d> DateTimeFormat<'d> {
w: &mut impl std::fmt::Write,
value: &impl DateTimeInput,
) -> std::fmt::Result {
datetime::write_pattern(&self.pattern, &self.symbols, value, &self.locale, w)
datetime::write_pattern(&self.pattern, self.symbols.get(), value, &self.locale, w)
.map_err(|_| std::fmt::Error)
}

Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/format/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ mod tests {
let mut sink = String::new();
write_pattern(
&pattern,
&data.symbols,
&data.get().symbols,
&datetime,
&"und".parse().unwrap(),
&mut sink,
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/format/zoned_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
W: fmt::Write + ?Sized,
{
let pattern = &zoned_datetime_format.datetime_format.pattern;
let symbols = &zoned_datetime_format.datetime_format.symbols;
let symbols = zoned_datetime_format.datetime_format.symbols.get();

match field.symbol {
FieldSymbol::TimeZone(_time_zone) => time_zone::write_field(
Expand Down
24 changes: 12 additions & 12 deletions components/datetime/src/skeleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ mod test {
provider::{gregory::DatesV1, key::GREGORY_V1},
};

fn get_data_provider() -> DataPayload<'static, DatesV1> {
fn get_data_payload() -> DataPayload<'static, DatesV1> {
let provider = icu_testdata::get_provider();
let langid = langid!("en");
provider
Expand Down Expand Up @@ -669,10 +669,10 @@ mod test {
..Default::default()
};
let requested_fields = components.to_vec_fields();
let data_provider = get_data_provider();
let data_provider = get_data_payload();

match get_best_available_format_pattern(
&data_provider.patterns.datetime.skeletons,
&data_provider.get().patterns.datetime.skeletons,
&requested_fields,
) {
BestSkeleton::AllFieldsMatch(available_format_pattern)
Expand All @@ -696,10 +696,10 @@ mod test {
..Default::default()
};
let requested_fields = components.to_vec_fields();
let data_provider = get_data_provider();
let data_provider = get_data_payload();

match get_best_available_format_pattern(
&data_provider.patterns.datetime.skeletons,
&data_provider.get().patterns.datetime.skeletons,
&requested_fields,
) {
BestSkeleton::MissingOrExtraFields(available_format_pattern) => {
Expand All @@ -721,11 +721,11 @@ mod test {
..Default::default()
};
let requested_fields = components.to_vec_fields();
let data_provider = get_data_provider();
let data_provider = get_data_payload();

match create_best_pattern_for_fields(
&data_provider.patterns.datetime.skeletons,
&data_provider.patterns.datetime.length_patterns,
&data_provider.get().patterns.datetime.skeletons,
&data_provider.get().patterns.datetime.length_patterns,
&requested_fields,
) {
BestSkeleton::AllFieldsMatch(available_format_pattern) => {
Expand All @@ -744,11 +744,11 @@ mod test {
fn test_skeleton_empty_bag() {
let components: components::Bag = Default::default();
let requested_fields = components.to_vec_fields();
let data_provider = get_data_provider();
let data_provider = get_data_payload();

assert_eq!(
get_best_available_format_pattern(
&data_provider.patterns.datetime.skeletons,
&data_provider.get().patterns.datetime.skeletons,
&requested_fields
),
BestSkeleton::NoMatch,
Expand All @@ -765,11 +765,11 @@ mod test {
..Default::default()
};
let requested_fields = components.to_vec_fields();
let data_provider = get_data_provider();
let data_provider = get_data_payload();

assert_eq!(
get_best_available_format_pattern(
&data_provider.patterns.datetime.skeletons,
&data_provider.get().patterns.datetime.skeletons,
&requested_fields
),
BestSkeleton::NoMatch,
Expand Down
21 changes: 17 additions & 4 deletions components/datetime/src/time_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,14 @@ impl<'d> TimeZoneFormat<'d> {
&self
.exemplar_cities
.as_ref()
.map(|p| p.get())
.and_then(|cities| time_zone.time_zone_id().and_then(|id| cities.get(id)))
.map(|location| self.zone_formats.region_format.replace("{0}", location))
.map(|location| {
self.zone_formats
.get()
.region_format
.replace("{0}", location)
})
.ok_or(fmt::Error)?,
)
.map_err(DateTimeFormatError::from)
Expand All @@ -417,6 +423,7 @@ impl<'d> TimeZoneFormat<'d> {
sink.write_str(
self.mz_generic_short
.as_ref()
.map(|p| p.get())
.and_then(|metazones| time_zone.metazone_id().and_then(|mz| metazones.get(mz)))
.ok_or(fmt::Error)?,
)
Expand All @@ -434,6 +441,7 @@ impl<'d> TimeZoneFormat<'d> {
sink.write_str(
self.mz_generic_long
.as_ref()
.map(|p| p.get())
.and_then(|metazones| time_zone.metazone_id().and_then(|mz| metazones.get(mz)))
.ok_or(fmt::Error)?,
)
Expand All @@ -451,6 +459,7 @@ impl<'d> TimeZoneFormat<'d> {
sink.write_str(
self.mz_specific_short
.as_ref()
.map(|p| p.get())
.and_then(|metazones| time_zone.metazone_id().and_then(|mz| metazones.get(mz)))
.and_then(|specific_names| {
time_zone
Expand All @@ -473,6 +482,7 @@ impl<'d> TimeZoneFormat<'d> {
sink.write_str(
self.mz_specific_long
.as_ref()
.map(|p| p.get())
.and_then(|metazones| time_zone.metazone_id().and_then(|mz| metazones.get(mz)))
.and_then(|specific_names| {
time_zone
Expand All @@ -497,20 +507,21 @@ impl<'d> TimeZoneFormat<'d> {
) -> Result<(), DateTimeFormatError> {
let gmt_offset = time_zone.gmt_offset();
if gmt_offset.is_zero() {
sink.write_str(&self.zone_formats.gmt_zero_format.clone())
sink.write_str(&self.zone_formats.get().gmt_zero_format.clone())
.map_err(DateTimeFormatError::from)
} else {
// TODO(blocked on #277) Use formatter utility instead of replacing "{0}".
sink.write_str(
&self
.zone_formats
.get()
.gmt_format
.replace(
"{0}",
if gmt_offset.is_positive() {
&self.zone_formats.hour_format.0
&self.zone_formats.get().hour_format.0
} else {
&self.zone_formats.hour_format.1
&self.zone_formats.get().hour_format.1
},
)
// support all combos of "(HH|H):mm" by replacing longest patterns first.
Expand All @@ -531,6 +542,7 @@ impl<'d> TimeZoneFormat<'d> {
sink.write_str(
self.exemplar_cities
.as_ref()
.map(|p| p.get())
.and_then(|cities| time_zone.time_zone_id().and_then(|id| cities.get(id)))
.ok_or(fmt::Error)?,
)
Expand All @@ -551,6 +563,7 @@ impl<'d> TimeZoneFormat<'d> {
sink.write_str(
self.exemplar_cities
.as_ref()
.map(|p| p.get())
.and_then(|cities| cities.get("Etc/Unknown"))
.unwrap_or(&Cow::Borrowed("Unknown")),
)
Expand Down
1 change: 1 addition & 0 deletions components/datetime/src/zoned_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl<'d> ZonedDateTimeFormat<'d> {
.take_payload()?;

let pattern = data
.get()
.patterns
.get_pattern_for_options(options)?
.unwrap_or_default();
Expand Down
4 changes: 2 additions & 2 deletions components/datetime/tests/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn test_dayperiod_patterns() {
});
let provider = StructProvider {
key: GREGORY_V1,
data: data.as_ref(),
data: data.get(),
};
let dtf =
DateTimeFormat::try_new(langid.clone(), &provider, &format_options)
Expand Down Expand Up @@ -195,7 +195,7 @@ fn test_time_zone_patterns() {
});
let date_provider = StructProvider {
key: GREGORY_V1,
data: data.as_ref(),
data: data.get(),
};

let dtf = ZonedDateTimeFormat::try_new(
Expand Down
2 changes: 1 addition & 1 deletion components/decimal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<'d> FixedDecimalFormat<'d> {
FormattedFixedDecimal {
value,
options: &self.options,
symbols: &self.symbols,
symbols: self.symbols.get(),
}
}
}
30 changes: 8 additions & 22 deletions components/locale_canonicalizer/src/locale_canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,43 +91,29 @@ impl LocaleCanonicalizer<'_> {
/// ```
pub fn maximize<T: AsMut<LanguageIdentifier>>(&self, mut langid: T) -> CanonicalizationResult {
let langid = langid.as_mut();
let data = self.likely_subtags.get();

if !langid.language.is_empty() && langid.script.is_some() && langid.region.is_some() {
return CanonicalizationResult::Unmodified;
}

if let Some(language) = langid.language.into() {
if let Some(region) = langid.region {
maximize_locale!(
langid,
self.likely_subtags.language_region,
language,
region.into()
);
maximize_locale!(langid, data.language_region, language, region.into());
}
if let Some(script) = langid.script {
maximize_locale!(
langid,
self.likely_subtags.language_script,
language,
script.into()
);
maximize_locale!(langid, data.language_script, language, script.into());
}
maximize_locale!(langid, self.likely_subtags.language, language);
maximize_locale!(langid, data.language, language);
} else if let Some(script) = langid.script {
if let Some(region) = langid.region {
maximize_locale!(
langid,
self.likely_subtags.script_region,
script.into(),
region.into()
);
maximize_locale!(langid, data.script_region, script.into(), region.into());
}
maximize_locale!(langid, self.likely_subtags.script, script.into());
maximize_locale!(langid, data.script, script.into());
} else if let Some(region) = langid.region {
maximize_locale!(langid, self.likely_subtags.region, region.into());
maximize_locale!(langid, data.region, region.into());
}
update_langid(&self.likely_subtags.und, langid)
update_langid(&data.und, langid)
}

/// This returns a new Locale that is the result of running the
Expand Down
15 changes: 5 additions & 10 deletions components/plurals/benches/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use icu_provider::prelude::*;
fn parser(c: &mut Criterion) {
use icu_plurals::rules::parse_condition;

let plurals_data = helpers::get_plurals_data();
let fixture_data = helpers::get_plurals_data();

let provider = icu_testdata::get_provider();

let mut rules = vec![];

for langid in &plurals_data.langs {
let plurals_data: DataPayload<icu_plurals::provider::PluralRuleStringsV1> = provider
for langid in &fixture_data.langs {
let data_payload: DataPayload<icu_plurals::provider::PluralRuleStringsV1> = provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: icu_plurals::provider::key::CARDINAL_V1,
Expand All @@ -32,14 +32,9 @@ fn parser(c: &mut Criterion) {
.unwrap()
.take_payload()
.unwrap();
let data = data_payload.get();

let r = &[
&plurals_data.zero,
&plurals_data.one,
&plurals_data.two,
&plurals_data.few,
&plurals_data.many,
];
let r = &[&data.zero, &data.one, &data.two, &data.few, &data.many];

for i in r {
if let Some(x) = i {
Expand Down
2 changes: 1 addition & 1 deletion components/plurals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl PluralRules {
type_: PluralRuleType,
) -> Result<Self, PluralRulesError> {
let data = resolver::resolve_plural_data(langid.clone(), data_provider, type_)?;
Self::new_from_data(langid, &data)
Self::new_from_data(langid, data.get())
}

/// Returns the [`Plural Category`] appropriate for the given number.
Expand Down
2 changes: 1 addition & 1 deletion components/uniset/src/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn get_prop<'d, D: DataProvider<'d, UnicodeProperty<'d>> + ?Sized>(ppucd_provide
let resp: DataResponse<UnicodeProperty> = ppucd_provider.load_payload(&data_req)?;

let ppucd_property_payload: DataPayload<UnicodeProperty> = resp.take_payload()?;
let ppucd_property: UnicodeProperty = ppucd_property_payload.as_ref().clone();
let ppucd_property: UnicodeProperty = ppucd_property_payload.get().clone();
ppucd_property.try_into()
}

Expand Down
2 changes: 1 addition & 1 deletion experimental/provider_ppucd/src/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,5 @@ fn test_ppucd_provider_parse() {
8287, 8288, 12288, 12289,
],
};
assert_eq!(exp_prop_uniset, ppucd_property_cow.as_ref().clone());
assert_eq!(exp_prop_uniset, ppucd_property_cow.get().clone());
}
2 changes: 1 addition & 1 deletion provider/cldr/src/download/cldr_allinone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use std::path::PathBuf;
/// .unwrap()
/// .take_payload()
/// .unwrap();
/// assert_eq!(data.few, Some(Cow::Borrowed("n % 10 = 3 and n % 100 != 13")));
/// assert_eq!(data.get().few, Some(Cow::Borrowed("n % 10 = 3 and n % 100 != 13")));
/// }
///
/// // Calling demo(downloader) will cause the data to actually get downloaded.
Expand Down
Loading

0 comments on commit 72c85fa

Please sign in to comment.