summaryrefslogtreecommitdiffstats
path: root/intl/icu_capi/src/displaynames.rs
blob: eec14ed83937f00732836e6a14a91bbbcc099dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

#[diplomat::bridge]
pub mod ffi {
    use crate::errors::ffi::ICU4XError;
    use crate::locale::ffi::ICU4XLocale;
    use crate::provider::ffi::ICU4XDataProvider;
    use alloc::boxed::Box;
    use diplomat_runtime::DiplomatWriteable;
    #[allow(unused_imports)] // feature-specific
    use icu_displaynames::{DisplayNamesOptions, Fallback, LanguageDisplay};
    use icu_displaynames::{LocaleDisplayNamesFormatter, RegionDisplayNames};
    use icu_locid::subtags::Region;
    use writeable::Writeable;

    //  FFI version of `LocaleDisplayNamesFormatter`.
    #[diplomat::opaque]
    #[diplomat::rust_link(icu::displaynames::LocaleDisplayNamesFormatter, Struct)]
    pub struct ICU4XLocaleDisplayNamesFormatter(pub LocaleDisplayNamesFormatter);

    //  FFI version of `RegionDisplayNames`.
    #[diplomat::opaque]
    #[diplomat::rust_link(icu::displaynames::RegionDisplayNames, Struct)]
    pub struct ICU4XRegionDisplayNames(pub RegionDisplayNames);

    //  FFI version of `DisplayNamesOptions`.
    #[diplomat::rust_link(icu::displaynames::options::DisplayNamesOptions, Struct)]
    pub struct ICU4XDisplayNamesOptionsV1 {
        /// The optional formatting style to use for display name.
        pub style: ICU4XDisplayNamesStyle,
        /// The fallback return when the system does not have the
        /// requested display name, defaults to "code".
        pub fallback: ICU4XDisplayNamesFallback,
        /// The language display kind, defaults to "dialect".
        pub language_display: ICU4XLanguageDisplay,
    }

    // FFI version of `Style` option for the display names.
    #[diplomat::rust_link(icu::displaynames::options::Style, Enum)]
    pub enum ICU4XDisplayNamesStyle {
        Auto,
        Narrow,
        Short,
        Long,
        Menu,
    }

    // FFI version of `Fallback` option for the display names.
    #[diplomat::rust_link(icu::displaynames::options::Fallback, Enum)]
    #[diplomat::enum_convert(Fallback, needs_wildcard)]
    pub enum ICU4XDisplayNamesFallback {
        Code,
        None,
    }

    // FFI version of `LanguageDisplay`.
    #[diplomat::rust_link(icu::displaynames::options::LanguageDisplay, Enum)]
    #[diplomat::enum_convert(LanguageDisplay, needs_wildcard)]
    pub enum ICU4XLanguageDisplay {
        Dialect,
        Standard,
    }

    impl ICU4XLocaleDisplayNamesFormatter {
        /// Creates a new `LocaleDisplayNamesFormatter` from locale data and an options bag.
        #[diplomat::rust_link(icu::displaynames::LocaleDisplayNamesFormatter::try_new, FnInStruct)]
        pub fn create(
            provider: &ICU4XDataProvider,
            locale: &ICU4XLocale,
            options: ICU4XDisplayNamesOptionsV1,
        ) -> Result<Box<ICU4XLocaleDisplayNamesFormatter>, ICU4XError> {
            let locale = locale.to_datalocale();
            let options = DisplayNamesOptions::from(options);

            Ok(Box::new(ICU4XLocaleDisplayNamesFormatter(
                call_constructor!(
                    LocaleDisplayNamesFormatter::try_new,
                    LocaleDisplayNamesFormatter::try_new_with_any_provider,
                    LocaleDisplayNamesFormatter::try_new_with_buffer_provider,
                    provider,
                    &locale,
                    options,
                )?,
            )))
        }

        /// Returns the locale-specific display name of a locale.
        #[diplomat::rust_link(icu::displaynames::LocaleDisplayNamesFormatter::of, FnInStruct)]
        pub fn of(
            &self,
            locale: &ICU4XLocale,
            write: &mut DiplomatWriteable,
        ) -> Result<(), ICU4XError> {
            self.0.of(&locale.0).write_to(write)?;
            Ok(())
        }
    }

    impl ICU4XRegionDisplayNames {
        /// Creates a new `RegionDisplayNames` from locale data and an options bag.
        #[diplomat::rust_link(icu::displaynames::RegionDisplayNames::try_new, FnInStruct)]
        pub fn create(
            provider: &ICU4XDataProvider,
            locale: &ICU4XLocale,
        ) -> Result<Box<ICU4XRegionDisplayNames>, ICU4XError> {
            let locale = locale.to_datalocale();
            Ok(Box::new(ICU4XRegionDisplayNames(call_constructor!(
                RegionDisplayNames::try_new,
                RegionDisplayNames::try_new_with_any_provider,
                RegionDisplayNames::try_new_with_buffer_provider,
                provider,
                &locale,
                Default::default()
            )?)))
        }

        /// Returns the locale specific display name of a region.
        /// Note that the funtion returns an empty string in case the display name for a given
        /// region code is not found.
        #[diplomat::rust_link(icu::displaynames::RegionDisplayNames::of, FnInStruct)]
        pub fn of(&self, region: &str, write: &mut DiplomatWriteable) -> Result<(), ICU4XError> {
            self.0
                .of(region.parse::<Region>()?)
                .unwrap_or("")
                .write_to(write)?;
            Ok(())
        }
    }
}

#[allow(unused_imports)] // feature-specific
use icu_displaynames::{DisplayNamesOptions, Fallback, LanguageDisplay, Style};

impl From<ffi::ICU4XDisplayNamesStyle> for Option<Style> {
    fn from(style: ffi::ICU4XDisplayNamesStyle) -> Option<Style> {
        match style {
            ffi::ICU4XDisplayNamesStyle::Auto => None,
            ffi::ICU4XDisplayNamesStyle::Narrow => Some(Style::Narrow),
            ffi::ICU4XDisplayNamesStyle::Short => Some(Style::Short),
            ffi::ICU4XDisplayNamesStyle::Long => Some(Style::Long),
            ffi::ICU4XDisplayNamesStyle::Menu => Some(Style::Menu),
        }
    }
}

impl From<ffi::ICU4XDisplayNamesOptionsV1> for DisplayNamesOptions {
    fn from(other: ffi::ICU4XDisplayNamesOptionsV1) -> DisplayNamesOptions {
        let mut options = DisplayNamesOptions::default();
        options.style = other.style.into();
        options.fallback = other.fallback.into();
        options.language_display = other.language_display.into();
        options
    }
}