Skip to content

Commit

Permalink
Changes to icu_pattern and DurationFormatter formatToParts
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Dec 13, 2024
1 parent 6e0c8cf commit e6877e2
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 48 deletions.
6 changes: 3 additions & 3 deletions components/experimental/src/dimension/units/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ pub struct FormattedUnit<'l> {
}

impl Writeable for FormattedUnit<'_> {
fn write_to<W>(&self, sink: &mut W) -> core::result::Result<(), core::fmt::Error>
fn write_to_parts<W>(&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)
}
}

Expand Down
10 changes: 10 additions & 0 deletions components/experimental/src/duration/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
20 changes: 0 additions & 20 deletions components/pattern/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions components/pattern/src/double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -89,7 +89,7 @@ where
};
(
WriteableAsTryWriteableInfallible(writeable),
crate::PATTERN_PLACEHOLDER_PART,
crate::parts::PLACEHOLDER,
)
}
}
Expand All @@ -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;
Expand All @@ -113,7 +113,7 @@ where
};
(
WriteableAsTryWriteableInfallible(writeable),
crate::PATTERN_PLACEHOLDER_PART,
crate::parts::PLACEHOLDER,
)
}
}
Expand Down
19 changes: 8 additions & 11 deletions components/pattern/src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,30 @@ 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::<SinglePlaceholder>::try_from_str(
/// "Hello, {0}!",
/// Default::default(),
/// )
/// .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!"
/// );
/// ```
///
Expand Down
3 changes: 1 addition & 2 deletions components/pattern/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,14 @@ mod implementations;
mod multi_named;
#[cfg(feature = "alloc")]
mod parser;
pub mod parts;
mod single;

pub use common::PatternBackend;
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;
Expand Down
8 changes: 4 additions & 4 deletions components/pattern/src/multi_named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -109,7 +109,7 @@ where
Some(value) => Ok(value),
None => Err(MissingNamedPlaceholderError { name: key.0 }),
};
(writeable, crate::PATTERN_PLACEHOLDER_PART)
(writeable, crate::parts::PLACEHOLDER)
}
}

Expand All @@ -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,
Expand All @@ -136,7 +136,7 @@ where
Some(value) => Ok(value),
None => Err(MissingNamedPlaceholderError { name: key.0 }),
};
(writeable, crate::PATTERN_PLACEHOLDER_PART)
(writeable, crate::parts::PLACEHOLDER)
}
}

Expand Down
60 changes: 60 additions & 0 deletions components/pattern/src/parts.rs
Original file line number Diff line number Diff line change
@@ -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::<SinglePlaceholder>::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",
};
8 changes: 4 additions & 4 deletions components/pattern/src/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}
Expand All @@ -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,
)
}
}
Expand Down

0 comments on commit e6877e2

Please sign in to comment.