/rust/registry/src/index.crates.io-6f17d22bba15001f/icu_capi-1.5.1/src/week.rs
Line | Count | Source (jump to first uncovered line) |
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 | | use icu_calendar::week::WeekOf; |
6 | | |
7 | 0 | #[diplomat::bridge] Unexecuted instantiation: ICU4XWeekCalculator_create Unexecuted instantiation: ICU4XWeekCalculator_create_from_first_day_of_week_and_min_week_days Unexecuted instantiation: ICU4XWeekCalculator_first_weekday Unexecuted instantiation: ICU4XWeekCalculator_min_week_days Unexecuted instantiation: ICU4XWeekCalculator_weekend Unexecuted instantiation: ICU4XWeekCalculator_destroy Unexecuted instantiation: ICU4XWeekOf_destroy Unexecuted instantiation: ICU4XWeekRelativeUnit_destroy Unexecuted instantiation: ICU4XWeekendContainsDay_destroy |
8 | | pub mod ffi { |
9 | | use crate::date::ffi::ICU4XIsoWeekday; |
10 | | use crate::errors::ffi::ICU4XError; |
11 | | use crate::locale::ffi::ICU4XLocale; |
12 | | use crate::provider::ffi::ICU4XDataProvider; |
13 | | use alloc::boxed::Box; |
14 | | use icu_calendar::types::IsoWeekday; |
15 | | use icu_calendar::week::{RelativeUnit, WeekCalculator}; |
16 | | |
17 | | #[diplomat::rust_link(icu::calendar::week::RelativeUnit, Enum)] |
18 | 0 | #[diplomat::enum_convert(RelativeUnit)] Unexecuted instantiation: <icu_capi::week::ffi::ICU4XWeekRelativeUnit as core::convert::From<icu_calendar::week_of::RelativeUnit>>::from Unexecuted instantiation: <icu_calendar::week_of::RelativeUnit as core::convert::From<icu_capi::week::ffi::ICU4XWeekRelativeUnit>>::from |
19 | | pub enum ICU4XWeekRelativeUnit { |
20 | | Previous, |
21 | | Current, |
22 | | Next, |
23 | | } |
24 | | |
25 | | #[diplomat::rust_link(icu::calendar::week::WeekOf, Struct)] |
26 | | #[diplomat::out] |
27 | | pub struct ICU4XWeekOf { |
28 | | pub week: u16, |
29 | | pub unit: ICU4XWeekRelativeUnit, |
30 | | } |
31 | | /// A Week calculator, useful to be passed in to `week_of_year()` on Date and DateTime types |
32 | | #[diplomat::opaque] |
33 | | #[diplomat::rust_link(icu::calendar::week::WeekCalculator, Struct)] |
34 | | pub struct ICU4XWeekCalculator(pub WeekCalculator); |
35 | | |
36 | | impl ICU4XWeekCalculator { |
37 | | /// Creates a new [`ICU4XWeekCalculator`] from locale data. |
38 | | #[diplomat::rust_link(icu::calendar::week::WeekCalculator::try_new, FnInStruct)] |
39 | | #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
40 | 0 | pub fn create( |
41 | 0 | provider: &ICU4XDataProvider, |
42 | 0 | locale: &ICU4XLocale, |
43 | 0 | ) -> Result<Box<ICU4XWeekCalculator>, ICU4XError> { |
44 | 0 | let locale = locale.to_datalocale(); |
45 | 0 |
|
46 | 0 | Ok(Box::new(ICU4XWeekCalculator(call_constructor!( |
47 | | WeekCalculator::try_new, |
48 | | WeekCalculator::try_new_with_any_provider, |
49 | | WeekCalculator::try_new_with_buffer_provider, |
50 | | provider, |
51 | 0 | &locale, |
52 | 0 | )?))) |
53 | 0 | } |
54 | | |
55 | | #[diplomat::rust_link( |
56 | | icu::calendar::week::WeekCalculator::first_weekday, |
57 | | StructField, |
58 | | compact |
59 | | )] |
60 | | #[diplomat::rust_link( |
61 | | icu::calendar::week::WeekCalculator::min_week_days, |
62 | | StructField, |
63 | | compact |
64 | | )] |
65 | | #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "from_first_day_of_week_and_min_week_days")] |
66 | 0 | pub fn create_from_first_day_of_week_and_min_week_days( |
67 | 0 | first_weekday: ICU4XIsoWeekday, |
68 | 0 | min_week_days: u8, |
69 | 0 | ) -> Box<ICU4XWeekCalculator> { |
70 | 0 | let mut calculator = WeekCalculator::default(); |
71 | 0 | calculator.first_weekday = first_weekday.into(); |
72 | 0 | calculator.min_week_days = min_week_days; |
73 | 0 | Box::new(ICU4XWeekCalculator(calculator)) |
74 | 0 | } |
75 | | |
76 | | /// Returns the weekday that starts the week for this object's locale |
77 | | #[diplomat::rust_link(icu::calendar::week::WeekCalculator::first_weekday, StructField)] |
78 | | #[diplomat::attr(supports = accessors, getter)] |
79 | 0 | pub fn first_weekday(&self) -> ICU4XIsoWeekday { |
80 | 0 | self.0.first_weekday.into() |
81 | 0 | } |
82 | | /// The minimum number of days overlapping a year required for a week to be |
83 | | /// considered part of that year |
84 | | #[diplomat::rust_link(icu::calendar::week::WeekCalculator::min_week_days, StructField)] |
85 | | #[diplomat::attr(supports = accessors, getter)] |
86 | 0 | pub fn min_week_days(&self) -> u8 { |
87 | 0 | self.0.min_week_days |
88 | 0 | } |
89 | | |
90 | | #[diplomat::rust_link(icu::calendar::week::WeekCalculator::weekend, FnInStruct)] |
91 | | #[diplomat::attr(supports = accessors, getter)] |
92 | 0 | pub fn weekend(&self) -> ICU4XWeekendContainsDay { |
93 | 0 | let mut contains = ICU4XWeekendContainsDay::default(); |
94 | 0 | for day in self.0.weekend() { |
95 | 0 | match day { |
96 | 0 | IsoWeekday::Monday => contains.monday = true, |
97 | 0 | IsoWeekday::Tuesday => contains.tuesday = true, |
98 | 0 | IsoWeekday::Wednesday => contains.wednesday = true, |
99 | 0 | IsoWeekday::Thursday => contains.thursday = true, |
100 | 0 | IsoWeekday::Friday => contains.friday = true, |
101 | 0 | IsoWeekday::Saturday => contains.saturday = true, |
102 | 0 | IsoWeekday::Sunday => contains.sunday = true, |
103 | | } |
104 | | } |
105 | 0 | contains |
106 | 0 | } |
107 | | } |
108 | | |
109 | | /// Documents which days of the week are considered to be a part of the weekend |
110 | | #[diplomat::rust_link(icu::calendar::week::WeekCalculator::weekend, FnInStruct)] |
111 | | #[derive(Default)] |
112 | | pub struct ICU4XWeekendContainsDay { |
113 | | pub monday: bool, |
114 | | pub tuesday: bool, |
115 | | pub wednesday: bool, |
116 | | pub thursday: bool, |
117 | | pub friday: bool, |
118 | | pub saturday: bool, |
119 | | pub sunday: bool, |
120 | | } |
121 | | } |
122 | | |
123 | | impl From<WeekOf> for ffi::ICU4XWeekOf { |
124 | 0 | fn from(other: WeekOf) -> Self { |
125 | 0 | ffi::ICU4XWeekOf { |
126 | 0 | week: other.week, |
127 | 0 | unit: other.unit.into(), |
128 | 0 | } |
129 | 0 | } |
130 | | } |