tor-browser

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

time.rs (3855B)


      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::errors::ffi::{CalendarError, Rfc9557ParseError};
     12 
     13    #[diplomat::opaque]
     14    /// An ICU4X Time object representing a time in terms of hour, minute, second, nanosecond
     15    #[diplomat::rust_link(icu::time::Time, Struct)]
     16    pub struct Time(pub icu_time::Time);
     17 
     18    impl Time {
     19        /// Creates a new [`Time`] given field values
     20        #[diplomat::rust_link(icu::time::Time::try_new, FnInStruct)]
     21        #[diplomat::rust_link(icu::time::Time::new, FnInStruct, hidden)]
     22        #[diplomat::attr(supports = fallible_constructors, constructor)]
     23        pub fn create(
     24            hour: u8,
     25            minute: u8,
     26            second: u8,
     27            subsecond: u32,
     28        ) -> Result<Box<Time>, CalendarError> {
     29            let hour = hour.try_into()?;
     30            let minute = minute.try_into()?;
     31            let second = second.try_into()?;
     32            let subsecond = subsecond.try_into()?;
     33            let time = icu_time::Time {
     34                hour,
     35                minute,
     36                second,
     37                subsecond,
     38            };
     39            Ok(Box::new(Time(time)))
     40        }
     41 
     42        /// Creates a new [`Time`] from an IXDTF string.
     43        #[diplomat::rust_link(icu::time::Time::try_from_str, FnInStruct)]
     44        #[diplomat::rust_link(icu::time::Time::try_from_utf8, FnInStruct, hidden)]
     45        #[diplomat::rust_link(icu::time::Time::from_str, FnInStruct, hidden)]
     46        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor)]
     47        pub fn from_string(v: &DiplomatStr) -> Result<Box<Time>, Rfc9557ParseError> {
     48            Ok(Box::new(Time(icu_time::Time::try_from_utf8(v)?)))
     49        }
     50 
     51        /// Creates a new [`Time`] representing the start of the day (00:00:00.000).
     52        #[diplomat::rust_link(icu::time::Time::start_of_day, FnInStruct)]
     53        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor)]
     54        pub fn start_of_day() -> Result<Box<Time>, CalendarError> {
     55            let time = icu_time::Time::start_of_day();
     56            Ok(Box::new(Time(time)))
     57        }
     58 
     59        /// Creates a new [`Time`] representing noon (12:00:00.000).
     60        #[diplomat::rust_link(icu::time::Time::noon, FnInStruct)]
     61        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor)]
     62        pub fn noon() -> Result<Box<Time>, CalendarError> {
     63            let time = icu_time::Time::noon();
     64            Ok(Box::new(Time(time)))
     65        }
     66 
     67        /// Returns the hour in this time
     68        #[diplomat::rust_link(icu::time::Time::hour, StructField)]
     69        #[diplomat::attr(auto, getter)]
     70        pub fn hour(&self) -> u8 {
     71            self.0.hour.into()
     72        }
     73        /// Returns the minute in this time
     74        #[diplomat::rust_link(icu::time::Time::minute, StructField)]
     75        #[diplomat::attr(auto, getter)]
     76        pub fn minute(&self) -> u8 {
     77            self.0.minute.into()
     78        }
     79        /// Returns the second in this time
     80        #[diplomat::rust_link(icu::time::Time::second, StructField)]
     81        #[diplomat::attr(auto, getter)]
     82        pub fn second(&self) -> u8 {
     83            self.0.second.into()
     84        }
     85        /// Returns the subsecond in this time as nanoseconds
     86        #[diplomat::rust_link(icu::time::Time::subsecond, StructField)]
     87        #[diplomat::attr(auto, getter)]
     88        pub fn subsecond(&self) -> u32 {
     89            self.0.subsecond.into()
     90        }
     91    }
     92 }