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

DurationFormatter: Implement DurationFormatter #5321

Merged
merged 13 commits into from
Aug 13, 2024

Conversation

kartva
Copy link
Member

@kartva kartva commented Aug 1, 2024

follow up to #5263

will add documentation, benchmarks, and more tests in separate PRs

Comment on lines +590 to +594
struct PartSink {
string: String,
parts: Vec<(usize, usize, writeable::Part)>,
}

Copy link
Member Author

Choose a reason for hiding this comment

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

PartSink is very similar to the TestSink the writeable library internally uses. I create it here to add part information to ListFormatter-emitted parts. (changing parts::Element to parts::HOUR, etc.)

It would be nice if ListFormatter could optionally preserve part annotations for the Writeables it formats. I'd be happy to try to work on that if that sounds useful.

Copy link
Member

Choose a reason for hiding this comment

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

Good point, @sffc , what do you think ?

Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't ever need to allocate anything, including into a Vec or String, when formatting in ICU4X. If we can do it in datetime format, which is an order of magnitude more complex than any other ICU4X formatter right now, we can do it here in DurationFormatter. :)

Please note that ListFormatter is already wired up to allow you to do this. The ListFormatter invokes the write_to_parts function of the writeables passed to its format function. You should make sure those writeables add the unit part. You'll get the ELEMENT and FIELD parts from icu_list, but that's fine.

Copy link
Member

Choose a reason for hiding this comment

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

You can use #5328 for this now.

@kartva kartva requested review from sffc and younies August 1, 2024 04:06
@kartva kartva marked this pull request as ready for review August 1, 2024 04:06
@kartva kartva requested a review from a team as a code owner August 1, 2024 04:06
pub(crate) fn as_fixed_decimal_sign(&self) -> fixed_decimal::Sign {
match self {
DurationSign::Positive => fixed_decimal::Sign::Positive,
impl From<DurationSign> for fixed_decimal::Sign {
Copy link
Member

Choose a reason for hiding this comment

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

+1 that is good too.

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.

There are some code style and cleanups that need to be applied (no allocation, no panicking, more tests) but this is fine to land as a checkpoint commit.

@younies younies merged commit 1e3ad9b into unicode-org:main Aug 13, 2024
28 checks passed
Comment on lines +36 to +52
pub(crate) unit: DurationUnitFormatter,
pub(crate) list: ListFormatter,
pub(crate) fdf: FixedDecimalFormatter,
}

pub(crate) struct DurationUnitFormatter {
pub(crate) year: UnitsFormatter,
pub(crate) month: UnitsFormatter,
pub(crate) week: UnitsFormatter,
pub(crate) day: UnitsFormatter,
pub(crate) hour: UnitsFormatter,
pub(crate) minute: UnitsFormatter,
pub(crate) second: UnitsFormatter,
pub(crate) millisecond: UnitsFormatter,
pub(crate) microsecond: UnitsFormatter,
pub(crate) nanosecond: UnitsFormatter,
}
Copy link
Member

Choose a reason for hiding this comment

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

This is need to be only one units formatter

Copy link
Member

Choose a reason for hiding this comment

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

no need for now

Comment on lines +36 to +37
pub(crate) unit: DurationUnitFormatter,
pub(crate) list: ListFormatter,
Copy link
Member

Choose a reason for hiding this comment

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

units_formatters: DurationUnitFormatters

pub struct DurationFormatter {
/// Options for configuring the formatter.
pub(crate) options: ValidatedDurationFormatterOptions,
pub(crate) digital: DataPayload<provider::DigitalDurationDataV1Marker>,
pub(crate) unit: DurationUnitFormatter,
pub(crate) list: ListFormatter,
Copy link
Member

Choose a reason for hiding this comment

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

list_formatter: ListFormatter

pub(crate) fdf: FixedDecimalFormatter,
}

pub(crate) struct DurationUnitFormatter {
Copy link
Member

Choose a reason for hiding this comment

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

/// Contains all the needed formatters for formatting the duration units

DurationUnitFormatters

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