Skip to content

Commit

Permalink
remove iso specialisations
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian committed Jan 15, 2025
1 parent f6391e4 commit 846fcff
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 278 deletions.
56 changes: 6 additions & 50 deletions components/calendar/src/ixdtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,64 +40,18 @@ impl From<IxdtfError> for ParseError {
}
}

impl Date<Iso> {
/// Creates a [`Date`] in the ISO-8601 calendar from an IXDTF syntax string.
///
/// Ignores any calendar annotations in the string.
///
/// ✨ *Enabled with the `ixdtf` Cargo feature.*
///
/// # Examples
///
/// ```
/// use icu::calendar::Date;
///
/// let date = Date::try_iso_from_str("2024-07-17").unwrap();
///
/// assert_eq!(date.year().era_year_or_extended(), 2024);
/// assert_eq!(
/// date.month().standard_code,
/// icu::calendar::types::MonthCode(tinystr::tinystr!(4, "M07"))
/// );
/// assert_eq!(date.day_of_month().0, 17);
///
/// // Calendar annotations are ignored:
/// assert_eq!(date, Date::try_iso_from_str("2024-07-17[u-ca=buddhist]").unwrap());
/// ```
pub fn try_iso_from_str(ixdtf_str: &str) -> Result<Self, ParseError> {
Self::try_iso_from_utf8(ixdtf_str.as_bytes())
}

/// Creates a [`Date`] in the ISO-8601 calendar from an IXDTF syntax string.
///
/// See [`Self::try_iso_from_str()`].
///
/// ✨ *Enabled with the `ixdtf` Cargo feature.*
pub fn try_iso_from_utf8(ixdtf_str: &[u8]) -> Result<Self, ParseError> {
let ixdtf_record = IxdtfParser::from_utf8(ixdtf_str).parse()?;
Self::try_iso_from_ixdtf_record(&ixdtf_record)
}

#[doc(hidden)]
pub fn try_iso_from_ixdtf_record(ixdtf_record: &IxdtfParseRecord) -> Result<Self, ParseError> {
let date_record = ixdtf_record.date.ok_or(ParseError::MissingFields)?;
let date = Self::try_new_iso(date_record.year, date_record.month, date_record.day)?;
Ok(date)
}
}

impl FromStr for Date<Iso> {
type Err = ParseError;
fn from_str(ixdtf_str: &str) -> Result<Self, Self::Err> {
Self::try_iso_from_str(ixdtf_str)
Self::try_from_str(ixdtf_str, Iso)
}
}

impl<A: AsCalendar> Date<A> {
/// Creates a [`Date`] in the given calendar from an IXDTF syntax string.
///
/// Returns an error if the string has a calendar annotation that does not
/// match the calendar argument.
/// match the calendar argument, unless the argument is [`Iso`].
///
/// ✨ *Enabled with the `ixdtf` Cargo feature.*
///
Expand Down Expand Up @@ -139,15 +93,17 @@ impl<A: AsCalendar> Date<A> {
ixdtf_record: &IxdtfParseRecord,
calendar: A,
) -> Result<Self, ParseError> {
let iso = Date::try_iso_from_ixdtf_record(ixdtf_record)?;
let date_record = ixdtf_record.date.ok_or(ParseError::MissingFields)?;
let iso = Date::try_new_iso(date_record.year, date_record.month, date_record.day)?;

if let Some(ixdtf_calendar) = ixdtf_record.calendar {
let parsed_calendar = crate::AnyCalendarKind::get_for_bcp47_bytes(ixdtf_calendar)
.ok_or(ParseError::UnknownCalendar)?;
let expected_calendar = calendar
.as_calendar()
.any_calendar_kind()
.ok_or(ParseError::UnknownCalendar)?;
if parsed_calendar != expected_calendar {
if parsed_calendar != expected_calendar && expected_calendar != AnyCalendarKind::Iso {
return Err(ParseError::MismatchedCalendar(
expected_calendar,
parsed_calendar,
Expand Down
4 changes: 2 additions & 2 deletions components/datetime/src/combo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::{provider::neo::*, scaffold::*};
/// .unwrap();
///
/// let zdt = IxdtfParser::new()
/// .try_location_only_iso_from_str("2024-10-18T15:44[America/Los_Angeles]")
/// .try_location_only_from_str("2024-10-18T15:44[America/Los_Angeles]", formatter.calendar())
/// .unwrap();
///
/// assert_writeable_eq!(
Expand Down Expand Up @@ -90,7 +90,7 @@ use crate::{provider::neo::*, scaffold::*};
/// .unwrap();
///
/// let zdt = IxdtfParser::new()
/// .try_location_only_iso_from_str("2024-10-18T15:44[America/Los_Angeles]")
/// .try_location_only_from_str("2024-10-18T15:44[America/Los_Angeles]", formatter.calendar())
/// .unwrap();
///
/// assert_writeable_eq!(
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/tests/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn test_fixture(fixture_name: &str, file: &str) {
continue;
}
};
let input_iso = DateTime::try_iso_from_str(&fx.input.value).unwrap();
let input_iso = DateTime::try_from_str(&fx.input.value, Iso).unwrap();

let input_buddhist = DateTime::try_from_str(&fx.input.value, Buddhist).unwrap();
let input_chinese = DateTime::try_from_str(&fx.input.value, Chinese::new()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion components/icu/examples/jiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn main() -> Result<(), Box<dyn core::error::Error>> {
// Alternatively, the ICU ZonedDateTime can be parsed from a serialized IXDTF string.
assert_eq!(
IxdtfParser::new()
.try_iso_from_str(&zoned.to_string())
.try_from_str(&zoned.to_string(), icu::calendar::Iso)
.unwrap(),
zoned_date_time
);
Expand Down
Loading

0 comments on commit 846fcff

Please sign in to comment.