| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
//! π§ \[Unstable\] Data provider struct definitions for this ICU4X component. |
| 6 |
//! |
| 7 |
//! <div class="stab unstable"> |
| 8 |
//! π§ This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 9 |
//! including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 10 |
//! to be stable, their Rust representation might not be. Use with caution. |
| 11 |
//! </div> |
| 12 |
//! |
| 13 |
//! Read more about data providers: [`icu_provider`] |
| 14 |
|
| 15 |
// Provider structs must be stable |
| 16 |
#![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)] |
| 17 |
|
| 18 |
use core::fmt::Debug; |
| 19 |
use core::num::NonZeroU8; |
| 20 |
|
| 21 |
use crate::types::Weekday; |
| 22 |
use icu_locale_core::preferences::extensions::unicode::keywords::{ |
| 23 |
CalendarAlgorithm, HijriCalendarAlgorithm, |
| 24 |
}; |
| 25 |
use icu_provider::fallback::{LocaleFallbackConfig, LocaleFallbackPriority}; |
| 26 |
use icu_provider::prelude::*; |
| 27 |
use tinystr::TinyAsciiStr; |
| 28 |
|
| 29 |
#[cfg(feature = "compiled_data")] |
| 30 |
#[derive(Debug)] |
| 31 |
/// Baked data |
| 32 |
/// |
| 33 |
/// <div class="stab unstable"> |
| 34 |
/// π§ This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 35 |
/// including in SemVer minor releases. In particular, the `DataProvider` implementations are only |
| 36 |
/// guaranteed to match with this version's `*_unstable` providers. Use with caution. |
| 37 |
/// </div> |
| 38 |
pub struct Baked; |
| 39 |
|
| 40 |
#[cfg(feature = "compiled_data")] |
| 41 |
#[allow(unused_imports)] |
| 42 |
const _: () = { |
| 43 |
use icu_calendar_data::*; |
| 44 |
pub mod icu { |
| 45 |
pub use crate as calendar; |
| 46 |
} |
| 47 |
make_provider!(Baked); |
| 48 |
impl_calendar_japanese_modern_v1!(Baked); |
| 49 |
impl_calendar_preferred_v1!(Baked); |
| 50 |
impl_calendar_week_v1!(Baked); |
| 51 |
}; |
| 52 |
|
| 53 |
icu_provider::data_marker!( |
| 54 |
/// Modern Japanese era names |
| 55 |
CalendarJapaneseModernV1, |
| 56 |
"calendar/japanese/modern/v1", |
| 57 |
JapaneseEras, |
| 58 |
is_singleton = true |
| 59 |
); |
| 60 |
|
| 61 |
icu_provider::data_marker!( |
| 62 |
/// Week information |
| 63 |
CalendarWeekV1, |
| 64 |
"calendar/week/v1", |
| 65 |
WeekData, |
| 66 |
fallback_config = { |
| 67 |
let mut config = LocaleFallbackConfig::default(); |
| 68 |
config.priority = LocaleFallbackPriority::Region; |
| 69 |
config |
| 70 |
}, |
| 71 |
); |
| 72 |
|
| 73 |
/// Default calendar preferences for a region. |
| 74 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, yoke::Yokeable, zerofrom::ZeroFrom)] |
| 75 |
#[cfg_attr(feature = "datagen", derive(databake::Bake))] |
| 76 |
#[cfg_attr(feature = "datagen", databake(path = icu_calendar::provider))] |
| 77 |
pub struct CalendarPreference { |
| 78 |
/// The default calendar to use for this region. |
| 79 |
pub default_algorithm: CalendarAlgorithm, |
| 80 |
/// The default Hijri calendar to use for this region. |
| 81 |
pub default_hijri_algorithm: HijriCalendarAlgorithm, |
| 82 |
} |
| 83 |
|
| 84 |
#[cfg(any(feature = "serde", feature = "datagen"))] |
| 85 |
#[derive(serde::Serialize, serde::Deserialize)] |
| 86 |
struct CalendarPreferenceHuman<'a> { |
| 87 |
#[serde(borrow)] |
| 88 |
default_algorithm: &'a str, |
| 89 |
#[serde(borrow)] |
| 90 |
default_hijri_algorithm: &'a str, |
| 91 |
} |
| 92 |
|
| 93 |
#[cfg(feature = "datagen")] |
| 94 |
impl serde::Serialize for CalendarPreference { |
| 95 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 96 |
where |
| 97 |
S: serde::Serializer, |
| 98 |
{ |
| 99 |
if serializer.is_human_readable() { |
| 100 |
let human = CalendarPreferenceHuman { |
| 101 |
default_algorithm: self.default_algorithm.as_str(), |
| 102 |
default_hijri_algorithm: CalendarAlgorithm::Hijri(Some( |
| 103 |
self.default_hijri_algorithm, |
| 104 |
)) |
| 105 |
.as_str(), |
| 106 |
}; |
| 107 |
return human.serialize(serializer); |
| 108 |
} |
| 109 |
|
| 110 |
let default = match self.default_algorithm { |
| 111 |
CalendarAlgorithm::Buddhist => 1u8, |
| 112 |
CalendarAlgorithm::Chinese => 2, |
| 113 |
CalendarAlgorithm::Coptic => 3, |
| 114 |
CalendarAlgorithm::Dangi => 4, |
| 115 |
CalendarAlgorithm::Ethioaa => 5, |
| 116 |
CalendarAlgorithm::Ethiopic => 6, |
| 117 |
CalendarAlgorithm::Gregory => 7, |
| 118 |
CalendarAlgorithm::Hebrew => 8, |
| 119 |
CalendarAlgorithm::Indian => 9, |
| 120 |
CalendarAlgorithm::Hijri(Some(s)) if s == self.default_hijri_algorithm => 10, |
| 121 |
CalendarAlgorithm::Iso8601 => 11, |
| 122 |
CalendarAlgorithm::Japanese => 12, |
| 123 |
CalendarAlgorithm::Persian => 13, |
| 124 |
CalendarAlgorithm::Roc => 14, |
| 125 |
_ => { |
| 126 |
debug_assert!( |
| 127 |
false, |
| 128 |
"unknown calendar algorithm: {:?}", |
| 129 |
self.default_algorithm |
| 130 |
); |
| 131 |
0 |
| 132 |
} |
| 133 |
}; |
| 134 |
let hijri = match self.default_hijri_algorithm { |
| 135 |
HijriCalendarAlgorithm::Umalqura => 1, |
| 136 |
HijriCalendarAlgorithm::Tbla => 2, |
| 137 |
HijriCalendarAlgorithm::Civil => 3, |
| 138 |
HijriCalendarAlgorithm::Rgsa => 4, |
| 139 |
_ => { |
| 140 |
debug_assert!( |
| 141 |
false, |
| 142 |
"unknown hijri algorithm: {:?}", |
| 143 |
self.default_hijri_algorithm |
| 144 |
); |
| 145 |
0 |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
(default << 3 | hijri).serialize(serializer) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
#[cfg(feature = "serde")] |
| 154 |
impl<'de> serde::Deserialize<'de> for CalendarPreference { |
| 155 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 156 |
where |
| 157 |
D: serde::Deserializer<'de>, |
| 158 |
{ |
| 159 |
if deserializer.is_human_readable() { |
| 160 |
let human = CalendarPreferenceHuman::deserialize(deserializer)?; |
| 161 |
let default_val = |
| 162 |
icu_locale_core::extensions::unicode::Value::try_from_str(human.default_algorithm) |
| 163 |
.map_err(serde::de::Error::custom)?; |
| 164 |
let default_algorithm = |
| 165 |
CalendarAlgorithm::try_from(&default_val).map_err(serde::de::Error::custom)?; |
| 166 |
let hijri_val = icu_locale_core::extensions::unicode::Value::try_from_str( |
| 167 |
human.default_hijri_algorithm, |
| 168 |
) |
| 169 |
.map_err(serde::de::Error::custom)?; |
| 170 |
let default_hijri_algorithm = match CalendarAlgorithm::try_from(&hijri_val) { |
| 171 |
Ok(CalendarAlgorithm::Hijri(Some(h))) => h, |
| 172 |
_ => return Err(serde::de::Error::custom("expected islamic-* calendar")), |
| 173 |
}; |
| 174 |
return Ok(Self { |
| 175 |
default_algorithm, |
| 176 |
default_hijri_algorithm, |
| 177 |
}); |
| 178 |
} |
| 179 |
|
| 180 |
let packed = u8::deserialize(deserializer)?; |
| 181 |
let default_algorithm = packed >> 3; |
| 182 |
let default_hijri_algorithm = packed & 0x07; |
| 183 |
let default_hijri_algorithm = match default_hijri_algorithm { |
| 184 |
1 => HijriCalendarAlgorithm::Umalqura, |
| 185 |
2 => HijriCalendarAlgorithm::Tbla, |
| 186 |
3 => HijriCalendarAlgorithm::Civil, |
| 187 |
4 => HijriCalendarAlgorithm::Rgsa, |
| 188 |
_ => { |
| 189 |
debug_assert!( |
| 190 |
false, |
| 191 |
"unknown hijri algorithm discriminant: {default_hijri_algorithm}" |
| 192 |
); |
| 193 |
HijriCalendarAlgorithm::Civil |
| 194 |
} |
| 195 |
}; |
| 196 |
let default_algorithm = match default_algorithm { |
| 197 |
1 => CalendarAlgorithm::Buddhist, |
| 198 |
2 => CalendarAlgorithm::Chinese, |
| 199 |
3 => CalendarAlgorithm::Coptic, |
| 200 |
4 => CalendarAlgorithm::Dangi, |
| 201 |
5 => CalendarAlgorithm::Ethioaa, |
| 202 |
6 => CalendarAlgorithm::Ethiopic, |
| 203 |
7 => CalendarAlgorithm::Gregory, |
| 204 |
8 => CalendarAlgorithm::Hebrew, |
| 205 |
9 => CalendarAlgorithm::Indian, |
| 206 |
10 => CalendarAlgorithm::Hijri(Some(default_hijri_algorithm)), |
| 207 |
11 => CalendarAlgorithm::Iso8601, |
| 208 |
12 => CalendarAlgorithm::Japanese, |
| 209 |
13 => CalendarAlgorithm::Persian, |
| 210 |
14 => CalendarAlgorithm::Roc, |
| 211 |
_ => { |
| 212 |
debug_assert!( |
| 213 |
false, |
| 214 |
"unknown calendar algorithm discriminant: {default_algorithm}" |
| 215 |
); |
| 216 |
CalendarAlgorithm::Gregory |
| 217 |
} |
| 218 |
}; |
| 219 |
Ok(Self { |
| 220 |
default_algorithm, |
| 221 |
default_hijri_algorithm, |
| 222 |
}) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
impl CalendarPreference { |
| 227 |
/// Resolves a [`Option<CalendarAlgorithm>`] to a concrete algorithm against these preferences. |
| 228 |
pub fn resolve(&self, unresolved: Option<CalendarAlgorithm>) -> CalendarAlgorithm { |
| 229 |
match unresolved { |
| 230 |
Some(CalendarAlgorithm::Hijri(None)) => { |
| 231 |
CalendarAlgorithm::Hijri(Some(self.default_hijri_algorithm)) |
| 232 |
} |
| 233 |
Some(a) => a, |
| 234 |
None => self.default_algorithm, |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
icu_provider::data_marker!( |
| 240 |
/// The preferred calendar algorithm for a region. |
| 241 |
CalendarPreferredV1, |
| 242 |
"calendar/preferred/v1", |
| 243 |
CalendarPreference, |
| 244 |
fallback_config = { |
| 245 |
let mut config = LocaleFallbackConfig::default(); |
| 246 |
config.priority = LocaleFallbackPriority::Region; |
| 247 |
config |
| 248 |
}, |
| 249 |
); |
| 250 |
|
| 251 |
icu_provider::data_struct!( |
| 252 |
CalendarPreference, |
| 253 |
#[cfg(feature = "datagen")] |
| 254 |
); |
| 255 |
|
| 256 |
#[cfg(feature = "datagen")] |
| 257 |
/// The latest minimum set of markers required by this component. |
| 258 |
pub const MARKERS: &[DataMarkerInfo] = &[ |
| 259 |
CalendarJapaneseModernV1::INFO, |
| 260 |
CalendarWeekV1::INFO, |
| 261 |
CalendarPreferredV1::INFO, |
| 262 |
]; |
| 263 |
|
| 264 |
/// The date at which an era started |
| 265 |
/// |
| 266 |
/// The order of fields in this struct is important! |
| 267 |
/// |
| 268 |
/// <div class="stab unstable"> |
| 269 |
/// π§ This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 270 |
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 271 |
/// to be stable, their Rust representation might not be. Use with caution. |
| 272 |
/// </div> |
| 273 |
#[zerovec::make_ule(EraStartDateULE)] |
| 274 |
#[derive( |
| 275 |
Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug, yoke::Yokeable, zerofrom::ZeroFrom, |
| 276 |
)] |
| 277 |
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))] |
| 278 |
#[cfg_attr(feature = "datagen", databake(path = icu_calendar::provider))] |
| 279 |
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 280 |
#[cfg_attr(not(feature = "alloc"), zerovec::skip_derive(ZeroMapKV))] |
| 281 |
pub struct EraStartDate { |
| 282 |
/// The Gregorian year the era started in |
| 283 |
pub year: i32, |
| 284 |
/// The Gregorian month the era started in |
| 285 |
pub month: u8, |
| 286 |
/// The Gregorian day the era started in |
| 287 |
pub day: u8, |
| 288 |
} |
| 289 |
|
| 290 |
/// A data structure containing the necessary era data for constructing a |
| 291 |
/// [`Japanese`](crate::cal::Japanese) calendar object |
| 292 |
/// |
| 293 |
/// <div class="stab unstable"> |
| 294 |
/// π§ This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 295 |
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 296 |
/// to be stable, their Rust representation might not be. Use with caution. |
| 297 |
/// </div> |
| 298 |
#[derive(Debug, PartialEq, Clone, Copy, Default, yoke::Yokeable, zerofrom::ZeroFrom)] |
| 299 |
pub struct JapaneseEras(Option<PackedEra>); |
| 300 |
|
| 301 |
icu_provider::data_struct!( |
| 302 |
JapaneseEras, |
| 303 |
#[cfg(feature = "datagen")] |
| 304 |
); |
| 305 |
|
| 306 |
impl JapaneseEras { |
| 307 |
/// Creates a new [`JapaneseEras`] without a post-Reiwa era. |
| 308 |
pub const fn up_to_reiwa() -> Self { |
| 309 |
Self(None) |
| 310 |
} |
| 311 |
|
| 312 |
/// Creates a new [`JapaneseEras`] with the last post-Reiwa era's start date, code, and index. |
| 313 |
pub const fn with_last_era( |
| 314 |
start: EraStartDate, |
| 315 |
code: TinyAsciiStr<8>, |
| 316 |
index: u8, |
| 317 |
) -> Option<Self> { |
| 318 |
if start.year < 2026 { |
| 319 |
return Some(Self::up_to_reiwa()); |
| 320 |
} |
| 321 |
let Some(packed) = PackedEra::pack(start, code, index) else { |
| 322 |
return None; |
| 323 |
}; |
| 324 |
Some(Self(Some(packed))) |
| 325 |
} |
| 326 |
|
| 327 |
/// Returns all post-Meji eras, in chronological order. |
| 328 |
pub fn eras(self) -> impl DoubleEndedIterator<Item = (EraStartDate, TinyAsciiStr<8>, u8)> { |
| 329 |
use tinystr::tinystr; |
| 330 |
[ |
| 331 |
( |
| 332 |
EraStartDate { |
| 333 |
year: 1868, |
| 334 |
month: 10, |
| 335 |
day: 23, |
| 336 |
}, |
| 337 |
tinystr!(8, "meiji"), |
| 338 |
2, |
| 339 |
), |
| 340 |
( |
| 341 |
EraStartDate { |
| 342 |
year: 1912, |
| 343 |
month: 7, |
| 344 |
day: 30, |
| 345 |
}, |
| 346 |
tinystr!(8, "taisho"), |
| 347 |
3, |
| 348 |
), |
| 349 |
( |
| 350 |
EraStartDate { |
| 351 |
year: 1926, |
| 352 |
month: 12, |
| 353 |
day: 25, |
| 354 |
}, |
| 355 |
tinystr!(8, "showa"), |
| 356 |
4, |
| 357 |
), |
| 358 |
( |
| 359 |
EraStartDate { |
| 360 |
year: 1989, |
| 361 |
month: 1, |
| 362 |
day: 8, |
| 363 |
}, |
| 364 |
tinystr!(8, "heisei"), |
| 365 |
5, |
| 366 |
), |
| 367 |
( |
| 368 |
EraStartDate { |
| 369 |
year: 2019, |
| 370 |
month: 5, |
| 371 |
day: 1, |
| 372 |
}, |
| 373 |
tinystr!(8, "reiwa"), |
| 374 |
6, |
| 375 |
), |
| 376 |
] |
| 377 |
.into_iter() |
| 378 |
.chain(self.0.map(PackedEra::unpack)) |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
#[cfg(feature = "datagen")] |
| 383 |
impl databake::Bake for JapaneseEras { |
| 384 |
fn bake(&self, ctx: &databake::CrateEnv) -> databake::TokenStream { |
| 385 |
ctx.insert("icu_calendar"); |
| 386 |
if let Some((era_start_date, code, idx)) = self.eras().last() |
| 387 |
&& code != "reiwa" |
| 388 |
{ |
| 389 |
let era_start_date = era_start_date.bake(ctx); |
| 390 |
let code = code.bake(ctx); |
| 391 |
let idx = idx.bake(ctx); |
| 392 |
databake::quote! { |
| 393 |
icu_calendar::provider::JapaneseEras::with_last_era(#era_start_date, #code, #idx).unwrap() |
| 394 |
} |
| 395 |
} else { |
| 396 |
databake::quote! { |
| 397 |
icu_calendar::provider::JapaneseEras::up_to_reiwa() |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
#[cfg(feature = "datagen")] |
| 404 |
impl databake::BakeSize for JapaneseEras { |
| 405 |
fn borrows_size(&self) -> usize { |
| 406 |
0 |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
#[cfg(feature = "datagen")] |
| 411 |
impl serde::Serialize for JapaneseEras { |
| 412 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 413 |
where |
| 414 |
S: serde::Serializer, |
| 415 |
{ |
| 416 |
use zerovec::ZeroVec; |
| 417 |
|
| 418 |
#[derive(serde::Serialize)] |
| 419 |
struct Raw<'data> { |
| 420 |
pub dates_to_eras: ZeroVec<'data, (EraStartDate, TinyAsciiStr<16>)>, |
| 421 |
} |
| 422 |
|
| 423 |
let mut dates_to_eras = self |
| 424 |
.eras() |
| 425 |
.map(|(start, code, _)| (start, code.resize())) |
| 426 |
.collect::<ZeroVec<_>>(); |
| 427 |
|
| 428 |
while let Some((start, code, index)) = self.eras().last() |
| 429 |
&& dates_to_eras.len() + 2 <= index as usize |
| 430 |
{ |
| 431 |
use zerovec::ule::AsULE; |
| 432 |
|
| 433 |
dates_to_eras.with_mut(|v| v.push((start, code.resize()).to_unaligned())); |
| 434 |
} |
| 435 |
|
| 436 |
Raw { dates_to_eras }.serialize(serializer) |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
#[cfg(feature = "serde")] |
| 441 |
impl<'de> serde::Deserialize<'de> for JapaneseEras { |
| 442 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 443 |
where |
| 444 |
D: serde::Deserializer<'de>, |
| 445 |
{ |
| 446 |
use serde::de::Error; |
| 447 |
use zerovec::ZeroVec; |
| 448 |
|
| 449 |
#[derive(serde::Deserialize)] |
| 450 |
struct Raw<'data> { |
| 451 |
#[serde(borrow)] |
| 452 |
pub dates_to_eras: ZeroVec<'data, (EraStartDate, TinyAsciiStr<16>)>, |
| 453 |
} |
| 454 |
|
| 455 |
let Raw { dates_to_eras } = Raw::deserialize(deserializer)?; |
| 456 |
|
| 457 |
let Some((start, code)) = dates_to_eras.last() else { |
| 458 |
return Err(D::Error::custom("At least one era is required")); |
| 459 |
}; |
| 460 |
|
| 461 |
if Self::up_to_reiwa() |
| 462 |
.eras() |
| 463 |
.map(|(start, code, ..)| (start, code.resize())) |
| 464 |
.ne(dates_to_eras |
| 465 |
.iter() |
| 466 |
.take(Self::up_to_reiwa().eras().count())) |
| 467 |
{ |
| 468 |
return Err(D::Error::custom( |
| 469 |
"Invalid era data: Meiji through Reiwa must be present and in order", |
| 470 |
)); |
| 471 |
} |
| 472 |
|
| 473 |
Self::with_last_era( |
| 474 |
start, |
| 475 |
code.resize(), |
| 476 |
dates_to_eras.as_slice().len() as u8 + 1, |
| 477 |
) |
| 478 |
.ok_or_else(|| D::Error::custom("Invalid era")) |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
/// A type to represent a modern (post 2026, 8-byte code) era. |
| 483 |
/// |
| 484 |
/// <div class="stab unstable"> |
| 485 |
/// π§ This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 486 |
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 487 |
/// to be stable, their Rust representation might not be. Use with caution. |
| 488 |
/// </div> |
| 489 |
#[derive(PartialEq, Copy, Clone, yoke::Yokeable, zerofrom::ZeroFrom)] |
| 490 |
struct PackedEra(NonZeroU8 /* give a niche */, [u8; 6]); |
| 491 |
|
| 492 |
impl Debug for PackedEra { |
| 493 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 494 |
let (start, code, index) = self.unpack(); |
| 495 |
f.debug_struct("PackedEra") |
| 496 |
.field("start", &start) |
| 497 |
.field("code", &code) |
| 498 |
.field("index", &index) |
| 499 |
.field("<packed>", &(self.0, self.1)) |
| 500 |
.finish() |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
impl PackedEra { |
| 505 |
/// Construct a `PackedEra` from a tuple |
| 506 |
/// This supports start dates until 2086-12-31, indices 7 and 8, and lower ASCII alphabetic codes |
| 507 |
const fn pack(start: EraStartDate, code: TinyAsciiStr<8>, index: u8) -> Option<Self> { |
| 508 |
// 5 bits |
| 509 |
if !(0 < start.day && start.day < 1 << 5) { |
| 510 |
return None; |
| 511 |
} |
| 512 |
// 4 bits |
| 513 |
if !(0 < start.month && start.month < 1 << 4) { |
| 514 |
return None; |
| 515 |
} |
| 516 |
// 6 bits |
| 517 |
if !(2026 <= start.year && start.year < 2026 + (1 << 6)) { |
| 518 |
return None; |
| 519 |
} |
| 520 |
// 1 bit |
| 521 |
if !(7 <= index && index < 7 + (1 << 1)) { |
| 522 |
return None; |
| 523 |
} |
| 524 |
// 40 bits |
| 525 |
if !code.is_ascii_alphabetic_lowercase() { |
| 526 |
return None; |
| 527 |
} |
| 528 |
|
| 529 |
let mut packed = 0; |
| 530 |
|
| 531 |
let &[c1, c2, c3, c4, c5, c6, c7, c8] = code.all_bytes(); |
| 532 |
packed = (packed << 5) | c1.saturating_sub(b'a' - 1) as u64; |
| 533 |
packed = (packed << 5) | c2.saturating_sub(b'a' - 1) as u64; |
| 534 |
packed = (packed << 5) | c3.saturating_sub(b'a' - 1) as u64; |
| 535 |
packed = (packed << 5) | c4.saturating_sub(b'a' - 1) as u64; |
| 536 |
packed = (packed << 5) | c5.saturating_sub(b'a' - 1) as u64; |
| 537 |
packed = (packed << 5) | c6.saturating_sub(b'a' - 1) as u64; |
| 538 |
packed = (packed << 5) | c7.saturating_sub(b'a' - 1) as u64; |
| 539 |
packed = (packed << 5) | c8.saturating_sub(b'a' - 1) as u64; |
| 540 |
|
| 541 |
packed = (packed << 1) | (index as u64 - 7); |
| 542 |
packed = (packed << 6) | (start.year as u64 - 2026); |
| 543 |
packed = (packed << 4) | start.month as u64; |
| 544 |
packed = (packed << 5) | start.day as u64; |
| 545 |
|
| 546 |
let [a, b, c, d, e, f, g, z] = packed.to_le_bytes(); |
| 547 |
debug_assert!(z == 0); |
| 548 |
let Some(a) = NonZeroU8::new(a) else { |
| 549 |
return None; |
| 550 |
}; |
| 551 |
Some(Self(a, [b, c, d, e, f, g])) |
| 552 |
} |
| 553 |
|
| 554 |
/// Convert a `PackedEra` into a tuple of start date, code, and |
| 555 |
/// [`era_index`](crate::types::EraYear::era_index) |
| 556 |
const fn unpack(self) -> (EraStartDate, TinyAsciiStr<8>, u8) { |
| 557 |
let Self(a, [b, c, d, e, f, g]) = self; |
| 558 |
let mut packed = u64::from_le_bytes([a.get(), b, c, d, e, f, g, 0]); |
| 559 |
|
| 560 |
let (day, month, year, index); |
| 561 |
(packed, day) = ((packed >> 5), (packed & 0b1_1111) as u8); |
| 562 |
(packed, month) = ((packed >> 4), (packed & 0b1111) as u8); |
| 563 |
(packed, year) = ((packed >> 6), (packed & 0b11_1111) as i32 + 2026); |
| 564 |
(packed, index) = ((packed >> 1), (packed & 0b1) as u8 + 7); |
| 565 |
|
| 566 |
let (c1, c2, c3, c4, c5, c6, c7, c8); |
| 567 |
(packed, c8) = ((packed >> 5), (packed & 0b1_1111) as u8 + (b'a' - 1)); |
| 568 |
(packed, c7) = ((packed >> 5), (packed & 0b1_1111) as u8 + (b'a' - 1)); |
| 569 |
(packed, c6) = ((packed >> 5), (packed & 0b1_1111) as u8 + (b'a' - 1)); |
| 570 |
(packed, c5) = ((packed >> 5), (packed & 0b1_1111) as u8 + (b'a' - 1)); |
| 571 |
(packed, c4) = ((packed >> 5), (packed & 0b1_1111) as u8 + (b'a' - 1)); |
| 572 |
(packed, c3) = ((packed >> 5), (packed & 0b1_1111) as u8 + (b'a' - 1)); |
| 573 |
(packed, c2) = ((packed >> 5), (packed & 0b1_1111) as u8 + (b'a' - 1)); |
| 574 |
(packed, c1) = ((packed >> 5), (packed & 0b1_1111) as u8 + (b'a' - 1)); |
| 575 |
|
| 576 |
debug_assert!(packed == 0); |
| 577 |
|
| 578 |
( |
| 579 |
EraStartDate { year, month, day }, |
| 580 |
unsafe { |
| 581 |
TinyAsciiStr::from_utf8_unchecked([ |
| 582 |
if c1 < b'a' { 0 } else { c1 }, |
| 583 |
if c2 < b'a' { 0 } else { c2 }, |
| 584 |
if c3 < b'a' { 0 } else { c3 }, |
| 585 |
if c4 < b'a' { 0 } else { c4 }, |
| 586 |
if c5 < b'a' { 0 } else { c5 }, |
| 587 |
if c6 < b'a' { 0 } else { c6 }, |
| 588 |
if c7 < b'a' { 0 } else { c7 }, |
| 589 |
if c8 < b'a' { 0 } else { c8 }, |
| 590 |
]) |
| 591 |
}, |
| 592 |
index, |
| 593 |
) |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
#[test] |
| 598 |
fn japanese_eras_size() { |
| 599 |
assert_eq!(size_of::<PackedEra>(), 7); |
| 600 |
assert_eq!(size_of::<Option<PackedEra>>(), 7); |
| 601 |
assert_eq!(size_of::<crate::cal::Japanese>(), 7); |
| 602 |
assert_eq!(size_of::<crate::cal::AnyCalendar>(), 8); |
| 603 |
} |
| 604 |
|
| 605 |
#[test] |
| 606 |
fn japanese_eras_packing() { |
| 607 |
let start = EraStartDate { |
| 608 |
year: 2086, |
| 609 |
month: 11, |
| 610 |
day: 1, |
| 611 |
}; |
| 612 |
let code = tinystr::tinystr!(8, "fuzu"); |
| 613 |
let index = 8; |
| 614 |
|
| 615 |
assert_eq!( |
| 616 |
JapaneseEras::with_last_era(start, code, index) |
| 617 |
.unwrap() |
| 618 |
.eras() |
| 619 |
.last() |
| 620 |
.unwrap(), |
| 621 |
(start, code, index) |
| 622 |
); |
| 623 |
assert_ne!( |
| 624 |
JapaneseEras::up_to_reiwa().eras().last().unwrap(), |
| 625 |
(start, code, index) |
| 626 |
); |
| 627 |
} |
| 628 |
|
| 629 |
#[test] |
| 630 |
fn japanese_eras_serde_roundtrip() { |
| 631 |
let eras = JapaneseEras::with_last_era( |
| 632 |
EraStartDate { |
| 633 |
year: 2086, |
| 634 |
month: 11, |
| 635 |
day: 1, |
| 636 |
}, |
| 637 |
tinystr::tinystr!(8, "fuzu"), |
| 638 |
8, |
| 639 |
) |
| 640 |
.unwrap(); |
| 641 |
|
| 642 |
assert_eq!( |
| 643 |
serde_json::from_str::<JapaneseEras>(&serde_json::to_string(&eras).unwrap()).unwrap(), |
| 644 |
eras |
| 645 |
); |
| 646 |
|
| 647 |
let eras = JapaneseEras::up_to_reiwa(); |
| 648 |
|
| 649 |
assert_eq!( |
| 650 |
serde_json::from_str::<JapaneseEras>(&serde_json::to_string(&eras).unwrap()).unwrap(), |
| 651 |
eras |
| 652 |
); |
| 653 |
} |
| 654 |
|
| 655 |
#[test] |
| 656 |
fn japanese_eras_bake() { |
| 657 |
databake::test_bake!( |
| 658 |
JapaneseEras, |
| 659 |
const, |
| 660 |
crate::provider::JapaneseEras::with_last_era( |
| 661 |
crate::provider::EraStartDate { |
| 662 |
year: 2086i32, |
| 663 |
month: 11u8, |
| 664 |
day: 1u8, |
| 665 |
}, |
| 666 |
tinystr::tinystr!(8usize, "fuzu"), |
| 667 |
8u8 |
| 668 |
) |
| 669 |
.unwrap(), |
| 670 |
icu_calendar |
| 671 |
); |
| 672 |
databake::test_bake!( |
| 673 |
JapaneseEras, |
| 674 |
const, |
| 675 |
crate::provider::JapaneseEras::up_to_reiwa(), |
| 676 |
icu_calendar |
| 677 |
); |
| 678 |
} |
| 679 |
|
| 680 |
/// An ICU4X mapping to a subset of CLDR weekData. |
| 681 |
/// See CLDR-JSON's weekData.json for more context. |
| 682 |
/// |
| 683 |
/// <div class="stab unstable"> |
| 684 |
/// π§ This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 685 |
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 686 |
/// to be stable, their Rust representation might not be. Use with caution. |
| 687 |
/// </div> |
| 688 |
#[derive(Clone, Copy, Debug, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom)] |
| 689 |
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))] |
| 690 |
#[cfg_attr(feature = "datagen", databake(path = icu_calendar::provider))] |
| 691 |
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 692 |
#[allow(clippy::exhaustive_structs)] // used in data provider |
| 693 |
pub struct WeekData { |
| 694 |
/// The first day of a week. |
| 695 |
pub first_weekday: Weekday, |
| 696 |
/// Bitset representing weekdays that are part of the 'weekend', for calendar purposes. |
| 697 |
/// The number of days can be different between locales, and may not be contiguous. |
| 698 |
pub weekend: WeekdaySet, |
| 699 |
} |
| 700 |
|
| 701 |
icu_provider::data_struct!( |
| 702 |
WeekData, |
| 703 |
#[cfg(feature = "datagen")] |
| 704 |
); |
| 705 |
|
| 706 |
/// Bitset representing weekdays. |
| 707 |
// |
| 708 |
// This Bitset uses an [u8] to represent the weekend, thus leaving one bit free. |
| 709 |
// Each bit represents a day in the following order: |
| 710 |
// |
| 711 |
// ββ·Mon |
| 712 |
// βββ·Tue |
| 713 |
// ββββ·Wed |
| 714 |
// βββββ·Thu |
| 715 |
// ββββ ββ·Fri |
| 716 |
// ββββ βββ·Sat |
| 717 |
// ββββ ββββ·Sun |
| 718 |
// ββββ βββ |
| 719 |
// 0b0000_1010 |
| 720 |
// |
| 721 |
// Please note that this is not a range, this are the discrete days representing a weekend. Other examples: |
| 722 |
// 0b0101_1000 -> Tue, Thu, Fri |
| 723 |
// 0b0000_0110 -> Sat, Sun |
| 724 |
#[derive(Clone, Copy, Debug, PartialEq)] |
| 725 |
pub struct WeekdaySet(u8); |
| 726 |
|
| 727 |
impl WeekdaySet { |
| 728 |
/// Returns whether the set contains the day. |
| 729 |
pub const fn contains(&self, day: Weekday) -> bool { |
| 730 |
self.0 & day.bit_value() != 0 |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
impl WeekdaySet { |
| 735 |
/// Creates a new [`WeekdaySet`] using the provided days. |
| 736 |
pub const fn new(days: &[Weekday]) -> Self { |
| 737 |
let mut i = 0; |
| 738 |
let mut w = 0; |
| 739 |
#[expect(clippy::indexing_slicing)] |
| 740 |
while i < days.len() { |
| 741 |
w |= days[i].bit_value(); |
| 742 |
i += 1; |
| 743 |
} |
| 744 |
Self(w) |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
impl Weekday { |
| 749 |
/// Defines the bit order used for encoding and reading weekend days. |
| 750 |
const fn bit_value(self) -> u8 { |
| 751 |
match self { |
| 752 |
Weekday::Monday => 1 << 6, |
| 753 |
Weekday::Tuesday => 1 << 5, |
| 754 |
Weekday::Wednesday => 1 << 4, |
| 755 |
Weekday::Thursday => 1 << 3, |
| 756 |
Weekday::Friday => 1 << 2, |
| 757 |
Weekday::Saturday => 1 << 1, |
| 758 |
Weekday::Sunday => 1 << 0, |
| 759 |
} |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
#[cfg(feature = "datagen")] |
| 764 |
impl databake::Bake for WeekdaySet { |
| 765 |
fn bake(&self, ctx: &databake::CrateEnv) -> databake::TokenStream { |
| 766 |
ctx.insert("icu_calendar"); |
| 767 |
let days = |
| 768 |
crate::week::WeekdaySetIterator::new(Weekday::Monday, *self).map(|d| d.bake(ctx)); |
| 769 |
databake::quote! { |
| 770 |
icu_calendar::provider::WeekdaySet::new(&[#(#days),*]) |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
#[cfg(feature = "datagen")] |
| 776 |
impl databake::BakeSize for WeekdaySet { |
| 777 |
fn borrows_size(&self) -> usize { |
| 778 |
0 |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
#[cfg(feature = "datagen")] |
| 783 |
impl serde::Serialize for WeekdaySet { |
| 784 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 785 |
where |
| 786 |
S: serde::Serializer, |
| 787 |
{ |
| 788 |
if serializer.is_human_readable() { |
| 789 |
use serde::ser::SerializeSeq; |
| 790 |
|
| 791 |
let mut seq = serializer.serialize_seq(None)?; |
| 792 |
for day in crate::week::WeekdaySetIterator::new(Weekday::Monday, *self) { |
| 793 |
seq.serialize_element(&day)?; |
| 794 |
} |
| 795 |
seq.end() |
| 796 |
} else { |
| 797 |
self.0.serialize(serializer) |
| 798 |
} |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
#[cfg(feature = "serde")] |
| 803 |
impl<'de> serde::Deserialize<'de> for WeekdaySet { |
| 804 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 805 |
where |
| 806 |
D: serde::Deserializer<'de>, |
| 807 |
{ |
| 808 |
if deserializer.is_human_readable() { |
| 809 |
use core::marker::PhantomData; |
| 810 |
|
| 811 |
struct Visitor<'de>(PhantomData<&'de ()>); |
| 812 |
impl<'de> serde::de::Visitor<'de> for Visitor<'de> { |
| 813 |
type Value = WeekdaySet; |
| 814 |
fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 815 |
core::write!(f, "a sequence of Weekdays") |
| 816 |
} |
| 817 |
fn visit_seq<A: serde::de::SeqAccess<'de>>( |
| 818 |
self, |
| 819 |
mut seq: A, |
| 820 |
) -> Result<Self::Value, A::Error> { |
| 821 |
let mut set = WeekdaySet::new(&[]); |
| 822 |
while let Some(day) = seq.next_element::<Weekday>()? { |
| 823 |
set.0 |= day.bit_value(); |
| 824 |
} |
| 825 |
Ok(set) |
| 826 |
} |
| 827 |
} |
| 828 |
deserializer.deserialize_seq(Visitor(PhantomData)) |
| 829 |
} else { |
| 830 |
u8::deserialize(deserializer).map(Self) |
| 831 |
} |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
#[test] |
| 836 |
#[cfg(feature = "datagen")] |
| 837 |
fn test_weekdayset_bake() { |
| 838 |
databake::test_bake!( |
| 839 |
WeekdaySet, |
| 840 |
const, |
| 841 |
crate::provider::WeekdaySet::new(&[ |
| 842 |
crate::types::Weekday::Monday, |
| 843 |
crate::types::Weekday::Wednesday, |
| 844 |
crate::types::Weekday::Friday |
| 845 |
]), |
| 846 |
icu_calendar |
| 847 |
); |
| 848 |
} |
| 849 |
|
| 850 |
#[test] |
| 851 |
fn test_weekdayset_new() { |
| 852 |
use Weekday::*; |
| 853 |
|
| 854 |
let sat_sun_bitmap = Saturday.bit_value() | Sunday.bit_value(); |
| 855 |
let sat_sun_weekend = WeekdaySet::new(&[Saturday, Sunday]); |
| 856 |
assert_eq!(sat_sun_bitmap, sat_sun_weekend.0); |
| 857 |
|
| 858 |
let fri_sat_bitmap = Friday.bit_value() | Saturday.bit_value(); |
| 859 |
let fri_sat_weekend = WeekdaySet::new(&[Friday, Saturday]); |
| 860 |
assert_eq!(fri_sat_bitmap, fri_sat_weekend.0); |
| 861 |
|
| 862 |
let fri_sun_bitmap = Friday.bit_value() | Sunday.bit_value(); |
| 863 |
let fri_sun_weekend = WeekdaySet::new(&[Friday, Sunday]); |
| 864 |
assert_eq!(fri_sun_bitmap, fri_sun_weekend.0); |
| 865 |
|
| 866 |
let fri_bitmap = Friday.bit_value(); |
| 867 |
let fri_weekend = WeekdaySet::new(&[Friday, Friday]); |
| 868 |
assert_eq!(fri_bitmap, fri_weekend.0); |
| 869 |
|
| 870 |
let sun_mon_bitmap = Sunday.bit_value() | Monday.bit_value(); |
| 871 |
let sun_mon_weekend = WeekdaySet::new(&[Sunday, Monday]); |
| 872 |
assert_eq!(sun_mon_bitmap, sun_mon_weekend.0); |
| 873 |
|
| 874 |
let mon_sun_bitmap = Monday.bit_value() | Sunday.bit_value(); |
| 875 |
let mon_sun_weekend = WeekdaySet::new(&[Monday, Sunday]); |
| 876 |
assert_eq!(mon_sun_bitmap, mon_sun_weekend.0); |
| 877 |
|
| 878 |
let mon_bitmap = Monday.bit_value(); |
| 879 |
let mon_weekend = WeekdaySet::new(&[Monday]); |
| 880 |
assert_eq!(mon_bitmap, mon_weekend.0); |
| 881 |
} |