1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
// 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 ).
//! Temporary module for neo datetime skeletons (Semantic Skeleta)
#[cfg(feature = "serde")]
use crate::neo_serde::*;
#[cfg(feature = "datagen")]
use crate::options::{self, length};
use icu_timezone::scaffold::IntoOption;
/// A specification for the length of a date or component of a date.
///
/// Contrary to [`crate::options::length::Time`] and
/// [`crate::options::length::Date`], this has only three levels, with no
/// `Full`; this is because `Full` corresponds to additional components,
/// rather than to making the components wider than in `Long`.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[repr(u8)] // discriminants come from symbol count in UTS 35
#[non_exhaustive]
pub enum NeoSkeletonLength {
/// A long date, typically spelled-out, as in “January 1, 2000”.
Long = 4,
/// A medium-sized date; typically abbreviated, as in “Jan. 1, 2000”.
Medium = 3,
/// A short date; typically numeric, as in “1/1/2000”.
Short = 1,
}
impl IntoOption<NeoSkeletonLength> for NeoSkeletonLength {
#[inline]
fn into_option(self) -> Option<Self> {
Some(self)
}
}
impl NeoSkeletonLength {
/// All values of this enum.
pub const VALUES: &'static [Self] = &[Self::Long, Self::Medium, Self::Short];
/// Returns the date style corresponding to this length.
#[cfg(feature = "datagen")]
pub fn to_date_style(self) -> options::length::Date {
match self {
Self::Long => options::length::Date::Long,
Self::Medium => options::length::Date::Medium,
Self::Short => options::length::Date::Short,
}
}
/// Returns the time style corresponding to this length.
#[cfg(feature = "datagen")]
pub fn to_time_style(self) -> options::length::Time {
// Note: For now, make "long" and "medium" both map to "medium".
// This could be improved in light of additional data.
match self {
Self::Long => options::length::Time::Medium,
Self::Medium => options::length::Time::Medium,
Self::Short => options::length::Time::Short,
}
}
}
/// The alignment context of the formatted string.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[non_exhaustive]
pub enum Alignment {
/// Align fields as the locale specifies them to be aligned.
///
/// This is the default option.
Auto,
/// Align fields as appropriate for a column layout. For example:
///
/// | US Holiday | Date |
/// |--------------|------------|
/// | Memorial Day | 05/26/2025 |
/// | Labor Day | 09/01/2025 |
/// | Veterans Day | 11/11/2025 |
///
/// This option causes numeric fields to be padded when necessary. It does
/// not impact whether a numeric or spelled-out field is chosen.
Column,
}
impl IntoOption<Alignment> for Alignment {
#[inline]
fn into_option(self) -> Option<Self> {
Some(self)
}
}
/// A specification of how to render the year and the era.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[non_exhaustive]
pub enum YearStyle {
/// Display the century and/or era when needed to disambiguate the year,
/// based on locale preferences.
///
/// This is the default option.
///
/// Examples:
///
/// - `1000 BC`
/// - `77 AD`
/// - `1900`
/// - `'24`
Auto,
/// Always display the century, and display the era when needed to
/// disambiguate the year, based on locale preferences.
///
/// Examples:
///
/// - `1000 BC`
/// - `77 AD`
/// - `1900`
/// - `2024`
Full,
/// Always display the century and era.
///
/// Examples:
///
/// - `1000 BC`
/// - `77 AD`
/// - `1900 AD`
/// - `2024 AD`
Always,
// TODO(#4478): add Hide and Never options once there is data to back them
}
impl IntoOption<YearStyle> for YearStyle {
#[inline]
fn into_option(self) -> Option<Self> {
Some(self)
}
}
/// A specification for how precisely to display the time of day.
///
/// The examples below are based on the following inputs and hour cycles:
///
/// 1. 11 o'clock with 12-hour time
/// 2. 16:20 (4:20 pm) with 24-hour time
/// 3. 7:15:01.85 with 24-hour time
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "serde",
serde(from = "TimePrecisionSerde", into = "TimePrecisionSerde")
)]
#[non_exhaustive]
pub enum TimePrecision {
/// Always display the hour. Display smaller fields if they are nonzero.
///
/// Examples:
///
/// 1. `11 am`
/// 2. `16:20`
/// 3. `07:15:01.85`
HourPlus,
/// Always display the hour. Hide all other time fields.
///
/// Examples:
///
/// 1. `11 am`
/// 2. `16h`
/// 3. `07h`
HourExact,
/// Always display the hour and minute. Display the second if nonzero.
///
/// Examples:
///
/// 1. `11:00 am`
/// 2. `16:20`
/// 3. `07:15:01.85`
MinutePlus,
/// Always display the hour and minute. Hide the second.
///
/// Examples:
///
/// 1. `11:00 am`
/// 2. `16:20`
/// 3. `07:15`
MinuteExact,
/// Display the hour, minute, and second. Display fractional seconds if nonzero.
///
/// This is the default.
///
/// Examples:
///
/// 1. `11:00:00 am`
/// 2. `16:20:00`
/// 3. `07:15:01.85`
SecondPlus,
/// Display the hour, minute, and second with the given number of
/// fractional second digits.
///
/// Examples with [`FractionalSecondDigits::F1`]:
///
/// 1. `11:00:00.0 am`
/// 2. `16:20:00.0`
/// 3. `07:15:01.8`
SecondExact(FractionalSecondDigits),
}
impl IntoOption<TimePrecision> for TimePrecision {
#[inline]
fn into_option(self) -> Option<Self> {
Some(self)
}
}
impl TimePrecision {
/// Converts a [`length::Time`] to its nearest [`TimePrecision`].
#[doc(hidden)] // the types involved in this mapping may change
#[cfg(feature = "datagen")]
pub fn from_time_length(time_length: length::Time) -> Self {
match time_length {
length::Time::Full => todo!(),
length::Time::Long => todo!(),
length::Time::Medium => Self::SecondPlus,
length::Time::Short => Self::MinuteExact,
}
}
}
/// A specification for how many fractional second digits to display.
///
/// For example, to display the time with millisecond precision, use
/// [`FractionalSecondDigits::F3`].
///
/// Lower-precision digits will be truncated.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "u8", into = "u8"))]
#[non_exhaustive]
pub enum FractionalSecondDigits {
/// Zero fractional digits. This is the default.
F0,
/// One fractional digit (tenths of a second).
F1,
/// Two fractional digits (hundredths of a second).
F2,
/// Three fractional digits (thousandths of a second).
F3,
/// Four fractional digits.
F4,
/// Five fractional digits.
F5,
/// Six fractional digits.
F6,
/// Seven fractional digits.
F7,
/// Eight fractional digits.
F8,
/// Nine fractional digits.
F9,
}
/// An error from constructing [`FractionalSecondDigits`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, displaydoc::Display)]
#[non_exhaustive]
pub enum FractionalSecondError {
/// The provided value is out of range (0-9).
OutOfRange,
}
impl From<FractionalSecondDigits> for u8 {
fn from(value: FractionalSecondDigits) -> u8 {
use FractionalSecondDigits::*;
match value {
F0 => 0,
F1 => 1,
F2 => 2,
F3 => 3,
F4 => 4,
F5 => 5,
F6 => 6,
F7 => 7,
F8 => 8,
F9 => 9,
}
}
}
impl TryFrom<u8> for FractionalSecondDigits {
type Error = FractionalSecondError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
use FractionalSecondDigits::*;
match value {
0 => Ok(F0),
1 => Ok(F1),
2 => Ok(F2),
3 => Ok(F3),
4 => Ok(F4),
5 => Ok(F5),
6 => Ok(F6),
7 => Ok(F7),
8 => Ok(F8),
9 => Ok(F9),
_ => Err(FractionalSecondError::OutOfRange),
}
}
}