-
Notifications
You must be signed in to change notification settings - Fork 180
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
Add lower-level data API to PluralRules #575
Conversation
components/plurals/src/lib.rs
Outdated
|
||
/// Lower level helper that allows extracting PluralRules-relevant data from a data provider | ||
/// without constructing PluralRules | ||
pub fn get_plural_data<'d, D: DataProvider<'d, provider::PluralRuleStringsV1<'d>> + ?Sized>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are on PluralRules instead of DataProvider because we can still use slightly higher level types here.
I could instead move this to DataProvider and have it return PluralRuleStringsV1 if people prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I have a slight preference toward storing this on Provider data struct (and that's what I do on DTF) but that may be because I don't see the value you describe on storing it here yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zbraniecki if it's on Provider then I have to return PluralRuleStringsV1 instead of the converted type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I don't think it should be on provider, more like https://github.com/unicode-org/icu4x/blob/main/components/datetime/src/provider/helpers.rs - on https://github.com/unicode-org/icu4x/blob/main/components/plurals/src/provider.rs#L22
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. Should I be pulling out a PluralRulesV1 or a PluralRuleList?
I kinda prefer it on the type itself because it's all in one place then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd advocate for having a function in the icu_plurals::provider
module (probably in a sub module) that returns PluralRuleStringsV1.
Why PluralRuleStringsV1?
- It's public
- The same architecture (returning data structs) can be used in other crates
- No dependencies outside of the
provider
module
Codecov Report
@@ Coverage Diff @@
## main #575 +/- ##
=======================================
Coverage 74.22% 74.22%
=======================================
Files 128 128
Lines 7840 7845 +5
=======================================
+ Hits 5819 5823 +4
- Misses 2021 2022 +1
Continue to review full report at Codecov.
|
Pull Request Test Coverage Report for Build 180c8335e579ba3eaba9ffc5fa7e06a217d74a2b-PR-575
💛 - Coveralls |
Updated to use PluralRuleStringsV1. Unfortunately, this means we need to use a Cow since we have to return it, unless we're forced to clone it. Either way this will be boxed on the FFI side I guess, so it doesn't matter all that much. |
components/plurals/src/provider.rs
Outdated
use std::borrow::Cow; | ||
|
||
impl<'s> super::PluralRuleStringsV1<'s> { | ||
pub fn new_from_provider<D: DataProvider<'s, super::PluralRuleStringsV1<'s>> + ?Sized>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm torn on whether to put this function on the data struct itself, or whether to make it a function within the module. I think I tend to prefer "pure functions" with well-defined inputs and outputs. Another reason to prefer standalone functions is that in other, more complicated components, these functions might want to do things like return multiple data structs or return an enum with one of several structs depending on the input. So, associating the function with a single data struct might not be the most generalizable choice.
components/plurals/src/provider.rs
Outdated
@@ -46,3 +46,35 @@ pub struct PluralRuleStringsV1<'s> { | |||
)] | |||
pub many: Option<Cow<'s, str>>, | |||
} | |||
|
|||
mod convert { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. Basically what we want is a way to map "locale + options => data struct". Module name brainstorm:
resolver
resolution
mapping
mapper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah since it's a method it was a private module so I didn't care about the name, but if I'm making it a function I will.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed; in the latest commit.
(I also rebased over master but did not change the commit structure, unsure if GitHub's diffs handle that well)
/// data obtained from a provider | ||
pub fn new_from_data<'d>( | ||
langid: LanguageIdentifier, | ||
data: &PluralRuleStringsV1<'d>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be taken by value. I think in most cases we should take ownership of the data struct in order to avoid unnecessary clones. It's not useful for the caller to keep the data struct around any longer than when they call new_from_data
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no unnecessary clone happening here, is there? We're parsing the data struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the data struct owns its own strings, then passing by &
means that they need to be cloned in the general case. For PluralRuleStringsV1 in particular, maybe this doesn't matter.
I guess the question is whether we should make these methods "always take by value", "always take by reference", or "case-by-case basis"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bear in mind, we usually get a Cow out, so if we wanted to take ownership we would be forced to clone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #615, once that is resolved we can revisit this since the data model will change.
Notice: the branch changed across the force-push!
~ Your Friendly Jira-GitHub PR Checker Bot |
Notice: the branch changed across the force-push!
~ Your Friendly Jira-GitHub PR Checker Bot |
Progress on #560
This is a draft of how the lower-level data API will look. Want to get confirmation on this before adding FFI versions and also perhaps adding the same API to other types.