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 ).
7 use crate::errors::ffi::ICU4XError;
8 use crate::provider::ffi::ICU4XDataProvider;
10 use icu_timezone::IanaBcp47RoundTripMapper;
11 use icu_timezone::IanaToBcp47Mapper;
12 use icu_timezone::TimeZoneBcp47Id;
14 /// An object capable of mapping from an IANA time zone ID to a BCP-47 ID.
16 /// This can be used via `try_set_iana_time_zone_id()` on [`ICU4XCustomTimeZone`].
18 /// [`ICU4XCustomTimeZone`]: crate::timezone::ffi::ICU4XCustomTimeZone
20 #[diplomat::rust_link(icu::timezone::IanaToBcp47Mapper, Struct)]
21 #[diplomat::rust_link(icu::timezone::IanaToBcp47Mapper::as_borrowed, FnInStruct, hidden)]
22 pub struct ICU4XIanaToBcp47Mapper(pub IanaToBcp47Mapper);
24 impl ICU4XIanaToBcp47Mapper {
25 #[diplomat::rust_link(icu::timezone::IanaToBcp47Mapper::new, FnInStruct)]
27 provider: &ICU4XDataProvider,
28 ) -> Result<Box<ICU4XIanaToBcp47Mapper>, ICU4XError> {
29 Ok(Box::new(ICU4XIanaToBcp47Mapper(call_constructor!(
30 IanaToBcp47Mapper::new [r => Ok(r)],
31 IanaToBcp47Mapper::try_new_with_any_provider,
32 IanaToBcp47Mapper::try_new_with_buffer_provider,
38 /// An object capable of mapping from a BCP-47 time zone ID to an IANA ID.
40 #[diplomat::rust_link(icu::timezone::IanaBcp47RoundTripMapper, Struct)]
41 #[diplomat::rust_link(
42 icu::timezone::IanaBcp47RoundTripMapper::as_borrowed,
46 pub struct ICU4XBcp47ToIanaMapper(pub IanaBcp47RoundTripMapper);
48 impl ICU4XBcp47ToIanaMapper {
49 #[diplomat::rust_link(icu::timezone::IanaBcp47RoundTripMapper::new, FnInStruct)]
51 provider: &ICU4XDataProvider,
52 ) -> Result<Box<ICU4XBcp47ToIanaMapper>, ICU4XError> {
53 Ok(Box::new(ICU4XBcp47ToIanaMapper(call_constructor!(
54 IanaBcp47RoundTripMapper::new [r => Ok(r)],
55 IanaBcp47RoundTripMapper::try_new_with_any_provider,
56 IanaBcp47RoundTripMapper::try_new_with_buffer_provider,
61 /// Writes out the canonical IANA time zone ID corresponding to the given BCP-47 ID.
62 #[diplomat::rust_link(
63 icu::datetime::time_zone::IanaBcp47RoundTripMapper::bcp47_to_iana,
69 write: &mut diplomat_runtime::DiplomatWriteable,
70 ) -> Result<(), ICU4XError> {
71 use core::str::FromStr;
72 use writeable::Writeable;
73 let handle = self.0.as_borrowed();
74 TimeZoneBcp47Id::from_str(value)
76 .and_then(|bcp47_id| handle.bcp47_to_iana(bcp47_id))
77 .ok_or(ICU4XError::TimeZoneInvalidIdError)?