tor-browser

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

calendar.rs (4284B)


      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    use alloc::sync::Arc;
     11 
     12    #[cfg(feature = "buffer_provider")]
     13    use crate::unstable::errors::ffi::DataError;
     14    use crate::unstable::locale_core::ffi::Locale;
     15    #[cfg(feature = "buffer_provider")]
     16    use crate::unstable::provider::ffi::DataProvider;
     17 
     18    /// The various calendar types currently supported by [`Calendar`]
     19    #[diplomat::enum_convert(icu_calendar::AnyCalendarKind, needs_wildcard)]
     20    #[diplomat::rust_link(icu::calendar::AnyCalendarKind, Enum)]
     21    pub enum CalendarKind {
     22        /// The kind of an Iso calendar
     23        Iso = 0,
     24        /// The kind of a Gregorian calendar
     25        Gregorian = 1,
     26        /// The kind of a Buddhist calendar
     27        Buddhist = 2,
     28        /// The kind of a Japanese calendar with modern eras
     29        Japanese = 3,
     30        /// The kind of a Japanese calendar with modern and historic eras
     31        JapaneseExtended = 4,
     32        /// The kind of an Ethiopian calendar, with Amete Mihret era
     33        Ethiopian = 5,
     34        /// The kind of an Ethiopian calendar, with Amete Alem era
     35        EthiopianAmeteAlem = 6,
     36        /// The kind of a Indian calendar
     37        Indian = 7,
     38        /// The kind of a Coptic calendar
     39        Coptic = 8,
     40        /// The kind of a Dangi calendar
     41        Dangi = 9,
     42        /// The kind of a Chinese calendar
     43        Chinese = 10,
     44        /// The kind of a Hebrew calendar
     45        Hebrew = 11,
     46        /// The kind of a Hijri tabular, type II leap years, Friday epoch, calendar
     47        HijriTabularTypeIIFriday = 12,
     48        /// The kind of a Hijri simulated, Mecca calendar
     49        HijriSimulatedMecca = 18,
     50        /// The kind of a Hijri tabular, type II leap years, Thursday epoch, calendar
     51        HijriTabularTypeIIThursday = 14,
     52        /// The kind of a Hijri Umm al-Qura calendar
     53        HijriUmmAlQura = 15,
     54        /// The kind of a Persian calendar
     55        Persian = 16,
     56        /// The kind of a Roc calendar
     57        Roc = 17,
     58    }
     59 
     60    impl CalendarKind {
     61        /// Creates a new [`CalendarKind`] for the specified locale, using compiled data.
     62        #[diplomat::rust_link(icu::calendar::AnyCalendarKind::new, FnInEnum)]
     63        pub fn create(locale: &Locale) -> Self {
     64            let prefs = (&locale.0).into();
     65            icu_calendar::AnyCalendarKind::new(prefs).into()
     66        }
     67    }
     68 
     69    #[diplomat::opaque]
     70    #[diplomat::transparent_convert]
     71    #[diplomat::rust_link(icu::calendar::AnyCalendar, Enum)]
     72    pub struct Calendar(pub Arc<icu_calendar::AnyCalendar>);
     73 
     74    impl Calendar {
     75        /// Creates a new [`Calendar`] for the specified kind, using compiled data.
     76        #[diplomat::rust_link(icu::calendar::AnyCalendar::new, FnInEnum)]
     77        #[diplomat::attr(auto, constructor)]
     78        #[cfg(feature = "compiled_data")]
     79        pub fn create(kind: CalendarKind) -> Box<Calendar> {
     80            Box::new(Calendar(Arc::new(icu_calendar::AnyCalendar::new(
     81                kind.into(),
     82            ))))
     83        }
     84 
     85        /// Creates a new [`Calendar`] for the specified kind, using a particular data source.
     86        #[diplomat::rust_link(icu::calendar::AnyCalendar::new, FnInEnum)]
     87        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "new_with_provider")]
     88        #[cfg(feature = "buffer_provider")]
     89        pub fn create_with_provider(
     90            provider: &DataProvider,
     91            kind: CalendarKind,
     92        ) -> Result<Box<Calendar>, DataError> {
     93            Ok(Box::new(Calendar(Arc::new(
     94                icu_calendar::AnyCalendar::try_new_with_buffer_provider(
     95                    provider.get()?,
     96                    kind.into(),
     97                )?,
     98            ))))
     99        }
    100 
    101        /// Returns the kind of this calendar
    102        #[diplomat::rust_link(icu::calendar::AnyCalendar::kind, FnInEnum)]
    103        #[diplomat::attr(auto, getter)]
    104        pub fn kind(&self) -> CalendarKind {
    105            self.0.kind().into()
    106        }
    107    }
    108 }