1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use icu_calendar::week::WeekOf;

#[diplomat::bridge]
pub mod ffi {
    use crate::date::ffi::ICU4XIsoWeekday;
    use crate::errors::ffi::ICU4XError;
    use crate::locale::ffi::ICU4XLocale;
    use crate::provider::ffi::ICU4XDataProvider;
    use alloc::boxed::Box;
    use icu_calendar::types::IsoWeekday;
    use icu_calendar::week::{RelativeUnit, WeekCalculator};

    #[diplomat::rust_link(icu::calendar::week::RelativeUnit, Enum)]
    #[diplomat::enum_convert(RelativeUnit)]
    pub enum ICU4XWeekRelativeUnit {
        Previous,
        Current,
        Next,
    }

    #[diplomat::rust_link(icu::calendar::week::WeekOf, Struct)]
    #[diplomat::out]
    pub struct ICU4XWeekOf {
        pub week: u16,
        pub unit: ICU4XWeekRelativeUnit,
    }
    /// A Week calculator, useful to be passed in to `week_of_year()` on Date and DateTime types
    #[diplomat::opaque]
    #[diplomat::rust_link(icu::calendar::week::WeekCalculator, Struct)]
    pub struct ICU4XWeekCalculator(pub WeekCalculator);

    impl ICU4XWeekCalculator {
        /// Creates a new [`ICU4XWeekCalculator`] from locale data.
        #[diplomat::rust_link(icu::calendar::week::WeekCalculator::try_new, FnInStruct)]
        #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
        pub fn create(
            provider: &ICU4XDataProvider,
            locale: &ICU4XLocale,
        ) -> Result<Box<ICU4XWeekCalculator>, ICU4XError> {
            let locale = locale.to_datalocale();

            Ok(Box::new(ICU4XWeekCalculator(call_constructor!(
                WeekCalculator::try_new,
                WeekCalculator::try_new_with_any_provider,
                WeekCalculator::try_new_with_buffer_provider,
                provider,
                &locale,
            )?)))
        }

        #[diplomat::rust_link(
            icu::calendar::week::WeekCalculator::first_weekday,
            StructField,
            compact
        )]
        #[diplomat::rust_link(
            icu::calendar::week::WeekCalculator::min_week_days,
            StructField,
            compact
        )]
        #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "from_first_day_of_week_and_min_week_days")]
        pub fn create_from_first_day_of_week_and_min_week_days(
            first_weekday: ICU4XIsoWeekday,
            min_week_days: u8,
        ) -> Box<ICU4XWeekCalculator> {
            let mut calculator = WeekCalculator::default();
            calculator.first_weekday = first_weekday.into();
            calculator.min_week_days = min_week_days;
            Box::new(ICU4XWeekCalculator(calculator))
        }

        /// Returns the weekday that starts the week for this object's locale
        #[diplomat::rust_link(icu::calendar::week::WeekCalculator::first_weekday, StructField)]
        #[diplomat::attr(supports = accessors, getter)]
        pub fn first_weekday(&self) -> ICU4XIsoWeekday {
            self.0.first_weekday.into()
        }
        /// The minimum number of days overlapping a year required for a week to be
        /// considered part of that year
        #[diplomat::rust_link(icu::calendar::week::WeekCalculator::min_week_days, StructField)]
        #[diplomat::attr(supports = accessors, getter)]
        pub fn min_week_days(&self) -> u8 {
            self.0.min_week_days
        }

        #[diplomat::rust_link(icu::calendar::week::WeekCalculator::weekend, FnInStruct)]
        #[diplomat::attr(supports = accessors, getter)]
        pub fn weekend(&self) -> ICU4XWeekendContainsDay {
            let mut contains = ICU4XWeekendContainsDay::default();
            for day in self.0.weekend() {
                match day {
                    IsoWeekday::Monday => contains.monday = true,
                    IsoWeekday::Tuesday => contains.tuesday = true,
                    IsoWeekday::Wednesday => contains.wednesday = true,
                    IsoWeekday::Thursday => contains.thursday = true,
                    IsoWeekday::Friday => contains.friday = true,
                    IsoWeekday::Saturday => contains.saturday = true,
                    IsoWeekday::Sunday => contains.sunday = true,
                }
            }
            contains
        }
    }

    /// Documents which days of the week are considered to be a part of the weekend
    #[diplomat::rust_link(icu::calendar::week::WeekCalculator::weekend, FnInStruct)]
    #[derive(Default)]
    pub struct ICU4XWeekendContainsDay {
        pub monday: bool,
        pub tuesday: bool,
        pub wednesday: bool,
        pub thursday: bool,
        pub friday: bool,
        pub saturday: bool,
        pub sunday: bool,
    }
}

impl From<WeekOf> for ffi::ICU4XWeekOf {
    fn from(other: WeekOf) -> Self {
        ffi::ICU4XWeekOf {
            week: other.week,
            unit: other.unit.into(),
        }
    }
}