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::errors::ffi::{CalendarError, CalendarParseError}; 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)] create( hour: u8, minute: u8, second: u8, subsecond: u32, ) -> Result<Box<Time>, CalendarError>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)] from_string(v: &DiplomatStr) -> Result<Box<Time>, CalendarParseError>47 pub fn from_string(v: &DiplomatStr) -> Result<Box<Time>, CalendarParseError> { 48 Ok(Box::new(Time(icu_time::Time::try_from_utf8(v)?))) 49 } 50 51 /// Creates a new [`Time`] representing midnight (00:00.000). 52 #[diplomat::rust_link(icu::time::Time::midnight, FnInStruct)] 53 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor)] midnight() -> Result<Box<Time>, CalendarError>54 pub fn midnight() -> Result<Box<Time>, CalendarError> { 55 let time = icu_time::Time::midnight(); 56 Ok(Box::new(Time(time))) 57 } 58 59 /// Returns the hour in this time 60 #[diplomat::rust_link(icu::time::Time::hour, StructField)] 61 #[diplomat::attr(auto, getter)] hour(&self) -> u862 pub fn hour(&self) -> u8 { 63 self.0.hour.into() 64 } 65 /// Returns the minute in this time 66 #[diplomat::rust_link(icu::time::Time::minute, StructField)] 67 #[diplomat::attr(auto, getter)] minute(&self) -> u868 pub fn minute(&self) -> u8 { 69 self.0.minute.into() 70 } 71 /// Returns the second in this time 72 #[diplomat::rust_link(icu::time::Time::second, StructField)] 73 #[diplomat::attr(auto, getter)] second(&self) -> u874 pub fn second(&self) -> u8 { 75 self.0.second.into() 76 } 77 /// Returns the subsecond in this time as nanoseconds 78 #[diplomat::rust_link(icu::time::Time::subsecond, StructField)] 79 #[diplomat::attr(auto, getter)] subsecond(&self) -> u3280 pub fn subsecond(&self) -> u32 { 81 self.0.subsecond.into() 82 } 83 } 84 } 85