Bug 1869647 - Mark hasStorageAccess.sub.https.window.html as intermittent after wpt...
[gecko.git] / intl / icu_capi / src / iana_bcp47_mapper.rs
blobc77730410dc7260d33463caf8cefeac4efa23388
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 ).
5 #[diplomat::bridge]
6 pub mod ffi {
7     use crate::errors::ffi::ICU4XError;
8     use crate::provider::ffi::ICU4XDataProvider;
9     use alloc::boxed::Box;
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.
15     ///
16     /// This can be used via `try_set_iana_time_zone_id()` on [`ICU4XCustomTimeZone`].
17     ///
18     /// [`ICU4XCustomTimeZone`]: crate::timezone::ffi::ICU4XCustomTimeZone
19     #[diplomat::opaque]
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)]
26         pub fn create(
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,
33                 provider,
34             )?)))
35         }
36     }
38     /// An object capable of mapping from a BCP-47 time zone ID to an IANA ID.
39     #[diplomat::opaque]
40     #[diplomat::rust_link(icu::timezone::IanaBcp47RoundTripMapper, Struct)]
41     #[diplomat::rust_link(
42         icu::timezone::IanaBcp47RoundTripMapper::as_borrowed,
43         FnInStruct,
44         hidden
45     )]
46     pub struct ICU4XBcp47ToIanaMapper(pub IanaBcp47RoundTripMapper);
48     impl ICU4XBcp47ToIanaMapper {
49         #[diplomat::rust_link(icu::timezone::IanaBcp47RoundTripMapper::new, FnInStruct)]
50         pub fn create(
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,
57                 provider,
58             )?)))
59         }
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,
64             FnInStruct
65         )]
66         pub fn get(
67             &self,
68             value: &str,
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)
75                 .ok()
76                 .and_then(|bcp47_id| handle.bcp47_to_iana(bcp47_id))
77                 .ok_or(ICU4XError::TimeZoneInvalidIdError)?
78                 .write_to(write)?;
79             Ok(())
80         }
81     }