Skip to content

Commit

Permalink
Make field module be unstable and add stable enums for DateTimeNames (
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Jan 9, 2025
1 parent ebc88a4 commit 9ef8e64
Show file tree
Hide file tree
Showing 40 changed files with 760 additions and 483 deletions.
4 changes: 3 additions & 1 deletion components/datetime/benches/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use icu_datetime::{fields::components, fieldsets::serde::CompositeFieldSetSerde, options};
use icu_datetime::{
fieldsets::serde::CompositeFieldSetSerde, options, provider::fields::components,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down
8 changes: 4 additions & 4 deletions components/datetime/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
use crate::fieldsets::Combo;
use crate::raw::neo::RawOptions;
use crate::scaffold::GetField;
use crate::{fields, fieldsets};
use crate::{fieldsets, provider};
use icu_provider::prelude::*;

/// An enumeration over all possible date field sets.
Expand Down Expand Up @@ -372,7 +372,7 @@ macro_rules! impl_attrs {
(@zone, $type:path, [$($variant:ident),+,]) => {
impl_attrs! { @composite, $type, Zone }
impl $type {
pub(crate) fn to_field(self) -> (fields::TimeZone, fields::FieldLength) {
pub(crate) fn to_field(self) -> (provider::fields::TimeZone, provider::fields::FieldLength) {
match self {
$(
Self::$variant(variant) => variant.to_field(),
Expand Down Expand Up @@ -453,9 +453,9 @@ impl_attrs! {
impl TimeFieldSet {
pub(crate) const fn id_str_for_hour_cycle(
self,
hour_cycle: Option<fields::Hour>,
hour_cycle: Option<provider::fields::Hour>,
) -> &'static DataMarkerAttributes {
use fields::Hour::*;
use provider::fields::Hour::*;
match hour_cycle {
None => Self::ATTR_T,
Some(H11 | H12) => Self::ATTR_T12,
Expand Down
19 changes: 16 additions & 3 deletions components/datetime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::{fields::Field, pattern::PatternLoadError};
use crate::pattern::PatternLoadError;
use displaydoc::Display;
use icu_calendar::{
any_calendar::AnyCalendarKind,
Expand Down Expand Up @@ -36,6 +36,10 @@ impl From<DataError> for DateTimeFormatterLoadError {
}
}

/// The specific field for which an error occurred.
#[derive(Display, Debug, Copy, Clone, PartialEq)]
pub struct ErrorField(pub(crate) crate::provider::fields::Field);

/// An error from mixing calendar types in a formatter.
#[derive(Display, Debug, Copy, Clone, PartialEq)]
#[displaydoc("DateTimeFormatter for {this_kind} calendar was given a {date_kind:?} calendar")]
Expand Down Expand Up @@ -97,7 +101,7 @@ pub enum DateTimeWriteError {
/// The output will contain fallback values using field identifiers (such as `tue` for `IsoWeekday::Tuesday`,
/// `M02` for month 2, etc.).
#[displaydoc("Names for {0:?} not loaded")]
NamesNotLoaded(Field),
NamesNotLoaded(ErrorField),
/// An input field (such as "hour" or "month") is missing.
///
/// This *only* happens if the formatter has been created using
Expand All @@ -107,12 +111,21 @@ pub enum DateTimeWriteError {
/// The output will contain the string `{X}` instead, where `X` is the symbol for which the input is missing.
#[displaydoc("Incomplete input, missing value for {0:?}")]
MissingInputField(&'static str),
/// The pattern contains a field that has a valid symbol but invalid length.
///
/// This *only* happens if the formatter has been created using
/// [`TypedDateTimeNames::with_pattern_unchecked`], and the pattern contains
/// a field with a length not supported in formatting.
///
/// The output will contain fallback values similar to [`DateTimeWriteError::NamesNotLoaded`].
#[displaydoc("Field length for {0:?} is invalid")]
UnsupportedLength(ErrorField),
/// Unsupported field
///
/// This *only* happens if the formatter has been created using
/// [`TypedDateTimeNames::with_pattern_unchecked`], and the pattern contains unsupported fields.
///
/// The output will contain the string `{unsupported:X}`, where `X` is the symbol of the unsupported field.
#[displaydoc("Unsupported field {0:?}")]
UnsupportedField(Field),
UnsupportedField(ErrorField),
}
1 change: 0 additions & 1 deletion components/datetime/src/fieldsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub mod enums;
pub use crate::combo::Combo;

use crate::{
fields,
options::*,
provider::{neo::*, time_zones::tz, *},
raw::neo::RawOptions,
Expand Down
43 changes: 31 additions & 12 deletions components/datetime/src/format/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use super::time_zone::{FormatTimeZone, FormatTimeZoneError, Iso8601Format, TimeZoneFormatterUnit};
use crate::error::DateTimeWriteError;
use crate::fields::{self, FieldLength, FieldSymbol, Second, Year};
use crate::error::{DateTimeWriteError, ErrorField};
use crate::input::ExtractedInput;
use crate::provider::fields::{self, FieldLength, FieldSymbol, Second, Year};
use crate::provider::pattern::runtime::PatternMetadata;
use crate::provider::pattern::PatternItem;
use crate::{parts, pattern::*};
Expand Down Expand Up @@ -138,8 +138,13 @@ where
let era_symbol = datetime_names
.get_name_for_era(l, era)
.map_err(|e| match e {
GetSymbolForEraError::Invalid => DateTimeWriteError::InvalidEra(era),
GetSymbolForEraError::NotLoaded => DateTimeWriteError::NamesNotLoaded(field),
GetNameForEraError::InvalidEraCode => DateTimeWriteError::InvalidEra(era),
GetNameForEraError::InvalidFieldLength => {
DateTimeWriteError::UnsupportedLength(ErrorField(field))
}
GetNameForEraError::NotLoaded => {
DateTimeWriteError::NamesNotLoaded(ErrorField(field))
}
});
match era_symbol {
Err(e) => {
Expand Down Expand Up @@ -181,14 +186,17 @@ where
})
})?;
return Ok(Err(match e {
GetSymbolForCyclicYearError::Invalid { max } => {
GetNameForCyclicYearError::InvalidYearNumber { max } => {
DateTimeWriteError::InvalidCyclicYear {
value: cyclic.get() as usize,
max,
}
}
GetSymbolForCyclicYearError::NotLoaded => {
DateTimeWriteError::NamesNotLoaded(field)
GetNameForCyclicYearError::InvalidFieldLength => {
DateTimeWriteError::UnsupportedLength(ErrorField(field))
}
GetNameForCyclicYearError::NotLoaded => {
DateTimeWriteError::NamesNotLoaded(ErrorField(field))
}
}));
}
Expand Down Expand Up @@ -255,11 +263,14 @@ where
w.with_part(Part::ERROR, |w| w.write_str(&month.formatting_code.0))
})?;
Err(match e {
GetNameForMonthError::Invalid => {
GetNameForMonthError::InvalidMonthCode => {
DateTimeWriteError::InvalidMonthCode(month.formatting_code)
}
GetNameForMonthError::InvalidFieldLength => {
DateTimeWriteError::UnsupportedLength(ErrorField(field))
}
GetNameForMonthError::NotLoaded => {
DateTimeWriteError::NamesNotLoaded(field)
DateTimeWriteError::NamesNotLoaded(ErrorField(field))
}
})
}
Expand All @@ -272,7 +283,12 @@ where
match datetime_names
.get_name_for_weekday(weekday, l, iso_weekday)
.map_err(|e| match e {
GetNameForWeekdayError::NotLoaded => DateTimeWriteError::NamesNotLoaded(field),
GetNameForWeekdayError::InvalidFieldLength => {
DateTimeWriteError::UnsupportedLength(ErrorField(field))
}
GetNameForWeekdayError::NotLoaded => {
DateTimeWriteError::NamesNotLoaded(ErrorField(field))
}
}) {
Err(e) => {
w.with_part(PART, |w| {
Expand Down Expand Up @@ -392,8 +408,11 @@ where
})
})?;
Err(match e {
GetNameForDayPeriodError::InvalidFieldLength => {
DateTimeWriteError::UnsupportedLength(ErrorField(field))
}
GetNameForDayPeriodError::NotLoaded => {
DateTimeWriteError::NamesNotLoaded(field)
DateTimeWriteError::NamesNotLoaded(ErrorField(field))
}
})
}
Expand Down Expand Up @@ -551,7 +570,7 @@ fn perform_timezone_fallback(
Err(DateTimeWriteError::FixedDecimalFormatterNotLoaded)
}
FormatTimeZoneError::NamesNotLoaded => {
Err(DateTimeWriteError::NamesNotLoaded(field))
Err(DateTimeWriteError::NamesNotLoaded(ErrorField(field)))
}
FormatTimeZoneError::MissingInputField(f) => {
Err(DateTimeWriteError::MissingInputField(f))
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/format/time_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::pattern::TimeZoneDataPayloadsBorrowed;
use crate::provider::time_zones::MetazoneId;
use crate::{fields::FieldLength, input::ExtractedInput};
use crate::{input::ExtractedInput, provider::fields::FieldLength};
use core::fmt;
use fixed_decimal::SignedFixedDecimal;
use icu_calendar::{Date, Iso, Time};
Expand Down
2 changes: 0 additions & 2 deletions components/datetime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,10 @@ extern crate alloc;
mod combo;
mod error;
mod external_loaders;
pub mod fields;
pub mod fieldsets;
mod format;
pub mod input;
mod neo;
pub mod neo_pattern;
#[cfg(all(feature = "experimental", feature = "serde"))]
mod neo_serde;
pub mod options;
Expand Down
5 changes: 0 additions & 5 deletions components/datetime/src/neo_pattern.rs

This file was deleted.

Loading

0 comments on commit 9ef8e64

Please sign in to comment.