1use icu_calendar::week::WeekOf;
6
7#[diplomat::bridge]
8pub 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 #[diplomat::enum_convert(RelativeUnit)]
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 #[diplomat::opaque]
33 #[diplomat::rust_link(icu::calendar::week::WeekCalculator, Struct)]
34 pub struct ICU4XWeekCalculator(pub WeekCalculator);
35
36 impl ICU4XWeekCalculator {
37 #[diplomat::rust_link(icu::calendar::week::WeekCalculator::try_new, FnInStruct)]
39 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
40 pub fn create(
41 provider: &ICU4XDataProvider,
42 locale: &ICU4XLocale,
43 ) -> Result<Box<ICU4XWeekCalculator>, ICU4XError> {
44 let locale = locale.to_datalocale();
45
46 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 &locale,
52 )?)))
53 }
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 pub fn create_from_first_day_of_week_and_min_week_days(
67 first_weekday: ICU4XIsoWeekday,
68 min_week_days: u8,
69 ) -> Box<ICU4XWeekCalculator> {
70 let mut calculator = WeekCalculator::default();
71 calculator.first_weekday = first_weekday.into();
72 calculator.min_week_days = min_week_days;
73 Box::new(ICU4XWeekCalculator(calculator))
74 }
75
76 #[diplomat::rust_link(icu::calendar::week::WeekCalculator::first_weekday, StructField)]
78 #[diplomat::attr(supports = accessors, getter)]
79 pub fn first_weekday(&self) -> ICU4XIsoWeekday {
80 self.0.first_weekday.into()
81 }
82 #[diplomat::rust_link(icu::calendar::week::WeekCalculator::min_week_days, StructField)]
85 #[diplomat::attr(supports = accessors, getter)]
86 pub fn min_week_days(&self) -> u8 {
87 self.0.min_week_days
88 }
89
90 #[diplomat::rust_link(icu::calendar::week::WeekCalculator::weekend, FnInStruct)]
91 #[diplomat::attr(supports = accessors, getter)]
92 pub fn weekend(&self) -> ICU4XWeekendContainsDay {
93 let mut contains = ICU4XWeekendContainsDay::default();
94 for day in self.0.weekend() {
95 match day {
96 IsoWeekday::Monday => contains.monday = true,
97 IsoWeekday::Tuesday => contains.tuesday = true,
98 IsoWeekday::Wednesday => contains.wednesday = true,
99 IsoWeekday::Thursday => contains.thursday = true,
100 IsoWeekday::Friday => contains.friday = true,
101 IsoWeekday::Saturday => contains.saturday = true,
102 IsoWeekday::Sunday => contains.sunday = true,
103 }
104 }
105 contains
106 }
107 }
108
109 #[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
123impl From<WeekOf> for ffi::ICU4XWeekOf {
124 fn from(other: WeekOf) -> Self {
125 ffi::ICU4XWeekOf {
126 week: other.week,
127 unit: other.unit.into(),
128 }
129 }
130}