tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

week.rs (3702B)


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