From e6877e2518b2580cb35e53dba7ef5792dd150b92 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Thu, 12 Dec 2024 17:53:54 -0800 Subject: [PATCH] Changes to icu_pattern and DurationFormatter formatToParts --- .../src/dimension/units/format.rs | 6 +- .../experimental/src/duration/format.rs | 10 ++++ components/pattern/src/common.rs | 20 ------- components/pattern/src/double.rs | 8 +-- components/pattern/src/frontend/mod.rs | 19 +++--- components/pattern/src/lib.rs | 3 +- components/pattern/src/multi_named.rs | 8 +-- components/pattern/src/parts.rs | 60 +++++++++++++++++++ components/pattern/src/single.rs | 8 +-- 9 files changed, 94 insertions(+), 48 deletions(-) create mode 100644 components/pattern/src/parts.rs diff --git a/components/experimental/src/dimension/units/format.rs b/components/experimental/src/dimension/units/format.rs index a39ab1cac8f..0b9c81717d9 100644 --- a/components/experimental/src/dimension/units/format.rs +++ b/components/experimental/src/dimension/units/format.rs @@ -21,15 +21,15 @@ pub struct FormattedUnit<'l> { } impl Writeable for FormattedUnit<'_> { - fn write_to(&self, sink: &mut W) -> core::result::Result<(), core::fmt::Error> + fn write_to_parts(&self, sink: &mut W) -> core::result::Result<(), core::fmt::Error> where - W: core::fmt::Write + ?Sized, + W: writeable::PartsWrite + ?Sized, { self.display_name .patterns .get(self.value.into(), self.plural_rules) .interpolate((self.fixed_decimal_formatter.format(self.value),)) - .write_to(sink) + .write_to_parts(sink) } } diff --git a/components/experimental/src/duration/format.rs b/components/experimental/src/duration/format.rs index 868f5caa06b..dd6b0302df6 100644 --- a/components/experimental/src/duration/format.rs +++ b/components/experimental/src/duration/format.rs @@ -710,12 +710,22 @@ mod tests { [ (0, 6, parts::YEAR), (0, 6, icu_list::parts::ELEMENT), + (0, 2, icu_pattern::parts::PLACEHOLDER), + (0, 1, icu_decimal::parts::MINUS_SIGN), + (1, 2, icu_decimal::parts::INTEGER), + (2, 6, icu_pattern::parts::LITERAL), (6, 8, icu_list::parts::LITERAL), (8, 14, parts::MONTH), (8, 14, icu_list::parts::ELEMENT), + (8, 9, icu_decimal::parts::INTEGER), + (8, 9, icu_pattern::parts::PLACEHOLDER), + (9, 14, icu_pattern::parts::LITERAL), (14, 16, icu_list::parts::LITERAL), (16, 21, parts::WEEK), (16, 21, icu_list::parts::ELEMENT), + (16, 17, icu_decimal::parts::INTEGER), + (16, 17, icu_pattern::parts::PLACEHOLDER), + (17, 21, icu_pattern::parts::LITERAL), (21, 23, icu_list::parts::LITERAL), (23, 37, icu_list::parts::ELEMENT), (23, 25, icu_decimal::parts::INTEGER), diff --git a/components/pattern/src/common.rs b/components/pattern/src/common.rs index a2e328e4baa..b7a2b8c8158 100644 --- a/components/pattern/src/common.rs +++ b/components/pattern/src/common.rs @@ -110,26 +110,6 @@ pub trait PatternBackend: crate::private::Sealed + 'static + core::fmt::Debug { fn empty() -> &'static Self::Store; } -/// Default annotation for the literal portion of a pattern. -/// -/// For more information, see [`PlaceholderValueProvider`]. For an example, see [`Pattern`]. -/// -/// [`Pattern`]: crate::Pattern -pub const PATTERN_LITERAL_PART: Part = Part { - category: "pattern", - value: "literal", -}; - -/// Default annotation for the placeholder portion of a pattern. -/// -/// For more information, see [`PlaceholderValueProvider`]. For an example, see [`Pattern`]. -/// -/// [`Pattern`]: crate::Pattern -pub const PATTERN_PLACEHOLDER_PART: Part = Part { - category: "pattern", - value: "placeholder", -}; - /// Trait implemented on collections that can produce [`TryWriteable`]s for interpolation. /// /// This trait determines the [`Part`]s produced by the writeable. In this crate, implementations diff --git a/components/pattern/src/double.rs b/components/pattern/src/double.rs index e5129e490fc..7c178ac55c3 100644 --- a/components/pattern/src/double.rs +++ b/components/pattern/src/double.rs @@ -80,7 +80,7 @@ where where W0: 'a, W1: 'a; - const LITERAL_PART: writeable::Part = crate::PATTERN_LITERAL_PART; + const LITERAL_PART: writeable::Part = crate::parts::LITERAL; #[inline] fn value_for(&self, key: DoublePlaceholderKey) -> (Self::W<'_>, writeable::Part) { let writeable = match key { @@ -89,7 +89,7 @@ where }; ( WriteableAsTryWriteableInfallible(writeable), - crate::PATTERN_PLACEHOLDER_PART, + crate::parts::PLACEHOLDER, ) } } @@ -103,7 +103,7 @@ where = WriteableAsTryWriteableInfallible<&'a W> where W: 'a; - const LITERAL_PART: writeable::Part = crate::PATTERN_LITERAL_PART; + const LITERAL_PART: writeable::Part = crate::parts::LITERAL; #[inline] fn value_for(&self, key: DoublePlaceholderKey) -> (Self::W<'_>, writeable::Part) { let [item0, item1] = self; @@ -113,7 +113,7 @@ where }; ( WriteableAsTryWriteableInfallible(writeable), - crate::PATTERN_PLACEHOLDER_PART, + crate::parts::PLACEHOLDER, ) } } diff --git a/components/pattern/src/frontend/mod.rs b/components/pattern/src/frontend/mod.rs index 39a22c6a31f..d466c3ac2a7 100644 --- a/components/pattern/src/frontend/mod.rs +++ b/components/pattern/src/frontend/mod.rs @@ -41,18 +41,20 @@ use writeable::{adapters::TryWriteableInfallibleAsWriteable, PartsWrite, TryWrit /// [`Pattern`] supports interpolating with [writeable::Part]s, annotations for whether the /// substring was a placeholder or a literal. /// -/// By default, the substrings are annotated with [`PATTERN_LITERAL_PART`] and -/// [`PATTERN_PLACEHOLDER_PART`]. This can be customized with [`PlaceholderValueProvider`]. +/// By default, the substrings are annotated with [`parts::LITERAL`] and +/// [`parts::PLACEHOLDER`]. This can be customized with [`PlaceholderValueProvider`]. +/// +/// For an example, see [`parts`]. /// /// # Examples /// -/// Interpolating a [`SinglePlaceholder`] pattern with parts: +/// Interpolating a [`SinglePlaceholder`] pattern: /// /// ``` /// use core::str::FromStr; /// use icu_pattern::Pattern; /// use icu_pattern::SinglePlaceholder; -/// use writeable::assert_writeable_parts_eq; +/// use writeable::assert_writeable_eq; /// /// let pattern = Pattern::::try_from_str( /// "Hello, {0}!", @@ -60,14 +62,9 @@ use writeable::{adapters::TryWriteableInfallibleAsWriteable, PartsWrite, TryWrit /// ) /// .unwrap(); /// -/// assert_writeable_parts_eq!( +/// assert_writeable_eq!( /// pattern.interpolate(["Alice"]), -/// "Hello, Alice!", -/// [ -/// (0, 7, icu_pattern::PATTERN_LITERAL_PART), -/// (7, 12, icu_pattern::PATTERN_PLACEHOLDER_PART), -/// (12, 13, icu_pattern::PATTERN_LITERAL_PART), -/// ] +/// "Hello, Alice!" /// ); /// ``` /// diff --git a/components/pattern/src/lib.rs b/components/pattern/src/lib.rs index 78b9cad560b..62693382608 100644 --- a/components/pattern/src/lib.rs +++ b/components/pattern/src/lib.rs @@ -59,6 +59,7 @@ mod implementations; mod multi_named; #[cfg(feature = "alloc")] mod parser; +pub mod parts; mod single; pub use common::PatternBackend; @@ -66,8 +67,6 @@ pub use common::PatternItem; #[cfg(feature = "alloc")] pub use common::PatternItemCow; pub use common::PlaceholderValueProvider; -pub use common::PATTERN_LITERAL_PART; -pub use common::PATTERN_PLACEHOLDER_PART; pub use double::DoublePlaceholder; pub use double::DoublePlaceholderKey; pub use error::PatternError; diff --git a/components/pattern/src/multi_named.rs b/components/pattern/src/multi_named.rs index ace5e3b2474..a648a29631a 100644 --- a/components/pattern/src/multi_named.rs +++ b/components/pattern/src/multi_named.rs @@ -99,7 +99,7 @@ where where W: 'a, Self: 'a; - const LITERAL_PART: writeable::Part = crate::PATTERN_LITERAL_PART; + const LITERAL_PART: writeable::Part = crate::parts::LITERAL; #[inline] fn value_for<'a>( &'a self, @@ -109,7 +109,7 @@ where Some(value) => Ok(value), None => Err(MissingNamedPlaceholderError { name: key.0 }), }; - (writeable, crate::PATTERN_PLACEHOLDER_PART) + (writeable, crate::parts::PLACEHOLDER) } } @@ -126,7 +126,7 @@ where where W: 'a, Self: 'a; - const LITERAL_PART: writeable::Part = crate::PATTERN_LITERAL_PART; + const LITERAL_PART: writeable::Part = crate::parts::LITERAL; #[inline] fn value_for<'a>( &'a self, @@ -136,7 +136,7 @@ where Some(value) => Ok(value), None => Err(MissingNamedPlaceholderError { name: key.0 }), }; - (writeable, crate::PATTERN_PLACEHOLDER_PART) + (writeable, crate::parts::PLACEHOLDER) } } diff --git a/components/pattern/src/parts.rs b/components/pattern/src/parts.rs new file mode 100644 index 00000000000..7976bd47cbb --- /dev/null +++ b/components/pattern/src/parts.rs @@ -0,0 +1,60 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +//! Parts of a formatted pattern. +//! +//! # Examples +//! +//! Interpolating a [`SinglePlaceholder`] pattern with parts: +//! +//! ``` +//! use core::str::FromStr; +//! use icu_pattern::Pattern; +//! use icu_pattern::SinglePlaceholder; +//! use writeable::assert_writeable_parts_eq; +//! +//! let pattern = Pattern::::try_from_str( +//! "Hello, {0}!", +//! Default::default(), +//! ) +//! .unwrap(); +//! +//! assert_writeable_parts_eq!( +//! pattern.interpolate(["Alice"]), +//! "Hello, Alice!", +//! [ +//! (0, 7, icu_pattern::parts::LITERAL), +//! (7, 12, icu_pattern::parts::PLACEHOLDER), +//! (12, 13, icu_pattern::parts::LITERAL), +//! ] +//! ); +//! ``` +//! +//! [`SinglePlaceholder`]: crate::SinglePlaceholder +//! [`DoublePlaceholder`]: crate::DoublePlaceholder +//! [`MultiNamedPlaceholder`]: crate::MultiNamedPlaceholder +//! [`LITERAL`]: crate::parts::LITERAL +//! [`PLACEHOLDER`]: crate::parts::PLACEHOLDER + +use writeable::Part; + +/// Default annotation for the literal portion of a pattern. +/// +/// For more information, see [`PlaceholderValueProvider`]. For an example, see [`Pattern`]. +/// +/// [`Pattern`]: crate::Pattern +pub const LITERAL: Part = Part { + category: "pattern", + value: "literal", +}; + +/// Default annotation for the placeholder portion of a pattern. +/// +/// For more information, see [`PlaceholderValueProvider`]. For an example, see [`Pattern`]. +/// +/// [`Pattern`]: crate::Pattern +pub const PLACEHOLDER: Part = Part { + category: "pattern", + value: "placeholder", +}; diff --git a/components/pattern/src/single.rs b/components/pattern/src/single.rs index 9f74913b317..a22547ddf96 100644 --- a/components/pattern/src/single.rs +++ b/components/pattern/src/single.rs @@ -69,11 +69,11 @@ where = WriteableAsTryWriteableInfallible<&'a W> where W: 'a; - const LITERAL_PART: writeable::Part = crate::PATTERN_LITERAL_PART; + const LITERAL_PART: writeable::Part = crate::parts::LITERAL; fn value_for(&self, _key: SinglePlaceholderKey) -> (Self::W<'_>, writeable::Part) { ( WriteableAsTryWriteableInfallible(&self.0), - crate::PATTERN_PLACEHOLDER_PART, + crate::parts::PLACEHOLDER, ) } } @@ -87,12 +87,12 @@ where = WriteableAsTryWriteableInfallible<&'a W> where W: 'a; - const LITERAL_PART: writeable::Part = crate::PATTERN_LITERAL_PART; + const LITERAL_PART: writeable::Part = crate::parts::LITERAL; fn value_for(&self, _key: SinglePlaceholderKey) -> (Self::W<'_>, writeable::Part) { let [value] = self; ( WriteableAsTryWriteableInfallible(value), - crate::PATTERN_PLACEHOLDER_PART, + crate::parts::PLACEHOLDER, ) } }