Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move map_project to closures #2185

Merged
merged 6 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 36 additions & 46 deletions components/datetime/src/provider/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,10 @@ where
D: ResourceProvider<TimePatternsV1Marker> + ?Sized,
{
let patterns_data = time_patterns_data_payload(data_provider, locale)?;
Ok(patterns_data.map_project_with_capture(
(length, preferences),
|data, (length, preferences), _| {
let pattern = pattern_for_time_length_inner(data, length, &preferences).clone();
pattern.into()
},
))
Ok(patterns_data.map_project(|data, _| {
let pattern = pattern_for_time_length_inner(data, length, &preferences).clone();
pattern.into()
}))
}

fn date_patterns_data_payload<D>(
Expand Down Expand Up @@ -128,12 +125,10 @@ where
D: ResourceProvider<DatePatternsV1Marker> + ?Sized,
{
let patterns_data = date_patterns_data_payload(data_provider, locale)?;
Ok(
patterns_data.map_project_with_capture(length, |data, length, _| {
let pattern = pattern_for_date_length_inner(data, length);
pattern.into()
}),
)
Ok(patterns_data.map_project(|data, _| {
let pattern = pattern_for_date_length_inner(data, length);
pattern.into()
}))
}

/// Determine the appropriate `Pattern` for a given `options::length::Date` bag.
Expand All @@ -146,18 +141,16 @@ where
D: ResourceProvider<DatePatternsV1Marker> + ?Sized,
{
let patterns_data = date_patterns_data_payload(data_provider, locale)?;
Ok(
patterns_data.map_project_with_capture(length, |data, length, _| {
let pattern = match length {
length::Date::Full => data.length_combinations.full,
length::Date::Long => data.length_combinations.long,
length::Date::Medium => data.length_combinations.medium,
length::Date::Short => data.length_combinations.short,
};
Ok(patterns_data.map_project(|data, _| {
let pattern = match length {
length::Date::Full => data.length_combinations.full,
length::Date::Long => data.length_combinations.long,
length::Date::Medium => data.length_combinations.medium,
length::Date::Short => data.length_combinations.short,
};

pattern.into()
}),
)
pattern.into()
}))
}

pub struct PatternSelector<'a, D: ?Sized> {
Expand Down Expand Up @@ -231,30 +224,27 @@ where
let time_patterns_data = time_patterns_data_payload(self.data_provider, self.locale)?;

let date_patterns_data = date_patterns_data_payload(self.data_provider, self.locale)?;
date_patterns_data.try_map_project_with_capture(
(date_length, time_length, preferences, time_patterns_data),
|data, (date_length, time_length, preferences, time_patterns_data), _| {
// TODO (#1131) - We may be able to remove the clone here.
let date = pattern_for_date_length_inner(data.clone(), date_length)
.expect_pattern("Lengths are single patterns");
date_patterns_data.try_map_project(|data, _| {
// TODO (#1131) - We may be able to remove the clone here.
let date = pattern_for_date_length_inner(data.clone(), date_length)
.expect_pattern("Lengths are single patterns");

let pattern = match date_length {
length::Date::Full => data.length_combinations.full,
length::Date::Long => data.length_combinations.long,
length::Date::Medium => data.length_combinations.medium,
length::Date::Short => data.length_combinations.short,
};
let pattern = match date_length {
length::Date::Full => data.length_combinations.full,
length::Date::Long => data.length_combinations.long,
length::Date::Medium => data.length_combinations.medium,
length::Date::Short => data.length_combinations.short,
};

// TODO (#1131) - We may be able to remove the clone here.
let time = pattern_for_time_length_inner(
time_patterns_data.get().clone(),
time_length,
&preferences,
)
.expect_pattern("Lengths are single patterns");
Ok(PatternPlurals::from(pattern.combined(date, time)?).into())
},
)
// TODO (#1131) - We may be able to remove the clone here.
let time = pattern_for_time_length_inner(
time_patterns_data.get().clone(),
time_length,
&preferences,
)
.expect_pattern("Lengths are single patterns");
Ok(PatternPlurals::from(pattern.combined(date, time)?).into())
})
}

/// Determine the appropriate `PatternPlurals` for a given `options::components::Bag`.
Expand Down
10 changes: 4 additions & 6 deletions components/datetime/src/raw/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,12 @@ impl DateTimeFormat {
date: DateFormat,
time: TimeFormat,
) -> Result<Self, DateTimeFormatError> {
let generic_pattern = &date.generic_pattern;
let time_patterns = &time.patterns;
let patterns = date
.patterns
.try_map_project_with_capture::<PatternPluralsFromPatternsV1Marker, (
DataPayload<GenericPatternV1Marker>,
DataPayload<PatternPluralsFromPatternsV1Marker>,
), DateTimeFormatError>(
(date.generic_pattern, time.patterns),
|data, (generic_pattern, time_patterns), _| {
.try_map_project::<PatternPluralsFromPatternsV1Marker, _, DateTimeFormatError>(
|data, _| {
let date_pattern = data.0.expect_pattern("Lengths are single patterns");
let time_pattern: crate::pattern::runtime::Pattern = time_patterns
.get()
Expand Down
31 changes: 14 additions & 17 deletions provider/blob/src/blob_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,21 @@ impl BufferProvider for BlobDataProvider {
Ok(DataResponse {
metadata,
payload: Some(DataPayload::from_yoked_buffer(
self.data.try_map_project_cloned_with_capture(
(key, req),
|blob, (key, req), _| {
let idx = blob
.keys
.get0(&key.get_hash())
.ok_or(DataErrorKind::MissingResourceKey)
.and_then(|cursor| {
cursor
.get1_copied_by(|bytes| req.options.strict_cmp(bytes).reverse())
.ok_or(DataErrorKind::MissingResourceOptions)
})
.map_err(|kind| kind.with_req(key, req))?;
blob.buffers.get(idx).ok_or_else(|| {
DataError::custom("Invalid blob bytes").with_req(key, req)
self.data.try_map_project_cloned(|blob, _| {
let idx = blob
.keys
.get0(&key.get_hash())
.ok_or(DataErrorKind::MissingResourceKey)
.and_then(|cursor| {
cursor
.get1_copied_by(|bytes| req.options.strict_cmp(bytes).reverse())
.ok_or(DataErrorKind::MissingResourceOptions)
})
},
)?,
.map_err(|kind| kind.with_req(key, req))?;
blob.buffers
.get(idx)
.ok_or_else(|| DataError::custom("Invalid blob bytes").with_req(key, req))
})?,
)),
})
}
Expand Down
Loading