Skip to main content

icu_capi/
week.rs

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#[diplomat::bridge]
6#[diplomat::abi_rename = "icu4x_{0}_mv1"]
7pub mod ffi {
8    use alloc::boxed::Box;
9
10    use crate::unstable::date::ffi::Weekday;
11    #[cfg(feature = "buffer_provider")]
12    use crate::unstable::provider::ffi::DataProvider;
13    #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
14    use crate::unstable::{errors::ffi::DataError, locale_core::ffi::Locale};
15
16    /// A Week calculator, useful to be passed in to `week_of_year()` on Date and DateTime types
17    #[diplomat::opaque]
18    #[diplomat::rust_link(icu::calendar::week::WeekInformation, Struct)]
19    pub struct WeekInformation(pub icu_calendar::week::WeekInformation);
20
21    impl WeekInformation {
22        /// Creates a new [`WeekInformation`] from locale data using compiled data.
23        #[diplomat::rust_link(icu::calendar::week::WeekInformation::try_new, FnInStruct)]
24        #[diplomat::attr(supports = fallible_constructors, constructor)]
25        #[cfg(feature = "compiled_data")]
26        pub fn create(locale: &Locale) -> Result<Box<WeekInformation>, DataError> {
27            let prefs = (&locale.0).into();
28
29            Ok(Box::new(WeekInformation(
30                icu_calendar::week::WeekInformation::try_new(prefs)?,
31            )))
32        }
33        /// Creates a new [`WeekInformation`] from locale data using a particular data source.
34        #[diplomat::rust_link(icu::calendar::week::WeekInformation::try_new, FnInStruct)]
35        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")]
36        #[cfg(feature = "buffer_provider")]
37        pub fn create_with_provider(
38            provider: &DataProvider,
39            locale: &Locale,
40        ) -> Result<Box<WeekInformation>, DataError> {
41            let prefs = (&locale.0).into();
42
43            Ok(Box::new(WeekInformation(
44                icu_calendar::week::WeekInformation::try_new_with_buffer_provider(
45                    provider.get()?,
46                    prefs,
47                )?,
48            )))
49        }
50
51        /// Returns the weekday that starts the week for this object's locale
52        #[diplomat::rust_link(icu::calendar::week::WeekInformation::first_weekday, StructField)]
53        #[diplomat::attr(auto, getter)]
54        pub fn first_weekday(&self) -> Weekday {
55            self.0.first_weekday.into()
56        }
57
58        #[diplomat::rust_link(icu::calendar::week::WeekInformation::weekend, StructField)]
59        #[diplomat::rust_link(icu::calendar::provider::WeekdaySet::contains, FnInStruct)]
60        pub fn is_weekend(&self, day: Weekday) -> bool {
61            self.0.weekend.contains(day.into())
62        }
63
64        #[diplomat::rust_link(icu::calendar::week::WeekInformation::weekend, FnInStruct)]
65        #[diplomat::attr(auto, getter)]
66        pub fn weekend(&self) -> Box<WeekdaySetIterator> {
67            Box::new(WeekdaySetIterator(self.0.weekend()))
68        }
69    }
70
71    /// Documents which days of the week are considered to be a part of the weekend
72    #[diplomat::rust_link(icu::calendar::week::WeekdaySetIterator, Struct)]
73    #[diplomat::opaque]
74    pub struct WeekdaySetIterator(icu_calendar::week::WeekdaySetIterator);
75
76    impl WeekdaySetIterator {
77        #[diplomat::attr(auto, iterator)]
78        #[diplomat::rust_link(icu::calendar::week::WeekdaySetIterator::next, FnInStruct)]
79        pub fn next(&mut self) -> Option<Weekday> {
80            self.0.next().map(Into::into)
81        }
82    }
83}