Skip to content

Conversation

@pdogr
Copy link
Contributor

@pdogr pdogr commented Nov 29, 2022

plural_rules_mapping
.other
.as_ref()
.expect("Mapping for PluralCategory::Other must be present.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: .expect is too strong here. This is a good example of something you should check at deserialization time. See #2768

Copy link
Contributor Author

@pdogr pdogr Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making other a required field in relativetime/src/provider.rs.
This removes the need for expect.

sink.with_part(parts::LITERAL, |s| s.write_str(&pattern.pattern))?;
} else {
sink.with_part(parts::LITERAL, |s| {
s.write_str(&pattern.pattern[..pattern.index as usize])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: Don't use indexing_slicing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced this by split_at function.

pdogr and others added 3 commits December 1, 2022 00:58
- Fix negative number checking
- Move lib test to doc test
- Fix docs in relativetime/src/error.rs
- Change format method to take Into<FixedDecimal>
- Make "dateFields.other" a non-optional field
- This removes the 'expect' in FormattedRelativeTime.format
- Replace indexing slicing by 'split_at'
- Add tests in cldr/relativetime for sentinel index (pattern without
  placeholder)
@pdogr pdogr marked this pull request as ready for review November 30, 2022 21:24
@pdogr pdogr requested review from a team and Manishearth as code owners November 30, 2022 21:24
- Handle Numeric::Auto
- Add doc tests
} else {
let (prefix, suffix) = singular_sub_pattern
.pattern
.split_at(singular_sub_pattern.index as usize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: split_at is panicky. It would be nice if there were a lint to catch it. Please see https://github.com/unicode-org/icu4x/pull/2854/files for a workaround

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay to use split_at with explicitly checking singular_sub_pattern.index <= pattern.len() ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary to check the length before calling split_at.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes. Using code similar to the above PR.

.split_at(singular_sub_pattern.index as usize);

sink.with_part(parts::LITERAL, |s| s.write_str(prefix))?;
// TODO: Remove this clone.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: when constructing the FormattedRelativeTime, store a bool for whether it is negative or not and remove the sign at that point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

pdogr added 3 commits December 1, 2022 15:38
- Remove displaynames component from deps
- Make RelativeTimeFormatter.format take FixedDecimal arg
- Fix clippy error
Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

fn write_to_parts<S: writeable::PartsWrite + ?Sized>(&self, sink: &mut S) -> core::fmt::Result {
if self.options.numeric == Numeric::Auto {
let relatives = &self.formatter.rt.get().relatives;
if let Ok(i8_value) = self.value.to_string().parse::<i8>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this: we shouldn't convert to a string and parse here. Better would be something along the lines of

if self.value.magnitude_range() == 0..=0 {
    let u8_value = self.value.digit_at(0);
    // ...
}

Also, note that this doesn't currently work correctly with negative numbers since we remove the sign earlier now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The values are either -2, -1, 0, 1, 2 in the data. The 0th digit + sign is sufficient.

sffc
sffc previously approved these changes Dec 2, 2022
@sffc
Copy link
Member

sffc commented Dec 2, 2022

Please update missing_apis.txt

sffc
sffc previously approved these changes Dec 2, 2022
@sffc
Copy link
Member

sffc commented Dec 5, 2022

@pdogr I think you can ignore the Coverage CI failure and merge this.

@pdogr pdogr merged commit 95d1d37 into unicode-org:main Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants