• 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     use diplomat_runtime::{DiplomatStr16Slice, DiplomatStrSlice};
11     #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
12     use icu_list::{options::ListFormatterOptions, ListFormatterPreferences};
13 
14     #[cfg(feature = "buffer_provider")]
15     use crate::provider::ffi::DataProvider;
16     #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
17     use crate::{errors::ffi::DataError, locale_core::ffi::Locale};
18 
19     use writeable::Writeable;
20 
21     #[diplomat::rust_link(icu::list::options::ListLength, Enum)]
22     #[diplomat::enum_convert(icu_list::options::ListLength, needs_wildcard)]
23     pub enum ListLength {
24         Wide,
25         Short,
26         Narrow,
27     }
28     #[diplomat::opaque]
29     #[diplomat::rust_link(icu::list::ListFormatter, Struct)]
30     pub struct ListFormatter(pub icu_list::ListFormatter);
31 
32     impl ListFormatter {
33         /// Construct a new ListFormatter instance for And patterns from compiled data.
34         #[diplomat::rust_link(icu::list::ListFormatter::try_new_and, FnInStruct)]
35         #[diplomat::rust_link(icu::list::options::ListFormatterOptions, Struct, hidden)]
36         #[diplomat::rust_link(
37             icu::list::options::ListFormatterOptions::with_length,
38             FnInStruct,
39             hidden
40         )]
41         #[diplomat::rust_link(
42             icu::list::options::ListFormatterOptions::default,
43             FnInStruct,
44             hidden
45         )]
46         #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "and_with_length")]
47         #[cfg(feature = "compiled_data")]
48         #[diplomat::demo(default_constructor)]
create_and_with_length( locale: &Locale, length: ListLength, ) -> Result<Box<ListFormatter>, DataError>49         pub fn create_and_with_length(
50             locale: &Locale,
51             length: ListLength,
52         ) -> Result<Box<ListFormatter>, DataError> {
53             let prefs = ListFormatterPreferences::from(&locale.0);
54             let options = ListFormatterOptions::default().with_length(length.into());
55             Ok(Box::new(ListFormatter(
56                 icu_list::ListFormatter::try_new_and(prefs, options)?,
57             )))
58         }
59 
60         /// Construct a new ListFormatter instance for And patterns
61         #[diplomat::rust_link(icu::list::ListFormatter::try_new_and, FnInStruct)]
62         #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "and_with_length_and_provider")]
63         #[cfg(feature = "buffer_provider")]
create_and_with_length_and_provider( provider: &DataProvider, locale: &Locale, length: ListLength, ) -> Result<Box<ListFormatter>, DataError>64         pub fn create_and_with_length_and_provider(
65             provider: &DataProvider,
66             locale: &Locale,
67             length: ListLength,
68         ) -> Result<Box<ListFormatter>, DataError> {
69             let prefs = ListFormatterPreferences::from(&locale.0);
70             let options = ListFormatterOptions::default().with_length(length.into());
71             Ok(Box::new(ListFormatter(
72                 icu_list::ListFormatter::try_new_and_with_buffer_provider(
73                     provider.get()?,
74                     prefs,
75                     options,
76                 )?,
77             )))
78         }
79 
80         /// Construct a new ListFormatter instance for And patterns from compiled data.
81         #[diplomat::rust_link(icu::list::ListFormatter::try_new_or, FnInStruct)]
82         #[diplomat::rust_link(icu::list::options::ListFormatterOptions, Struct, hidden)]
83         #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "or_with_length")]
84         #[cfg(feature = "compiled_data")]
create_or_with_length( locale: &Locale, length: ListLength, ) -> Result<Box<ListFormatter>, DataError>85         pub fn create_or_with_length(
86             locale: &Locale,
87             length: ListLength,
88         ) -> Result<Box<ListFormatter>, DataError> {
89             let prefs = ListFormatterPreferences::from(&locale.0);
90             let options = ListFormatterOptions::default().with_length(length.into());
91             Ok(Box::new(ListFormatter(
92                 icu_list::ListFormatter::try_new_or(prefs, options)?,
93             )))
94         }
95 
96         /// Construct a new ListFormatter instance for And patterns
97         #[diplomat::rust_link(icu::list::ListFormatter::try_new_or, FnInStruct)]
98         #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "or_with_length_and_provider")]
99         #[cfg(feature = "buffer_provider")]
create_or_with_length_and_provider( provider: &DataProvider, locale: &Locale, length: ListLength, ) -> Result<Box<ListFormatter>, DataError>100         pub fn create_or_with_length_and_provider(
101             provider: &DataProvider,
102             locale: &Locale,
103             length: ListLength,
104         ) -> Result<Box<ListFormatter>, DataError> {
105             let prefs = ListFormatterPreferences::from(&locale.0);
106             let options = ListFormatterOptions::default().with_length(length.into());
107             Ok(Box::new(ListFormatter(
108                 icu_list::ListFormatter::try_new_or_with_buffer_provider(
109                     provider.get()?,
110                     prefs,
111                     options,
112                 )?,
113             )))
114         }
115 
116         /// Construct a new ListFormatter instance for And patterns from compiled data.
117         #[diplomat::rust_link(icu::list::ListFormatter::try_new_unit, FnInStruct)]
118         #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "unit_with_length")]
119         #[cfg(feature = "compiled_data")]
create_unit_with_length( locale: &Locale, length: ListLength, ) -> Result<Box<ListFormatter>, DataError>120         pub fn create_unit_with_length(
121             locale: &Locale,
122             length: ListLength,
123         ) -> Result<Box<ListFormatter>, DataError> {
124             let prefs = ListFormatterPreferences::from(&locale.0);
125             let options = ListFormatterOptions::default().with_length(length.into());
126             Ok(Box::new(ListFormatter(
127                 icu_list::ListFormatter::try_new_unit(prefs, options)?,
128             )))
129         }
130 
131         /// Construct a new ListFormatter instance for And patterns
132         #[diplomat::rust_link(icu::list::ListFormatter::try_new_unit, FnInStruct)]
133         #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "unit_with_length_and_provider")]
134         #[cfg(feature = "buffer_provider")]
create_unit_with_length_and_provider( provider: &DataProvider, locale: &Locale, length: ListLength, ) -> Result<Box<ListFormatter>, DataError>135         pub fn create_unit_with_length_and_provider(
136             provider: &DataProvider,
137             locale: &Locale,
138             length: ListLength,
139         ) -> Result<Box<ListFormatter>, DataError> {
140             let prefs = ListFormatterPreferences::from(&locale.0);
141             let options = ListFormatterOptions::default().with_length(length.into());
142             Ok(Box::new(ListFormatter(
143                 icu_list::ListFormatter::try_new_unit_with_buffer_provider(
144                     provider.get()?,
145                     prefs,
146                     options,
147                 )?,
148             )))
149         }
150 
151         #[diplomat::rust_link(icu::list::ListFormatter::format, FnInStruct)]
152         #[diplomat::rust_link(icu::list::ListFormatter::format_to_string, FnInStruct, hidden)]
153         #[diplomat::rust_link(icu::list::FormattedList, Struct, hidden)]
154         #[diplomat::attr(not(supports = utf8_strings), disable)]
155         #[diplomat::attr(*, rename = "format")]
format_utf8(&self, list: &[DiplomatStrSlice], write: &mut DiplomatWrite)156         pub fn format_utf8(&self, list: &[DiplomatStrSlice], write: &mut DiplomatWrite) {
157             let _infallible = self
158                 .0
159                 .format(
160                     list.iter()
161                         .map(|a| potential_utf::PotentialUtf8::from_bytes(a))
162                         .map(writeable::adapters::LossyWrap),
163                 )
164                 .write_to(write);
165         }
166 
167         #[diplomat::rust_link(icu::list::ListFormatter::format, FnInStruct)]
168         #[diplomat::rust_link(icu::list::ListFormatter::format_to_string, FnInStruct, hidden)]
169         #[diplomat::rust_link(icu::list::FormattedList, Struct, hidden)]
170         #[diplomat::attr(not(supports = utf8_strings), rename = "format")]
171         #[diplomat::attr(supports = utf8_strings, rename = "format16")]
format_utf16(&self, list: &[DiplomatStr16Slice], write: &mut DiplomatWrite)172         pub fn format_utf16(&self, list: &[DiplomatStr16Slice], write: &mut DiplomatWrite) {
173             let _infallible = self
174                 .0
175                 .format(
176                     list.iter()
177                         .map(|a| potential_utf::PotentialUtf16::from_slice(a))
178                         .map(writeable::adapters::LossyWrap),
179                 )
180                 .write_to(write);
181         }
182     }
183 }
184