| 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 |
pub mod ffi { |
| 7 |
use alloc::boxed::Box; |
| 8 |
|
| 9 |
use icu_calendar::Time; |
| 10 |
|
| 11 |
use crate::errors::ffi::ICU4XError; |
| 12 |
|
| 13 |
#[diplomat::opaque] |
| 14 |
/// An ICU4X Time object representing a time in terms of hour, minute, second, nanosecond |
| 15 |
#[diplomat::rust_link(icu::calendar::Time, Struct)] |
| 16 |
pub struct ICU4XTime(pub Time); |
| 17 |
|
| 18 |
impl ICU4XTime { |
| 19 |
/// Creates a new [`ICU4XTime`] given field values |
| 20 |
#[diplomat::rust_link(icu::calendar::Time::try_new, FnInStruct)] |
| 21 |
#[diplomat::rust_link(icu::calendar::Time::new, FnInStruct, hidden)] |
| 22 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
| 23 |
pub fn create( |
| 24 |
hour: u8, |
| 25 |
minute: u8, |
| 26 |
second: u8, |
| 27 |
nanosecond: u32, |
| 28 |
) -> Result<Box<ICU4XTime>, ICU4XError> { |
| 29 |
let hour = hour.try_into()?; |
| 30 |
let minute = minute.try_into()?; |
| 31 |
let second = second.try_into()?; |
| 32 |
let nanosecond = nanosecond.try_into()?; |
| 33 |
let time = Time { |
| 34 |
hour, |
| 35 |
minute, |
| 36 |
second, |
| 37 |
nanosecond, |
| 38 |
}; |
| 39 |
Ok(Box::new(ICU4XTime(time))) |
| 40 |
} |
| 41 |
|
| 42 |
/// Creates a new [`ICU4XTime`] representing midnight (00:00.000). |
| 43 |
#[diplomat::rust_link(icu::calendar::Time::midnight, FnInStruct)] |
| 44 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "midnight")] |
| 45 |
pub fn create_midnight() -> Result<Box<ICU4XTime>, ICU4XError> { |
| 46 |
let time = Time::midnight(); |
| 47 |
Ok(Box::new(ICU4XTime(time))) |
| 48 |
} |
| 49 |
|
| 50 |
/// Returns the hour in this time |
| 51 |
#[diplomat::rust_link(icu::calendar::Time::hour, StructField)] |
| 52 |
#[diplomat::attr(supports = accessors, getter)] |
| 53 |
pub fn hour(&self) -> u8 { |
| 54 |
self.0.hour.into() |
| 55 |
} |
| 56 |
/// Returns the minute in this time |
| 57 |
#[diplomat::rust_link(icu::calendar::Time::minute, StructField)] |
| 58 |
#[diplomat::attr(supports = accessors, getter)] |
| 59 |
pub fn minute(&self) -> u8 { |
| 60 |
self.0.minute.into() |
| 61 |
} |
| 62 |
/// Returns the second in this time |
| 63 |
#[diplomat::rust_link(icu::calendar::Time::second, StructField)] |
| 64 |
#[diplomat::attr(supports = accessors, getter)] |
| 65 |
pub fn second(&self) -> u8 { |
| 66 |
self.0.second.into() |
| 67 |
} |
| 68 |
/// Returns the nanosecond in this time |
| 69 |
#[diplomat::rust_link(icu::calendar::Time::nanosecond, StructField)] |
| 70 |
#[diplomat::attr(supports = accessors, getter)] |
| 71 |
pub fn nanosecond(&self) -> u32 { |
| 72 |
self.0.nanosecond.into() |
| 73 |
} |
| 74 |
} |
| 75 |
} |