Bug 1910110 - Return early when channel URI is void r=rpl a=Aryx
[gecko.git] / intl / icu_capi / src / locale_directionality.rs
blobdaf3b75780f4260ae2671012b9108406c255bd3c
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::{
8         errors::ffi::ICU4XError,
9         locale::ffi::ICU4XLocale,
10         locid_transform::ffi::ICU4XLocaleExpander,
11         provider::{ffi::ICU4XDataProvider, ICU4XDataProviderInner},
12     };
13     use alloc::boxed::Box;
14     use icu_locid_transform::{Direction, LocaleDirectionality};
16     #[diplomat::rust_link(icu::locid_transform::Direction, Enum)]
17     pub enum ICU4XLocaleDirection {
18         LeftToRight,
19         RightToLeft,
20         Unknown,
21     }
23     #[diplomat::opaque]
24     #[diplomat::rust_link(icu::locid_transform::LocaleDirectionality, Struct)]
25     pub struct ICU4XLocaleDirectionality(pub LocaleDirectionality);
27     impl ICU4XLocaleDirectionality {
28         /// Construct a new ICU4XLocaleDirectionality instance
29         #[diplomat::rust_link(icu::locid_transform::LocaleDirectionality::new, FnInStruct)]
30         #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
31         pub fn create(
32             provider: &ICU4XDataProvider,
33         ) -> Result<Box<ICU4XLocaleDirectionality>, ICU4XError> {
34             Ok(Box::new(ICU4XLocaleDirectionality(call_constructor!(
35                 LocaleDirectionality::new [r => Ok(r)],
36                 LocaleDirectionality::try_new_with_any_provider,
37                 LocaleDirectionality::try_new_with_buffer_provider,
38                 provider,
39             )?)))
40         }
42         /// Construct a new ICU4XLocaleDirectionality instance with a custom expander
43         #[diplomat::rust_link(
44             icu::locid_transform::LocaleDirectionality::new_with_expander,
45             FnInStruct
46         )]
47         #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_expander")]
48         pub fn create_with_expander(
49             provider: &ICU4XDataProvider,
50             expander: &ICU4XLocaleExpander,
51         ) -> Result<Box<ICU4XLocaleDirectionality>, ICU4XError> {
52             #[allow(unused_imports)]
53             use icu_provider::prelude::*;
54             Ok(Box::new(ICU4XLocaleDirectionality(match &provider.0 {
55                 ICU4XDataProviderInner::Destroyed => Err(icu_provider::DataError::custom(
56                     "This provider has been destroyed",
57                 ))?,
58                 ICU4XDataProviderInner::Empty => {
59                     LocaleDirectionality::try_new_with_expander_unstable(
60                         &icu_provider_adapters::empty::EmptyDataProvider::new(),
61                         expander.0.clone(),
62                     )?
63                 }
64                 #[cfg(feature = "buffer_provider")]
65                 ICU4XDataProviderInner::Buffer(buffer_provider) => {
66                     LocaleDirectionality::try_new_with_expander_unstable(
67                         &buffer_provider.as_deserializing(),
68                         expander.0.clone(),
69                     )?
70                 }
71                 #[cfg(feature = "compiled_data")]
72                 ICU4XDataProviderInner::Compiled => {
73                     LocaleDirectionality::new_with_expander(expander.0.clone())
74                 }
75             })))
76         }
78         #[diplomat::rust_link(icu::locid_transform::LocaleDirectionality::get, FnInStruct)]
79         #[diplomat::attr(supports = indexing, indexer)]
80         pub fn get(&self, locale: &ICU4XLocale) -> ICU4XLocaleDirection {
81             match self.0.get(&locale.0) {
82                 Some(Direction::LeftToRight) => ICU4XLocaleDirection::LeftToRight,
83                 Some(Direction::RightToLeft) => ICU4XLocaleDirection::RightToLeft,
84                 _ => ICU4XLocaleDirection::Unknown,
85             }
86         }
88         #[diplomat::rust_link(
89             icu::locid_transform::LocaleDirectionality::is_left_to_right,
90             FnInStruct
91         )]
92         pub fn is_left_to_right(&self, locale: &ICU4XLocale) -> bool {
93             self.0.is_left_to_right(&locale.0)
94         }
96         #[diplomat::rust_link(
97             icu::locid_transform::LocaleDirectionality::is_right_to_left,
98             FnInStruct
99         )]
100         pub fn is_right_to_left(&self, locale: &ICU4XLocale) -> bool {
101             self.0.is_right_to_left(&locale.0)
102         }
103     }