tor-browser

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

datetime.rs (2737B)


      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 icu_calendar::Iso;
     11 
     12    use crate::unstable::calendar::ffi::Calendar;
     13    use crate::unstable::date::ffi::{Date, IsoDate};
     14    use crate::unstable::errors::ffi::Rfc9557ParseError;
     15    use crate::unstable::time::ffi::Time;
     16 
     17    /// An ICU4X DateTime object capable of containing a ISO-8601 date and time.
     18    #[diplomat::rust_link(icu::time::DateTime, Struct)]
     19    #[diplomat::out]
     20    pub struct IsoDateTime {
     21        pub date: Box<IsoDate>,
     22        pub time: Box<Time>,
     23    }
     24 
     25    impl IsoDateTime {
     26        /// Creates a new [`IsoDateTime`] from an IXDTF string.
     27        #[diplomat::rust_link(icu::time::DateTime::try_from_str, FnInStruct)]
     28        #[diplomat::rust_link(icu::time::DateTime::try_from_utf8, FnInStruct, hidden)]
     29        #[diplomat::rust_link(icu::time::DateTime::from_str, FnInStruct, hidden)]
     30        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor)]
     31        pub fn from_string(v: &DiplomatStr) -> Result<IsoDateTime, Rfc9557ParseError> {
     32            let icu_time::DateTime { date, time } = icu_time::DateTime::try_from_utf8(v, Iso)?;
     33            Ok(IsoDateTime {
     34                date: Box::new(IsoDate(date)),
     35                time: Box::new(Time(time)),
     36            })
     37        }
     38    }
     39 
     40    /// An ICU4X DateTime object capable of containing a date and time for any calendar.
     41    #[diplomat::rust_link(icu::time::DateTime, Struct)]
     42    #[diplomat::out]
     43    pub struct DateTime {
     44        pub date: Box<Date>,
     45        pub time: Box<Time>,
     46    }
     47 
     48    impl DateTime {
     49        /// Creates a new [`DateTime`] from an IXDTF string.
     50        #[diplomat::rust_link(icu::time::DateTime::try_from_str, FnInStruct)]
     51        #[diplomat::rust_link(icu::time::DateTime::try_from_utf8, FnInStruct, hidden)]
     52        #[diplomat::rust_link(icu::time::DateTime::from_str, FnInStruct, hidden)]
     53        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor)]
     54        pub fn from_string(
     55            v: &DiplomatStr,
     56            calendar: &Calendar,
     57        ) -> Result<DateTime, Rfc9557ParseError> {
     58            let icu_time::DateTime { date, time } =
     59                icu_time::DateTime::try_from_utf8(v, calendar.0.clone())?;
     60            Ok(DateTime {
     61                date: Box::new(Date(date)),
     62                time: Box::new(Time(time)),
     63            })
     64        }
     65    }
     66 }