displaynames.rs (9643B)
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(any(feature = "compiled_data", feature = "buffer_provider"))] 12 use crate::unstable::errors::ffi::DataError; 13 use crate::unstable::errors::ffi::LocaleParseError; 14 use crate::unstable::locale_core::ffi::Locale; 15 #[cfg(feature = "buffer_provider")] 16 use crate::unstable::provider::ffi::DataProvider; 17 use diplomat_runtime::DiplomatOption; 18 19 use writeable::Writeable; 20 21 #[diplomat::opaque] 22 #[diplomat::rust_link(icu::experimental::displaynames::LocaleDisplayNamesFormatter, Struct)] 23 pub struct LocaleDisplayNamesFormatter( 24 pub icu_experimental::displaynames::LocaleDisplayNamesFormatter, 25 ); 26 27 #[diplomat::opaque] 28 #[diplomat::rust_link(icu::experimental::displaynames::RegionDisplayNames, Struct)] 29 pub struct RegionDisplayNames(pub icu_experimental::displaynames::RegionDisplayNames); 30 31 #[diplomat::rust_link(icu::experimental::displaynames::options::DisplayNamesOptions, Struct)] 32 #[diplomat::attr(supports = non_exhaustive_structs, rename = "DisplayNamesOptions")] 33 pub struct DisplayNamesOptionsV1 { 34 /// The optional formatting style to use for display name. 35 pub style: DiplomatOption<DisplayNamesStyle>, 36 /// The fallback return when the system does not have the 37 /// requested display name, defaults to "code". 38 pub fallback: DiplomatOption<DisplayNamesFallback>, 39 /// The language display kind, defaults to "dialect". 40 pub language_display: DiplomatOption<LanguageDisplay>, 41 } 42 43 #[diplomat::rust_link(icu::experimental::displaynames::options::Style, Enum)] 44 #[diplomat::enum_convert(icu_experimental::displaynames::Style, needs_wildcard)] 45 pub enum DisplayNamesStyle { 46 Narrow, 47 Short, 48 Long, 49 Menu, 50 } 51 52 #[diplomat::rust_link(icu::experimental::displaynames::options::Fallback, Enum)] 53 #[diplomat::enum_convert(icu_experimental::displaynames::Fallback, needs_wildcard)] 54 pub enum DisplayNamesFallback { 55 Code, 56 None, 57 } 58 59 #[diplomat::rust_link(icu::experimental::displaynames::options::LanguageDisplay, Enum)] 60 #[diplomat::enum_convert(icu_experimental::displaynames::LanguageDisplay, needs_wildcard)] 61 pub enum LanguageDisplay { 62 Dialect, 63 Standard, 64 } 65 66 impl LocaleDisplayNamesFormatter { 67 /// Creates a new `LocaleDisplayNamesFormatter` from locale data and an options bag using compiled data. 68 #[diplomat::rust_link( 69 icu::experimental::displaynames::LocaleDisplayNamesFormatter::try_new, 70 FnInStruct 71 )] 72 #[diplomat::attr(all(supports = fallible_constructors, supports = non_exhaustive_structs), constructor)] 73 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors, not(supports = non_exhaustive_structs)), named_constructor = "v1")] 74 #[diplomat::attr(supports = non_exhaustive_structs, rename = "create")] 75 #[cfg(feature = "compiled_data")] 76 pub fn create_v1( 77 locale: &Locale, 78 options: DisplayNamesOptionsV1, 79 ) -> Result<Box<LocaleDisplayNamesFormatter>, DataError> { 80 let prefs = (&locale.0).into(); 81 let options = icu_experimental::displaynames::DisplayNamesOptions::from(options); 82 83 Ok(Box::new(LocaleDisplayNamesFormatter( 84 icu_experimental::displaynames::LocaleDisplayNamesFormatter::try_new( 85 prefs, options, 86 )?, 87 ))) 88 } 89 90 /// Creates a new `LocaleDisplayNamesFormatter` from locale data and an options bag using a particular data source. 91 #[diplomat::rust_link( 92 icu::experimental::displaynames::LocaleDisplayNamesFormatter::try_new, 93 FnInStruct 94 )] 95 #[diplomat::attr(supports = non_exhaustive_structs, rename = "create_with_provider")] 96 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors, supports = non_exhaustive_structs), named_constructor = "with_provider")] 97 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors, not(supports = non_exhaustive_structs)), named_constructor = "v1_with_provider")] 98 #[cfg(feature = "buffer_provider")] 99 pub fn create_v1_with_provider( 100 provider: &DataProvider, 101 locale: &Locale, 102 options: DisplayNamesOptionsV1, 103 ) -> Result<Box<LocaleDisplayNamesFormatter>, DataError> { 104 let prefs = (&locale.0).into(); 105 let options = icu_experimental::displaynames::DisplayNamesOptions::from(options); 106 107 Ok(Box::new(LocaleDisplayNamesFormatter( 108 icu_experimental::displaynames::LocaleDisplayNamesFormatter::try_new_with_buffer_provider(provider.get()?, prefs, 109 options, 110 )?, 111 ))) 112 } 113 114 /// Returns the locale-specific display name of a locale. 115 #[diplomat::rust_link( 116 icu::experimental::displaynames::LocaleDisplayNamesFormatter::of, 117 FnInStruct 118 )] 119 // Experimental, do not generate in demo: 120 #[diplomat::attr(demo_gen, disable)] 121 pub fn of(&self, locale: &Locale, write: &mut DiplomatWrite) { 122 let _infallible = self.0.of(&locale.0).write_to(write); 123 } 124 } 125 126 impl RegionDisplayNames { 127 /// Creates a new `RegionDisplayNames` from locale data and an options bag using compiled data. 128 #[diplomat::rust_link( 129 icu::experimental::displaynames::RegionDisplayNames::try_new, 130 FnInStruct 131 )] 132 #[diplomat::attr(all(supports = fallible_constructors, supports = non_exhaustive_structs), constructor)] 133 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors, not(supports = non_exhaustive_structs)), named_constructor = "v1")] 134 #[diplomat::attr(supports = non_exhaustive_structs, rename = "create")] 135 #[cfg(feature = "compiled_data")] 136 pub fn create_v1( 137 locale: &Locale, 138 options: DisplayNamesOptionsV1, 139 ) -> Result<Box<RegionDisplayNames>, DataError> { 140 let prefs = (&locale.0).into(); 141 let options = icu_experimental::displaynames::DisplayNamesOptions::from(options); 142 Ok(Box::new(RegionDisplayNames( 143 icu_experimental::displaynames::RegionDisplayNames::try_new(prefs, options)?, 144 ))) 145 } 146 147 /// Creates a new `RegionDisplayNames` from locale data and an options bag using a particular data source. 148 #[diplomat::rust_link( 149 icu::experimental::displaynames::RegionDisplayNames::try_new, 150 FnInStruct 151 )] 152 #[diplomat::attr(supports = non_exhaustive_structs, rename = "create_with_provider")] 153 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors, supports = non_exhaustive_structs), named_constructor = "with_provider")] 154 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors, not(supports = non_exhaustive_structs)), named_constructor = "v1_with_provider")] 155 #[cfg(feature = "buffer_provider")] 156 pub fn create_v1_with_provider( 157 provider: &DataProvider, 158 locale: &Locale, 159 options: DisplayNamesOptionsV1, 160 ) -> Result<Box<RegionDisplayNames>, DataError> { 161 let prefs = (&locale.0).into(); 162 let options = icu_experimental::displaynames::DisplayNamesOptions::from(options); 163 Ok(Box::new(RegionDisplayNames( 164 icu_experimental::displaynames::RegionDisplayNames::try_new_with_buffer_provider( 165 provider.get()?, 166 prefs, 167 options, 168 )?, 169 ))) 170 } 171 172 /// Returns the locale specific display name of a region. 173 /// Note that the function returns an empty string in case the display name for a given 174 /// region code is not found. 175 #[diplomat::rust_link(icu::experimental::displaynames::RegionDisplayNames::of, FnInStruct)] 176 // Experimental, do not generate in demo: 177 #[diplomat::attr(demo_gen, disable)] 178 pub fn of( 179 &self, 180 region: &DiplomatStr, 181 write: &mut DiplomatWrite, 182 ) -> Result<(), LocaleParseError> { 183 let _infallible = self 184 .0 185 .of(icu_locale_core::subtags::Region::try_from_utf8(region)?) 186 .unwrap_or("") 187 .write_to(write); 188 Ok(()) 189 } 190 } 191 } 192 193 impl From<ffi::DisplayNamesOptionsV1> for icu_experimental::displaynames::DisplayNamesOptions { 194 fn from( 195 other: ffi::DisplayNamesOptionsV1, 196 ) -> icu_experimental::displaynames::DisplayNamesOptions { 197 let mut options = icu_experimental::displaynames::DisplayNamesOptions::default(); 198 options.style = other.style.into_converted_option(); 199 options.fallback = other 200 .fallback 201 .into_converted_option() 202 .unwrap_or(options.fallback); 203 options.language_display = other 204 .language_display 205 .into_converted_option() 206 .unwrap_or(options.language_display); 207 options 208 } 209 }