Skip to content

Commit

Permalink
Add data loading for date pattern to TypedDateTimePattern
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Dec 9, 2023
1 parent 021bff2 commit 52a0ab1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion components/datetime/src/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod error;
pub mod hour_cycle;
mod item;
#[cfg(feature = "experimental")]
mod neo;
pub mod neo;
pub mod reference;
pub mod runtime;

Expand Down
71 changes: 67 additions & 4 deletions components/datetime/src/pattern/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use core::{marker::PhantomData, str::FromStr};
use core::marker::PhantomData;
use core::str::FromStr;

use super::{runtime, PatternError};
use crate::options::length;
use crate::provider::neo::*;
use crate::CldrCalendar;
use crate::Error;
use crate::{provider::neo::*, CldrCalendar};
use icu_provider::prelude::*;

#[derive(Debug)]
enum DateTimePatternInner {
Custom(runtime::Pattern<'static>),
Date(DataPayload<ErasedDatePatternV1Marker>),
Expand All @@ -21,6 +24,7 @@ enum DateTimePatternInner {
},
}

#[derive(Debug)]
pub struct TypedDateTimePattern<C: CldrCalendar> {
inner: DateTimePatternInner,
_calendar: PhantomData<C>,
Expand All @@ -35,6 +39,55 @@ impl<C: CldrCalendar> TypedDateTimePattern<C> {
})
}

/// Loads a [`TypedDateTimePattern`] for a date length.
///
/// # Examples
///
/// ```
/// use icu::calendar::Gregorian;
/// use icu::datetime::pattern::neo::TypedDateTimePattern;
/// use icu::datetime::options::length;
/// use icu::locid::locale;
///
/// let expected_pattern = TypedDateTimePattern::<Gregorian>::try_from_pattern_str("d MMM y").unwrap();
/// let actual_pattern = TypedDateTimePattern::<Gregorian>::try_new_date_with_length_unstable(
/// &icu::datetime::provider::Baked,
/// &locale!("fr").into(),
/// length::Date::Medium,
/// ).unwrap();
///
/// assert_eq!(expected_pattern, actual_pattern);
/// ```
pub fn try_new_date_with_length_unstable<P>(
provider: &P,
locale: &DataLocale,
length: length::Date,
) -> Result<Self, Error>
where
P: DataProvider<C::DatePatternV1Marker> + ?Sized,
{
let mut locale = locale.clone();
locale.set_aux(AuxiliaryKeys::from_subtag(aux::pattern_subtag_for(
match length {
length::Date::Full => aux::PatternLength::Full,
length::Date::Long => aux::PatternLength::Long,
length::Date::Medium => aux::PatternLength::Medium,
length::Date::Short => aux::PatternLength::Short,
},
None, // no hour cycle for date patterns
)));
let payload = provider
.load(DataRequest {
locale: &locale,
metadata: Default::default(),
})?
.take_payload()?;
Ok(Self {
inner: DateTimePatternInner::Date(payload.cast()),
_calendar: PhantomData,
})
}

pub(crate) fn as_borrowed(&self) -> DateTimePatternBorrowed {
let borrowed_inner = match self.inner {
DateTimePatternInner::Custom(ref pattern) => {
Expand Down Expand Up @@ -69,7 +122,17 @@ impl<C: CldrCalendar> FromStr for TypedDateTimePattern<C> {
}
}

#[derive(Debug, Copy, Clone)]
// Check equality on the borrowed variant since it flattens the difference
// between the three `Single`` pattern variants, which is not public-facing
impl<C: CldrCalendar> PartialEq for TypedDateTimePattern<C> {
fn eq(&self, other: &Self) -> bool {
self.as_borrowed().eq(&other.as_borrowed())
}
}

impl<C: CldrCalendar> Eq for TypedDateTimePattern<C> {}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum DateTimePatternBorrowedInner<'a> {
Single(&'a runtime::Pattern<'a>),
DateTime {
Expand All @@ -80,7 +143,7 @@ enum DateTimePatternBorrowedInner<'a> {
}

// Not clear if this needs to be public. For now, make it private.
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) struct DateTimePatternBorrowed<'a> {
inner: DateTimePatternBorrowedInner<'a>,
}

0 comments on commit 52a0ab1

Please sign in to comment.