| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use crate::FormattedDateTime; |
| 6 |
use crate::options::Alignment; |
| 7 |
use crate::provider::fields; |
| 8 |
use crate::provider::pattern::PatternItem; |
| 9 |
use crate::provider::pattern::runtime; |
| 10 |
use crate::provider::range_patterns::RangePatternInfoBorrowed; |
| 11 |
use crate::provider::semantic_skeletons::GluePattern; |
| 12 |
use core::fmt; |
| 13 |
use writeable::{PartsWrite, Writeable}; |
| 14 |
use zerovec::ule::AsULE; |
| 15 |
|
| 16 |
/// The formatting result of a date/time range. |
| 17 |
#[derive(Debug)] |
| 18 |
pub struct FormattedDateRange<'l>(pub(crate) FormattedDateRangeInner<'l>); |
| 19 |
|
| 20 |
#[derive(Debug)] |
| 21 |
#[allow(clippy::large_enum_variant)] // Short-lived formatting result, avoid heap allocation |
| 22 |
#[allow(dead_code, reason = "#5448")] |
| 23 |
pub(crate) enum FormattedDateRangeInner<'l> { |
| 24 |
/// The range resolved to a single date/time (start and end are equal). |
| 25 |
Single(FormattedDateTime<'l>), |
| 26 |
/// The range is formatted using a greatest difference pattern. |
| 27 |
GreatestDifference(FormattedGreatestDifference<'l>), |
| 28 |
/// The range has a single date but a range of times. |
| 29 |
TimeRangeMixed(FormattedTimeRangeMixed<'l>), |
| 30 |
/// The range is formatted using the fallback range pattern. |
| 31 |
Fallback(FormattedRangeFallback<'l>), |
| 32 |
} |
| 33 |
|
| 34 |
impl Writeable for FormattedDateRange<'_> { |
| 35 |
fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> Result<(), fmt::Error> { |
| 36 |
match &self.0 { |
| 37 |
FormattedDateRangeInner::Single(x) => x.write_to_parts(sink), |
| 38 |
FormattedDateRangeInner::GreatestDifference(x) => x.write_to_parts(sink), |
| 39 |
FormattedDateRangeInner::TimeRangeMixed(x) => x.write_to_parts(sink), |
| 40 |
FormattedDateRangeInner::Fallback(x) => x.write_to_parts(sink), |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
fn writeable_length_hint(&self) -> writeable::LengthHint { |
| 45 |
match &self.0 { |
| 46 |
FormattedDateRangeInner::Single(x) => x.writeable_length_hint(), |
| 47 |
FormattedDateRangeInner::GreatestDifference(x) => x.writeable_length_hint(), |
| 48 |
FormattedDateRangeInner::TimeRangeMixed(x) => x.writeable_length_hint(), |
| 49 |
FormattedDateRangeInner::Fallback(x) => x.writeable_length_hint(), |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
writeable::impl_display_with_writeable!(FormattedDateRange<'_>); |
| 55 |
|
| 56 |
/// The formatting result of a date/time range where the start and end dates |
| 57 |
/// differ by a specific field (e.g., day, month, year) and are formatted |
| 58 |
/// using a greatest-difference pattern. |
| 59 |
#[derive(Debug)] |
| 60 |
pub(crate) struct FormattedGreatestDifference<'l> { |
| 61 |
pub(crate) start: FormattedDateTime<'l>, |
| 62 |
#[allow(dead_code, reason = "#5448")] |
| 63 |
pub(crate) end: FormattedDateTime<'l>, |
| 64 |
pub(crate) pattern_info: RangePatternInfoBorrowed<'l>, |
| 65 |
pub(crate) glue: &'l GluePattern<'l>, |
| 66 |
pub(crate) alignment: Option<Alignment>, |
| 67 |
} |
| 68 |
|
| 69 |
impl Writeable for FormattedGreatestDifference<'_> { |
| 70 |
fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> Result<(), fmt::Error> { |
| 71 |
match &self.pattern_info { |
| 72 |
RangePatternInfoBorrowed::FullRange(pattern) => { |
| 73 |
let (start_pattern, end_pattern) = pattern.split_on_repeated_field(); |
| 74 |
let start_side = FormattedSingleSide { |
| 75 |
datetime: &self.start, |
| 76 |
pattern: start_pattern, |
| 77 |
alignment: self.alignment, |
| 78 |
}; |
| 79 |
let end_side = FormattedSingleSide { |
| 80 |
datetime: &self.end, |
| 81 |
pattern: end_pattern, |
| 82 |
alignment: self.alignment, |
| 83 |
}; |
| 84 |
start_side.write_to_parts(sink)?; |
| 85 |
end_side.write_to_parts(sink) |
| 86 |
} |
| 87 |
RangePatternInfoBorrowed::Symmetric(pattern) => { |
| 88 |
let start_side = FormattedSingleSide { |
| 89 |
datetime: &self.start, |
| 90 |
pattern: *pattern, |
| 91 |
alignment: self.alignment, |
| 92 |
}; |
| 93 |
let end_side = FormattedSingleSide { |
| 94 |
datetime: &self.end, |
| 95 |
pattern: *pattern, |
| 96 |
alignment: self.alignment, |
| 97 |
}; |
| 98 |
write_glue_pattern(sink, self.glue, &start_side, &end_side) |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
#[derive(Debug)] |
| 105 |
struct FormattedSingleSide<'a, 'l> { |
| 106 |
datetime: &'a FormattedDateTime<'l>, |
| 107 |
pattern: runtime::PatternBorrowed<'l>, |
| 108 |
alignment: Option<Alignment>, |
| 109 |
} |
| 110 |
|
| 111 |
impl Writeable for FormattedSingleSide<'_, '_> { |
| 112 |
fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> Result<(), fmt::Error> { |
| 113 |
let metadata = self.pattern.metadata; |
| 114 |
let decimal_formatter = self.datetime.names.decimal_formatter; |
| 115 |
|
| 116 |
for item in self.pattern.items.iter() { |
| 117 |
match item { |
| 118 |
PatternItem::Literal(ch) => sink.write_char(ch)?, |
| 119 |
PatternItem::Field(field) => { |
| 120 |
let mut field = field; |
| 121 |
let alignment = self.alignment.unwrap_or_default(); |
| 122 |
if matches!(alignment, Alignment::Column) |
| 123 |
&& field.length == fields::FieldLength::One |
| 124 |
&& matches!( |
| 125 |
field.symbol, |
| 126 |
fields::FieldSymbol::Month(_) |
| 127 |
| fields::FieldSymbol::Day(_) |
| 128 |
| fields::FieldSymbol::Week(_) |
| 129 |
| fields::FieldSymbol::Hour(_) |
| 130 |
) |
| 131 |
{ |
| 132 |
field.length = fields::FieldLength::Two; |
| 133 |
} |
| 134 |
|
| 135 |
let r = crate::format::datetime::try_write_field( |
| 136 |
field, |
| 137 |
metadata, |
| 138 |
&self.datetime.input, |
| 139 |
&self.datetime.names, |
| 140 |
decimal_formatter, |
| 141 |
sink, |
| 142 |
); |
| 143 |
|
| 144 |
match r { |
| 145 |
Ok(Ok(())) => {} |
| 146 |
Err(fmt::Error) => return Err(fmt::Error), |
| 147 |
Ok(Err(e)) => { |
| 148 |
debug_assert!(false, "unexpected error in FormattedSingleSide: {e:?}"); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
Ok(()) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
#[derive(Debug)] |
| 159 |
pub(crate) struct FormattedTimeRangeMixed<'l> { |
| 160 |
pub(crate) date: FormattedDateTime<'l>, |
| 161 |
pub(crate) time_range: FormattedGreatestDifference<'l>, |
| 162 |
pub(crate) glue: &'l GluePattern<'l>, |
| 163 |
} |
| 164 |
|
| 165 |
impl Writeable for FormattedTimeRangeMixed<'_> { |
| 166 |
fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> Result<(), fmt::Error> { |
| 167 |
write_glue_pattern(sink, self.glue, &self.time_range, &self.date) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
#[derive(Debug)] |
| 172 |
pub(crate) struct FormattedRangeFallback<'l> { |
| 173 |
pub(crate) start: FormattedDateTime<'l>, |
| 174 |
pub(crate) end: FormattedDateTime<'l>, |
| 175 |
pub(crate) glue: &'l GluePattern<'l>, |
| 176 |
} |
| 177 |
|
| 178 |
impl Writeable for FormattedRangeFallback<'_> { |
| 179 |
fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> Result<(), fmt::Error> { |
| 180 |
write_glue_pattern(sink, self.glue, &self.start, &self.end) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
pub(crate) fn write_glue_pattern<W, S, E>( |
| 185 |
sink: &mut W, |
| 186 |
glue: &GluePattern<'_>, |
| 187 |
start: &S, |
| 188 |
end: &E, |
| 189 |
) -> fmt::Result |
| 190 |
where |
| 191 |
W: PartsWrite + ?Sized, |
| 192 |
S: Writeable + ?Sized, |
| 193 |
E: Writeable + ?Sized, |
| 194 |
{ |
| 195 |
for generic_item_ule in glue.pattern.items.as_ule_slice().iter() { |
| 196 |
match generic_item_ule.as_pattern_item_ule() { |
| 197 |
Ok(pattern_item_ule) => { |
| 198 |
let pattern_item = <PatternItem as AsULE>::from_unaligned(*pattern_item_ule); |
| 199 |
if let PatternItem::Literal(ch) = pattern_item { |
| 200 |
sink.write_char(ch)?; |
| 201 |
} else { |
| 202 |
debug_assert!(false, "Expected only literals in glue pattern"); |
| 203 |
} |
| 204 |
} |
| 205 |
Err(0) => { |
| 206 |
start.write_to_parts(sink)?; |
| 207 |
} |
| 208 |
Err(1) => { |
| 209 |
end.write_to_parts(sink)?; |
| 210 |
} |
| 211 |
Err(_) => { |
| 212 |
debug_assert!(false, "Unexpected placeholder index in glue pattern"); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
Ok(()) |
| 217 |
} |