| 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 |
#![allow(deprecated)] // these APIs are deprecated in Rust |
| 6 |
|
| 7 |
#[diplomat::bridge] |
| 8 |
pub mod ffi { |
| 9 |
use crate::errors::ffi::ICU4XError; |
| 10 |
use crate::provider::ffi::ICU4XDataProvider; |
| 11 |
use alloc::boxed::Box; |
| 12 |
use icu_timezone::IanaBcp47RoundTripMapper; |
| 13 |
use icu_timezone::IanaToBcp47Mapper; |
| 14 |
use icu_timezone::TimeZoneBcp47Id; |
| 15 |
|
| 16 |
/// An object capable of mapping from an IANA time zone ID to a BCP-47 ID. |
| 17 |
/// |
| 18 |
/// This can be used via `try_set_iana_time_zone_id()` on [`ICU4XCustomTimeZone`]. |
| 19 |
/// |
| 20 |
/// [`ICU4XCustomTimeZone`]: crate::timezone::ffi::ICU4XCustomTimeZone |
| 21 |
#[diplomat::opaque] |
| 22 |
#[diplomat::rust_link(icu::timezone::IanaToBcp47Mapper, Struct)] |
| 23 |
#[diplomat::rust_link(icu::timezone::IanaToBcp47Mapper::as_borrowed, FnInStruct, hidden)] |
| 24 |
#[diplomat::rust_link(icu::timezone::IanaToBcp47MapperBorrowed, Struct, hidden)] |
| 25 |
pub struct ICU4XIanaToBcp47Mapper(pub IanaToBcp47Mapper); |
| 26 |
|
| 27 |
impl ICU4XIanaToBcp47Mapper { |
| 28 |
#[diplomat::rust_link(icu::timezone::IanaToBcp47Mapper::new, FnInStruct)] |
| 29 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
| 30 |
pub fn create( |
| 31 |
provider: &ICU4XDataProvider, |
| 32 |
) -> Result<Box<ICU4XIanaToBcp47Mapper>, ICU4XError> { |
| 33 |
Ok(Box::new(ICU4XIanaToBcp47Mapper(call_constructor!( |
| 34 |
IanaToBcp47Mapper::new [r => Ok(r)], |
| 35 |
IanaToBcp47Mapper::try_new_with_any_provider, |
| 36 |
IanaToBcp47Mapper::try_new_with_buffer_provider, |
| 37 |
provider, |
| 38 |
)?))) |
| 39 |
} |
| 40 |
|
| 41 |
#[diplomat::rust_link(icu::timezone::IanaToBcp47MapperBorrowed::get, FnInStruct)] |
| 42 |
#[diplomat::rust_link( |
| 43 |
icu::timezone::IanaToBcp47MapperBorrowed::get_bytes, |
| 44 |
FnInStruct, |
| 45 |
hidden |
| 46 |
)] |
| 47 |
#[diplomat::rust_link( |
| 48 |
icu::timezone::IanaBcp47RoundTripMapperBorrowed::iana_to_bcp47, |
| 49 |
FnInStruct |
| 50 |
)] |
| 51 |
#[diplomat::attr(supports = indexing, indexer)] |
| 52 |
pub fn get( |
| 53 |
&self, |
| 54 |
value: &DiplomatStr, |
| 55 |
write: &mut diplomat_runtime::DiplomatWriteable, |
| 56 |
) -> Result<(), ICU4XError> { |
| 57 |
use writeable::Writeable; |
| 58 |
let handle = self.0.as_borrowed(); |
| 59 |
if let Some(s) = handle.get_bytes(value) { |
| 60 |
Ok(s.0.write_to(write)?) |
| 61 |
} else { |
| 62 |
Err(ICU4XError::TimeZoneInvalidIdError) |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/// An object capable of mapping from a BCP-47 time zone ID to an IANA ID. |
| 68 |
#[diplomat::opaque] |
| 69 |
#[diplomat::rust_link(icu::timezone::IanaBcp47RoundTripMapper, Struct)] |
| 70 |
#[diplomat::rust_link(icu::timezone::IanaBcp47RoundTripMapperBorrowed, Struct, hidden)] |
| 71 |
#[diplomat::rust_link( |
| 72 |
icu::timezone::IanaBcp47RoundTripMapper::as_borrowed, |
| 73 |
FnInStruct, |
| 74 |
hidden |
| 75 |
)] |
| 76 |
pub struct ICU4XBcp47ToIanaMapper(pub IanaBcp47RoundTripMapper); |
| 77 |
|
| 78 |
impl ICU4XBcp47ToIanaMapper { |
| 79 |
#[diplomat::rust_link(icu::timezone::IanaBcp47RoundTripMapper::new, FnInStruct)] |
| 80 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
| 81 |
pub fn create( |
| 82 |
provider: &ICU4XDataProvider, |
| 83 |
) -> Result<Box<ICU4XBcp47ToIanaMapper>, ICU4XError> { |
| 84 |
Ok(Box::new(ICU4XBcp47ToIanaMapper(call_constructor!( |
| 85 |
IanaBcp47RoundTripMapper::new [r => Ok(r)], |
| 86 |
IanaBcp47RoundTripMapper::try_new_with_any_provider, |
| 87 |
IanaBcp47RoundTripMapper::try_new_with_buffer_provider, |
| 88 |
provider, |
| 89 |
)?))) |
| 90 |
} |
| 91 |
|
| 92 |
/// Writes out the canonical IANA time zone ID corresponding to the given BCP-47 ID. |
| 93 |
|
| 94 |
#[diplomat::rust_link( |
| 95 |
icu::timezone::IanaBcp47RoundTripMapperBorrowed::bcp47_to_iana, |
| 96 |
FnInStruct |
| 97 |
)] |
| 98 |
#[diplomat::attr(supports = indexing, indexer)] |
| 99 |
pub fn get( |
| 100 |
&self, |
| 101 |
value: &DiplomatStr, |
| 102 |
write: &mut diplomat_runtime::DiplomatWriteable, |
| 103 |
) -> Result<(), ICU4XError> { |
| 104 |
use writeable::Writeable; |
| 105 |
let handle = self.0.as_borrowed(); |
| 106 |
tinystr::TinyAsciiStr::from_bytes(value) |
| 107 |
.ok() |
| 108 |
.map(TimeZoneBcp47Id) |
| 109 |
.and_then(|bcp47_id| handle.bcp47_to_iana(bcp47_id)) |
| 110 |
.ok_or(ICU4XError::TimeZoneInvalidIdError)? |
| 111 |
.write_to(write)?; |
| 112 |
Ok(()) |
| 113 |
} |
| 114 |
} |
| 115 |
} |