Skip to main content

icu_capi/
time.rs

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