|
3 | 3 | // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
|
4 | 4 |
|
5 | 5 | //! Temporary module for neo datetime patterns
|
6 |
| -
|
7 |
| -use core::str::FromStr; |
8 |
| - |
9 |
| -use writeable::{impl_display_with_writeable, Writeable}; |
10 |
| - |
11 |
| -use crate::provider::pattern::{runtime, PatternError, PatternItem}; |
12 |
| -use crate::size_test_macro::size_test; |
13 |
| - |
14 |
| -size_test!(DateTimePattern, date_time_pattern_size, 32); |
15 |
| - |
16 |
| -/// A pattern for formatting a datetime in a calendar. |
17 |
| -/// |
18 |
| -/// [`DateTimePattern`] forgoes most internationalization functionality of the datetime crate. |
19 |
| -/// It assumes that the pattern is already localized for the customer's locale. Most clients |
20 |
| -/// should use [`DateTimeFormatter`] instead of directly formatting with patterns. |
21 |
| -/// |
22 |
| -/// There are two ways to make one of these: |
23 |
| -/// |
24 |
| -/// 1. From a custom pattern string: [`DateTimePattern::try_from_pattern_str`] |
25 |
| -/// 2. From a formatted datetime: [`FormattedNeoDateTime::pattern`] |
26 |
| -/// |
27 |
| -/// Things you can do with one of these: |
28 |
| -/// |
29 |
| -/// 1. Use it to directly format a datetime via [`TypedDateTimeNames`] |
30 |
| -/// 2. Convert it to a string pattern via [`Writeable`] |
31 |
| -/// 3. Get the resolved components |
32 |
| -/// |
33 |
| -#[doc = date_time_pattern_size!()] |
34 |
| -/// |
35 |
| -/// # Examples |
36 |
| -/// |
37 |
| -/// Create a pattern from a custom string and compare it to one from data, |
38 |
| -/// then check the resolved components: |
39 |
| -/// |
40 |
| -/// ``` |
41 |
| -/// use icu::calendar::Date; |
42 |
| -/// use icu::calendar::Gregorian; |
43 |
| -/// use icu::datetime::fieldset::YMD; |
44 |
| -/// use icu::datetime::neo_pattern::DateTimePattern; |
45 |
| -/// use icu::datetime::fields::components; |
46 |
| -/// use icu::datetime::FixedCalendarDateTimeFormatter; |
47 |
| -/// use icu::locale::locale; |
48 |
| -/// use writeable::assert_writeable_eq; |
49 |
| -/// |
50 |
| -/// // Create the pattern from a string: |
51 |
| -/// let pattern_str = "d MMM y"; |
52 |
| -/// let custom_pattern = |
53 |
| -/// DateTimePattern::try_from_pattern_str(pattern_str).unwrap(); |
54 |
| -/// assert_writeable_eq!(custom_pattern, pattern_str); |
55 |
| -/// |
56 |
| -/// // Load data that resolves to the same pattern: |
57 |
| -/// let data_pattern = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new( |
58 |
| -/// &locale!("es-MX").into(), |
59 |
| -/// YMD::medium(), |
60 |
| -/// ) |
61 |
| -/// .unwrap() |
62 |
| -/// // The pattern can depend on the datetime being formatted. |
63 |
| -/// .format(&Date::try_new_iso(2024, 1, 1).unwrap().to_calendar(Gregorian)) |
64 |
| -/// .pattern(); |
65 |
| -/// assert_writeable_eq!(data_pattern, pattern_str); |
66 |
| -/// assert_eq!(custom_pattern, data_pattern); |
67 |
| -/// |
68 |
| -/// // Check the resolved components: |
69 |
| -/// let mut expected_components_bag = components::Bag::default(); |
70 |
| -/// expected_components_bag.year = Some(components::Year::Numeric); |
71 |
| -/// expected_components_bag.month = Some(components::Month::Short); |
72 |
| -/// expected_components_bag.day = Some(components::Day::NumericDayOfMonth); |
73 |
| -/// let actual_components_bag = components::Bag::from(&data_pattern); |
74 |
| -/// assert_eq!(actual_components_bag, expected_components_bag); |
75 |
| -/// ``` |
76 |
| -/// |
77 |
| -/// [`DateTimeFormatter`]: crate::DateTimeFormatter |
78 |
| -/// [`FormattedNeoDateTime::pattern`]: crate::FormattedNeoDateTime::pattern |
79 |
| -/// [`TypedDateTimeNames`]: crate::TypedDateTimeNames |
80 |
| -#[derive(Debug)] |
81 |
| -pub struct DateTimePattern { |
82 |
| - pattern: runtime::Pattern<'static>, |
83 |
| -} |
84 |
| - |
85 |
| -impl DateTimePattern { |
86 |
| - /// Creates a [`DateTimePattern`] from a pattern string. |
87 |
| - /// |
88 |
| - /// For more details on the syntax, see UTS 35: |
89 |
| - /// <https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns> |
90 |
| - pub fn try_from_pattern_str(pattern_str: &str) -> Result<Self, PatternError> { |
91 |
| - let pattern = runtime::Pattern::from_str(pattern_str)?; |
92 |
| - Ok(Self { pattern }) |
93 |
| - } |
94 |
| - |
95 |
| - #[doc(hidden)] // TODO(#4467): Internal API |
96 |
| - pub fn from_runtime_pattern(pattern: runtime::Pattern<'static>) -> Self { |
97 |
| - Self { pattern } |
98 |
| - } |
99 |
| - |
100 |
| - pub(crate) fn iter_items(&self) -> impl Iterator<Item = PatternItem> + '_ { |
101 |
| - self.pattern.items.iter() |
102 |
| - } |
103 |
| - |
104 |
| - pub(crate) fn as_borrowed(&self) -> DateTimePatternBorrowed { |
105 |
| - DateTimePatternBorrowed(&self.pattern) |
106 |
| - } |
107 |
| -} |
108 |
| - |
109 |
| -impl FromStr for DateTimePattern { |
110 |
| - type Err = PatternError; |
111 |
| - fn from_str(s: &str) -> Result<Self, Self::Err> { |
112 |
| - Self::try_from_pattern_str(s) |
113 |
| - } |
114 |
| -} |
115 |
| - |
116 |
| -// Check equality on the borrowed variant since it flattens the difference |
117 |
| -// between the three `Single` pattern variants, which is not public-facing |
118 |
| -impl PartialEq for DateTimePattern { |
119 |
| - fn eq(&self, other: &Self) -> bool { |
120 |
| - self.as_borrowed().eq(&other.as_borrowed()) |
121 |
| - } |
122 |
| -} |
123 |
| - |
124 |
| -impl Eq for DateTimePattern {} |
125 |
| - |
126 |
| -impl Writeable for DateTimePattern { |
127 |
| - fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result { |
128 |
| - self.pattern.write_to(sink) |
129 |
| - } |
130 |
| -} |
131 |
| - |
132 |
| -impl_display_with_writeable!(DateTimePattern); |
133 |
| - |
134 |
| -// Not clear if this needs to be public. For now, make it private. |
135 |
| -#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
136 |
| -pub(crate) struct DateTimePatternBorrowed<'a>(pub(crate) &'a runtime::Pattern<'a>); |
0 commit comments