• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"]
7 #[diplomat::attr(auto, namespace = "icu4x")]
8 pub mod ffi {
9     use alloc::boxed::Box;
10 
11     #[cfg(feature = "buffer_provider")]
12     use crate::errors::ffi::DataError;
13     #[cfg(feature = "buffer_provider")]
14     use crate::provider::ffi::DataProvider;
15 
16     use crate::locale_core::ffi::Locale;
17 
18     #[diplomat::rust_link(icu::locale::Direction, Enum)]
19     pub enum LocaleDirection {
20         LeftToRight,
21         RightToLeft,
22         Unknown,
23     }
24 
25     #[diplomat::opaque]
26     #[diplomat::rust_link(icu::locale::LocaleDirectionality, Struct)]
27     pub struct LocaleDirectionality(pub icu_locale::LocaleDirectionality);
28 
29     impl LocaleDirectionality {
30         /// Construct a new LocaleDirectionality instance using compiled data.
31         #[diplomat::rust_link(icu::locale::LocaleDirectionality::new_common, FnInStruct)]
32         #[diplomat::attr(supports = constructors, constructor)]
33         #[cfg(feature = "compiled_data")]
create_common() -> Box<LocaleDirectionality>34         pub fn create_common() -> Box<LocaleDirectionality> {
35             Box::new(LocaleDirectionality(
36                 icu_locale::LocaleDirectionality::new_common(),
37             ))
38         }
39 
40         /// Construct a new LocaleDirectionality instance using a particular data source.
41         #[diplomat::rust_link(icu::locale::LocaleDirectionality::new_common, FnInStruct)]
42         #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")]
43         #[cfg(feature = "buffer_provider")]
create_common_with_provider( provider: &DataProvider, ) -> Result<Box<LocaleDirectionality>, DataError>44         pub fn create_common_with_provider(
45             provider: &DataProvider,
46         ) -> Result<Box<LocaleDirectionality>, DataError> {
47             Ok(Box::new(LocaleDirectionality(
48                 icu_locale::LocaleDirectionality::try_new_common_with_buffer_provider(
49                     provider.get()?,
50                 )?,
51             )))
52         }
53         /// Construct a new LocaleDirectionality instance using compiled data.
54         #[diplomat::rust_link(icu::locale::LocaleDirectionality::new_extended, FnInStruct)]
55         #[diplomat::rust_link(
56             icu::locale::LocaleDirectionality::new_with_expander,
57             FnInStruct,
58             hidden
59         )]
60         #[diplomat::attr(auto, named_constructor = "extended")]
61         #[cfg(feature = "compiled_data")]
create_extended() -> Box<LocaleDirectionality>62         pub fn create_extended() -> Box<LocaleDirectionality> {
63             Box::new(LocaleDirectionality(
64                 icu_locale::LocaleDirectionality::new_extended(),
65             ))
66         }
67 
68         /// Construct a new LocaleDirectionality instance using a particular data source.
69         #[diplomat::rust_link(icu::locale::LocaleDirectionality::new_extended, FnInStruct)]
70         #[diplomat::rust_link(
71             icu::locale::LocaleDirectionality::new_with_expander,
72             FnInStruct,
73             hidden
74         )]
75         #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "extended_with_provider")]
76         #[cfg(feature = "buffer_provider")]
create_extended_with_provider( provider: &DataProvider, ) -> Result<Box<LocaleDirectionality>, DataError>77         pub fn create_extended_with_provider(
78             provider: &DataProvider,
79         ) -> Result<Box<LocaleDirectionality>, DataError> {
80             Ok(Box::new(LocaleDirectionality(
81                 icu_locale::LocaleDirectionality::try_new_extended_with_buffer_provider(
82                     provider.get()?,
83                 )?,
84             )))
85         }
86         #[diplomat::rust_link(icu::locale::LocaleDirectionality::get, FnInStruct)]
87         #[diplomat::attr(auto, indexer)]
get(&self, locale: &Locale) -> LocaleDirection88         pub fn get(&self, locale: &Locale) -> LocaleDirection {
89             match self.0.get(&locale.0.id) {
90                 Some(icu_locale::Direction::LeftToRight) => LocaleDirection::LeftToRight,
91                 Some(icu_locale::Direction::RightToLeft) => LocaleDirection::RightToLeft,
92                 _ => LocaleDirection::Unknown,
93             }
94         }
95 
96         #[diplomat::rust_link(icu::locale::LocaleDirectionality::is_left_to_right, FnInStruct)]
is_left_to_right(&self, locale: &Locale) -> bool97         pub fn is_left_to_right(&self, locale: &Locale) -> bool {
98             self.0.is_left_to_right(&locale.0.id)
99         }
100 
101         #[diplomat::rust_link(icu::locale::LocaleDirectionality::is_right_to_left, FnInStruct)]
is_right_to_left(&self, locale: &Locale) -> bool102         pub fn is_right_to_left(&self, locale: &Locale) -> bool {
103             self.0.is_right_to_left(&locale.0.id)
104         }
105     }
106 }
107