| 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 |
pub mod ffi { |
| 7 |
use crate::errors::ffi::ICU4XError; |
| 8 |
use crate::locale::ffi::ICU4XLocale; |
| 9 |
use crate::provider::ffi::ICU4XDataProvider; |
| 10 |
use alloc::boxed::Box; |
| 11 |
#[allow(unused_imports)] // feature-specific |
| 12 |
use icu_experimental::displaynames::{DisplayNamesOptions, Fallback, LanguageDisplay}; |
| 13 |
use icu_experimental::displaynames::{LocaleDisplayNamesFormatter, RegionDisplayNames}; |
| 14 |
use icu_locid::subtags::Region; |
| 15 |
use writeable::Writeable; |
| 16 |
|
| 17 |
#[diplomat::opaque] |
| 18 |
#[diplomat::rust_link(icu::displaynames::LocaleDisplayNamesFormatter, Struct)] |
| 19 |
pub struct ICU4XLocaleDisplayNamesFormatter(pub LocaleDisplayNamesFormatter); |
| 20 |
|
| 21 |
#[diplomat::opaque] |
| 22 |
#[diplomat::rust_link(icu::displaynames::RegionDisplayNames, Struct)] |
| 23 |
pub struct ICU4XRegionDisplayNames(pub RegionDisplayNames); |
| 24 |
|
| 25 |
#[diplomat::rust_link(icu::displaynames::options::DisplayNamesOptions, Struct)] |
| 26 |
#[diplomat::attr(dart, rename = "DisplayNamesOptions")] |
| 27 |
pub struct ICU4XDisplayNamesOptionsV1 { |
| 28 |
/// The optional formatting style to use for display name. |
| 29 |
pub style: ICU4XDisplayNamesStyle, |
| 30 |
/// The fallback return when the system does not have the |
| 31 |
/// requested display name, defaults to "code". |
| 32 |
pub fallback: ICU4XDisplayNamesFallback, |
| 33 |
/// The language display kind, defaults to "dialect". |
| 34 |
pub language_display: ICU4XLanguageDisplay, |
| 35 |
} |
| 36 |
|
| 37 |
#[diplomat::rust_link(icu::displaynames::options::Style, Enum)] |
| 38 |
pub enum ICU4XDisplayNamesStyle { |
| 39 |
Auto, |
| 40 |
Narrow, |
| 41 |
Short, |
| 42 |
Long, |
| 43 |
Menu, |
| 44 |
} |
| 45 |
|
| 46 |
#[diplomat::rust_link(icu::displaynames::options::Fallback, Enum)] |
| 47 |
#[diplomat::enum_convert(Fallback, needs_wildcard)] |
| 48 |
pub enum ICU4XDisplayNamesFallback { |
| 49 |
Code, |
| 50 |
None, |
| 51 |
} |
| 52 |
|
| 53 |
#[diplomat::rust_link(icu::displaynames::options::LanguageDisplay, Enum)] |
| 54 |
#[diplomat::enum_convert(LanguageDisplay, needs_wildcard)] |
| 55 |
pub enum ICU4XLanguageDisplay { |
| 56 |
Dialect, |
| 57 |
Standard, |
| 58 |
} |
| 59 |
|
| 60 |
impl ICU4XLocaleDisplayNamesFormatter { |
| 61 |
/// Creates a new `LocaleDisplayNamesFormatter` from locale data and an options bag. |
| 62 |
#[diplomat::rust_link(icu::displaynames::LocaleDisplayNamesFormatter::try_new, FnInStruct)] |
| 63 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
| 64 |
pub fn create( |
| 65 |
provider: &ICU4XDataProvider, |
| 66 |
locale: &ICU4XLocale, |
| 67 |
options: ICU4XDisplayNamesOptionsV1, |
| 68 |
) -> Result<Box<ICU4XLocaleDisplayNamesFormatter>, ICU4XError> { |
| 69 |
let locale = locale.to_datalocale(); |
| 70 |
let options = DisplayNamesOptions::from(options); |
| 71 |
|
| 72 |
Ok(Box::new(ICU4XLocaleDisplayNamesFormatter( |
| 73 |
call_constructor!( |
| 74 |
LocaleDisplayNamesFormatter::try_new, |
| 75 |
LocaleDisplayNamesFormatter::try_new_with_any_provider, |
| 76 |
LocaleDisplayNamesFormatter::try_new_with_buffer_provider, |
| 77 |
provider, |
| 78 |
&locale, |
| 79 |
options, |
| 80 |
)?, |
| 81 |
))) |
| 82 |
} |
| 83 |
|
| 84 |
/// Returns the locale-specific display name of a locale. |
| 85 |
#[diplomat::rust_link(icu::displaynames::LocaleDisplayNamesFormatter::of, FnInStruct)] |
| 86 |
pub fn of( |
| 87 |
&self, |
| 88 |
locale: &ICU4XLocale, |
| 89 |
write: &mut DiplomatWriteable, |
| 90 |
) -> Result<(), ICU4XError> { |
| 91 |
self.0.of(&locale.0).write_to(write)?; |
| 92 |
Ok(()) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
impl ICU4XRegionDisplayNames { |
| 97 |
/// Creates a new `RegionDisplayNames` from locale data and an options bag. |
| 98 |
#[diplomat::rust_link(icu::displaynames::RegionDisplayNames::try_new, FnInStruct)] |
| 99 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
| 100 |
pub fn create( |
| 101 |
provider: &ICU4XDataProvider, |
| 102 |
locale: &ICU4XLocale, |
| 103 |
) -> Result<Box<ICU4XRegionDisplayNames>, ICU4XError> { |
| 104 |
let locale = locale.to_datalocale(); |
| 105 |
Ok(Box::new(ICU4XRegionDisplayNames(call_constructor!( |
| 106 |
RegionDisplayNames::try_new, |
| 107 |
RegionDisplayNames::try_new_with_any_provider, |
| 108 |
RegionDisplayNames::try_new_with_buffer_provider, |
| 109 |
provider, |
| 110 |
&locale, |
| 111 |
Default::default() |
| 112 |
)?))) |
| 113 |
} |
| 114 |
|
| 115 |
/// Returns the locale specific display name of a region. |
| 116 |
/// Note that the function returns an empty string in case the display name for a given |
| 117 |
/// region code is not found. |
| 118 |
#[diplomat::rust_link(icu::displaynames::RegionDisplayNames::of, FnInStruct)] |
| 119 |
pub fn of( |
| 120 |
&self, |
| 121 |
region: &DiplomatStr, |
| 122 |
write: &mut DiplomatWriteable, |
| 123 |
) -> Result<(), ICU4XError> { |
| 124 |
self.0 |
| 125 |
.of(Region::try_from_bytes(region)?) |
| 126 |
.unwrap_or("") |
| 127 |
.write_to(write)?; |
| 128 |
Ok(()) |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
#[allow(unused_imports)] // feature-specific |
| 134 |
use icu_experimental::displaynames::{DisplayNamesOptions, Fallback, LanguageDisplay, Style}; |
| 135 |
|
| 136 |
impl From<ffi::ICU4XDisplayNamesStyle> for Option<Style> { |
| 137 |
fn from(style: ffi::ICU4XDisplayNamesStyle) -> Option<Style> { |
| 138 |
match style { |
| 139 |
ffi::ICU4XDisplayNamesStyle::Auto => None, |
| 140 |
ffi::ICU4XDisplayNamesStyle::Narrow => Some(Style::Narrow), |
| 141 |
ffi::ICU4XDisplayNamesStyle::Short => Some(Style::Short), |
| 142 |
ffi::ICU4XDisplayNamesStyle::Long => Some(Style::Long), |
| 143 |
ffi::ICU4XDisplayNamesStyle::Menu => Some(Style::Menu), |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
impl From<ffi::ICU4XDisplayNamesOptionsV1> for DisplayNamesOptions { |
| 149 |
fn from(other: ffi::ICU4XDisplayNamesOptionsV1) -> DisplayNamesOptions { |
| 150 |
let mut options = DisplayNamesOptions::default(); |
| 151 |
options.style = other.style.into(); |
| 152 |
options.fallback = other.fallback.into(); |
| 153 |
options.language_display = other.language_display.into(); |
| 154 |
options |
| 155 |
} |
| 156 |
} |