//! \[Unstable\] Data provider struct definitions for this ICU4X component. //! //! <div class="stab unstable"> //! This code is considered unstable; it may change at any time, in breaking or non-breaking ways, //! including in SemVer minor releases. While the serde representation of data structs is guaranteed //! to be stable, their Rust representation might not be. Use with caution. //! </div> //! //! Read more about data providers: [`icu_provider`]
// Provider structs must be stable #![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]
usecrate::types::IsoWeekday; use core::str::FromStr; use icu_provider::prelude::*; use tinystr::TinyStr16; use zerovec::ZeroVec;
#[cfg(feature = "compiled_data")] #[derive(Debug)] /// Baked data /// /// <div class="stab unstable"> /// This code is considered unstable; it may change at any time, in breaking or non-breaking ways, /// including in SemVer minor releases. In particular, the `DataProvider` implementations are only /// guaranteed to match with this version's `*_unstable` providers. Use with caution. /// </div> pubstruct Baked;
#[cfg(feature = "compiled_data")] const _: () = { pubmod icu { pubusecrateas calendar; #[allow(unused_imports)] // baked data may or may not need this pubuse icu_locid_transform as locid_transform;
}
icu_calendar_data::make_provider!(Baked);
icu_calendar_data::impl_calendar_chinesecache_v1!(Baked);
icu_calendar_data::impl_calendar_dangicache_v1!(Baked);
icu_calendar_data::impl_calendar_islamicobservationalcache_v1!(Baked);
icu_calendar_data::impl_calendar_islamicummalquracache_v1!(Baked);
icu_calendar_data::impl_calendar_japanese_v1!(Baked);
icu_calendar_data::impl_calendar_japanext_v1!(Baked);
icu_calendar_data::impl_datetime_week_data_v1!(Baked);
icu_calendar_data::impl_datetime_week_data_v2!(Baked);
};
#[cfg(feature = "datagen")] /// The latest minimum set of keys required by this component. pubconst KEYS: &[DataKey] = &[
ChineseCacheV1Marker::KEY,
DangiCacheV1Marker::KEY,
IslamicObservationalCacheV1Marker::KEY,
IslamicUmmAlQuraCacheV1Marker::KEY,
JapaneseErasV1Marker::KEY,
JapaneseExtendedErasV1Marker::KEY,
WeekDataV2Marker::KEY, // We include the duplicate data for now, as icu_datetime loads it directly // https://github.com/unicode-org/icu4x/pull/4364#discussion_r1419877997
WeekDataV1Marker::KEY,
];
/// The date at which an era started /// /// The order of fields in this struct is important! /// /// <div class="stab unstable"> /// This code is considered unstable; it may change at any time, in breaking or non-breaking ways, /// including in SemVer minor releases. While the serde representation of data structs is guaranteed /// to be stable, their Rust representation might not be. Use with caution. /// </div> #[zerovec::make_ule(EraStartDateULE)] #[derive(
Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, yoke::Yokeable, zerofrom::ZeroFrom,
)] #[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_calendar::provider),
)] #[cfg_attr(feature = "serde", derive(serde::Deserialize))] pubstruct EraStartDate { /// The year the era started in pub year: i32, /// The month the era started in pub month: u8, /// The day the era started in pub day: u8,
}
/// A data structure containing the necessary era data for constructing a /// [`Japanese`](crate::japanese::Japanese) calendar object /// /// <div class="stab unstable"> /// This code is considered unstable; it may change at any time, in breaking or non-breaking ways, /// including in SemVer minor releases. While the serde representation of data structs is guaranteed /// to be stable, their Rust representation might not be. Use with caution. /// </div> #[icu_provider::data_struct(
marker(JapaneseErasV1Marker, "calendar/japanese@1", singleton),
marker(JapaneseExtendedErasV1Marker, "calendar/japanext@1", singleton)
)] #[derive(Debug, PartialEq, Clone, Default)] #[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_calendar::provider),
)] #[cfg_attr(feature = "serde", derive(serde::Deserialize))] pubstruct JapaneseErasV1<'data> { /// A map from era start dates to their era codes #[cfg_attr(feature = "serde", serde(borrow))] pub dates_to_eras: ZeroVec<'data, (EraStartDate, TinyStr16)>,
}
impl FromStr for EraStartDate { type Err = (); fn from_str(mut s: &str) -> Result<Self, Self::Err> { let sign = iflet Some(suffix) = s.strip_prefix('-') {
s = suffix;
-1
} else { 1
};
letmut split = s.split('-'); let year = split.next().ok_or(())?.parse::<i32>().map_err(|_| ())? * sign; let month = split.next().ok_or(())?.parse().map_err(|_| ())?; let day = split.next().ok_or(())?.parse().map_err(|_| ())?;
Ok(EraStartDate { year, month, day })
}
}
/// An ICU4X mapping to a subset of CLDR weekData. /// See CLDR-JSON's weekData.json for more context. /// /// <div class="stab unstable"> /// This code is considered unstable; it may change at any time, in breaking or non-breaking ways, /// including in SemVer minor releases. While the serde representation of data structs is guaranteed /// to be stable, their Rust representation might not be. Use with caution. /// </div> #[icu_provider::data_struct(marker(
WeekDataV1Marker, "datetime/week_data@1",
fallback_by = "region"
))] #[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_calendar::provider),
)] #[cfg_attr(feature = "serde", derive(serde::Deserialize))] #[allow(clippy::exhaustive_structs)] // used in data provider pubstruct WeekDataV1 { /// The first day of a week. pub first_weekday: IsoWeekday, /// For a given week, the minimum number of that week's days present in a given month or year for the week to be considered part of that month or year. pub min_week_days: u8,
}
/// An ICU4X mapping to a subset of CLDR weekData. /// See CLDR-JSON's weekData.json for more context. /// /// <div class="stab unstable"> /// This code is considered unstable; it may change at any time, in breaking or non-breaking ways, /// including in SemVer minor releases. While the serde representation of data structs is guaranteed /// to be stable, their Rust representation might not be. Use with caution. /// </div> #[icu_provider::data_struct(marker(
WeekDataV2Marker, "datetime/week_data@2",
fallback_by = "region"
))] #[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake), databake(path = icu_calendar::provider))] #[cfg_attr(feature = "serde", derive(serde::Deserialize))] #[allow(clippy::exhaustive_structs)] // used in data provider pubstruct WeekDataV2 { /// The first day of a week. pub first_weekday: IsoWeekday, /// For a given week, the minimum number of that week's days present in a given month or year for the week to be considered part of that month or year. pub min_week_days: u8, /// Bitset representing weekdays that are part of the 'weekend', for calendar purposes. /// The number of days can be different between locales, and may not be contiguous. pub weekend: WeekdaySet,
}
/// Bitset representing weekdays. // // This Bitset uses an [u8] to represent the weekend, thus leaving one bit free. // Each bit represents a day in the following order: // // ┌▷Mon // │┌▷Tue // ││┌▷Wed // │││┌▷Thu // ││││ ┌▷Fri // ││││ │┌▷Sat // ││││ ││┌▷Sun // ││││ │││ // 0b0000_1010 // // Please note that this is not a range, this are the discrete days representing a weekend. Other examples: // 0b0101_1000 -> Tue, Thu, Fri // 0b0000_0110 -> Sat, Sun #[derive(Clone, Copy, Debug, PartialEq)] pubstruct WeekdaySet(u8);
impl WeekdaySet { /// Returns whether the set contains the day. pubconstfn contains(&self, day: IsoWeekday) -> bool { self.0 & day.bit_value() != 0
}
}
impl WeekdaySet { /// Creates a new [WeekdaySet] using the provided days. pubconstfn new(days: &[IsoWeekday]) -> Self { letmut i = 0; letmut w = 0; #[allow(clippy::indexing_slicing)] while i < days.len() {
w |= days[i].bit_value();
i += 1;
} Self(w)
}
}
impl IsoWeekday { /// Defines the bit order used for encoding and reading weekend days. constfn bit_value(&self) -> u8 { matchself {
IsoWeekday::Monday => 1 << 6,
IsoWeekday::Tuesday => 1 << 5,
IsoWeekday::Wednesday => 1 << 4,
IsoWeekday::Thursday => 1 << 3,
IsoWeekday::Friday => 1 << 2,
IsoWeekday::Saturday => 1 << 1,
IsoWeekday::Sunday => 1 << 0,
}
}
}
#[cfg(feature = "datagen")] impl databake::Bake for WeekdaySet { fn bake(&self, ctx: &databake::CrateEnv) -> databake::TokenStream {
ctx.insert("icu_calendar"); let days = crate::week_of::WeekdaySetIterator::new(IsoWeekday::Monday, *self).map(|d| d.bake(ctx));
databake::quote! {
icu_calendar::provider::WeekdaySet::new(&[#(#days),*])
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.