Debian Codegraph @

debian/rust-vendor/icu_calendar/src/preferences.rs

raw
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 //! Locale preferences used by this crate
6
7 #[doc(inline)]
8 /// **This is a reexport of a type in [`icu::locale`](icu_locale_core::preferences::extensions::unicode::keywords)**.
9 #[doc = "\n"] // prevent autoformatting
10 pub use icu_locale_core::preferences::extensions::unicode::keywords::CalendarAlgorithm;
11 /// **This is a reexport of a type in [`icu::locale`](icu_locale_core::preferences::extensions::unicode::keywords)**.
12 #[doc = "\n"] // prevent autoformatting
13 pub use icu_locale_core::preferences::extensions::unicode::keywords::FirstDay;
14 #[doc(inline)]
15 /// **This is a reexport of a type in [`icu::locale`](icu_locale_core::preferences::extensions::unicode::keywords)**.
16 #[doc = "\n"] // prevent autoformatting
17 pub use icu_locale_core::preferences::extensions::unicode::keywords::HijriCalendarAlgorithm;
18
19 use icu_locale_core::preferences::define_preferences;
20
21 define_preferences!(
22 /// The preferences for calendars formatting.
23 [Copy]
24 CalendarPreferences,
25 {
26 /// The user's preferred calendar system.
27 calendar_algorithm: CalendarAlgorithm
28 }
29 );
30
31 define_preferences!(
32 /// The preferences for the week information.
33 [Copy]
34 WeekPreferences,
35 {
36 /// The first day of the week
37 first_weekday: FirstDay
38 }
39 );
40
41 impl CalendarPreferences {
42 /// Selects the [`CalendarAlgorithm`] appropriate for the given [`CalendarPreferences`].
43 ///
44 /// This is guaranteed to return a [`CalendarAlgorithm`] that successfully converts into an
45 /// [`AnyCalendarKind`](crate::AnyCalendarKind) (i.e. not `-u-ca-islamic` or `-u-ca-islamic-rgsa`).
46 ///
47 /// # Example
48 ///
49 /// ```
50 /// use icu::calendar::preferences::{CalendarAlgorithm, CalendarPreferences, HijriCalendarAlgorithm};
51 /// use icu::locale::locale;
52 ///
53 /// assert_eq!(CalendarPreferences::from(&locale!("und")).resolved_algorithm(), CalendarAlgorithm::Gregory);
54 /// assert_eq!(CalendarPreferences::from(&locale!("und-US-u-ca-hebrew")).resolved_algorithm(), CalendarAlgorithm::Hebrew);
55 /// assert_eq!(CalendarPreferences::from(&locale!("und-AF")).resolved_algorithm(), CalendarAlgorithm::Persian);
56 /// assert_eq!(CalendarPreferences::from(&locale!("und-US-u-rg-thxxxx")).resolved_algorithm(), CalendarAlgorithm::Buddhist);
57 /// assert_eq!(
58 /// CalendarPreferences::from(&locale!("und-US-u-ca-islamic")).resolved_algorithm(),
59 /// CalendarAlgorithm::Hijri(Some(HijriCalendarAlgorithm::Civil))
60 /// );
61 /// # assert_eq!(CalendarPreferences::from(&"und-US-u-ca-islamic-rgsa".parse::<icu::locale::Locale>().unwrap()).resolved_algorithm(),
62 /// # CalendarAlgorithm::Hijri(Some(HijriCalendarAlgorithm::Civil))
63 /// # );
64 /// # assert_eq!(CalendarPreferences::from(&"und-US-u-ca-islamic-foo".parse::<icu::locale::Locale>().unwrap()).resolved_algorithm(),
65 /// # CalendarAlgorithm::Gregory
66 /// # );
67 /// # assert_eq!(CalendarPreferences::from(&"und-US-u-ca-hebrew-foo".parse::<icu::locale::Locale>().unwrap()).resolved_algorithm(),
68 /// # CalendarAlgorithm::Gregory
69 /// # );
70 /// ```
71 #[deprecated(since = "2.3.0")]
72 pub fn resolved_algorithm(self) -> CalendarAlgorithm {
73 use icu_locale_core::subtags::{Region, region};
74 const AE: Region = region!("AE");
75 const BH: Region = region!("BH");
76 const KW: Region = region!("KW");
77 const QA: Region = region!("QA");
78 const SA: Region = region!("SA");
79 const TH: Region = region!("TH");
80 const AF: Region = region!("AF");
81 const IR: Region = region!("IR");
82
83 // This is tested to be consistent with CLDR in icu_provider_source::calendar::test_calendar_resolution
84 match (
85 self.calendar_algorithm,
86 self.locale_preferences
87 .to_data_locale_region_priority()
88 .region,
89 ) {
90 (
91 Some(CalendarAlgorithm::Hijri(None | Some(HijriCalendarAlgorithm::Rgsa))),
92 Some(AE | BH | KW | QA | SA),
93 ) => CalendarAlgorithm::Hijri(Some(HijriCalendarAlgorithm::Umalqura)),
94 (Some(CalendarAlgorithm::Hijri(None | Some(HijriCalendarAlgorithm::Rgsa))), _) => {
95 CalendarAlgorithm::Hijri(Some(HijriCalendarAlgorithm::Civil))
96 }
97 (Some(a), _) => a,
98 (None, Some(TH)) => CalendarAlgorithm::Buddhist,
99 (None, Some(AF | IR)) => CalendarAlgorithm::Persian,
100 (None, _) => CalendarAlgorithm::Gregory,
101 }
102 }
103 }