Debian Codegraph @

debian/rust-vendor/icu_locale/src/names/mod.rs

raw
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 //! Types representing the name of a language, script, region, or variant.
6 //!
7 //! See `icu_experimental::displaynames` for another display names API designed for loading
8 //! multiple names at once, such as when populating a dropdown menu. Please send feedback on
9 //! your use cases.
10 //!
11 //! Display names for full locale identifiers are not currently supported.
12 //! If you have any feedback, please let us know at
13 //! <https://github.com/unicode-org/icu4x/issues/7825>.
14 //!
15 //! <div class="stab unstable">
16 //! 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
17 //! including in SemVer minor releases. Do not use this type unless you are prepared for things to occasionally break.
18 //!
19 //! Graduation tracking issue: [issue #3913](https://github.com/unicode-org/icu4x/issues/3913).
20 //! </div>
21
22 mod language;
23 mod region;
24 mod script;
25 mod variant;
26
27 // Re-export from submodules
28 pub use language::{
29 LanguageIdentifierDisplayName, LanguageIdentifierDisplayNameBorrowed,
30 LanguageIdentifierNameFallbackError,
31 };
32 pub use region::{RegionDisplayName, RegionDisplayNameBorrowed};
33 pub use script::{ScriptDisplayName, ScriptDisplayNameBorrowed};
34 pub use variant::{VariantDisplayName, VariantDisplayNameBorrowed};
35
36 use icu_locale_core::preferences::define_preferences;
37 use icu_provider::prelude::*;
38
39 define_preferences!(
40 /// The preferences for display names.
41 [Copy]
42 DisplayNamesPreferences,
43 {}
44 );
45
46 /// A bag of options defining how a language identifier display name will be formatted.
47 #[derive(Copy, Debug, Eq, PartialEq, Clone, Default)]
48 #[non_exhaustive]
49 pub struct LanguageIdentifierDisplayNameOptions {
50 /// The language display kind, defaults to "dialect".
51 pub language_display: Option<LanguageDisplay>,
52 }
53
54 impl LanguageIdentifierDisplayNameOptions {
55 pub(crate) fn should_load_dialect(self) -> bool {
56 self.language_display.unwrap_or_default() == LanguageDisplay::Dialect
57 }
58 }
59
60 /// An enum for language display style.
61 #[non_exhaustive]
62 #[derive(Debug, Eq, PartialEq, Clone, Copy, Default)]
63 pub enum LanguageDisplay {
64 /// Dialect display mode (default).
65 #[default]
66 Dialect,
67 /// Standard display mode.
68 Standard,
69 }
70
71 fn load_one<M0, M1, P>(
72 provider: &P,
73 locale: &DataLocale,
74 attrs: &DataMarkerAttributes,
75 ) -> Result<Option<DataPayload<M1>>, DataError>
76 where
77 M0: DataMarker,
78 M1: DynamicDataMarker<DataStruct = M0::DataStruct>,
79 P: DataProvider<M0> + ?Sized,
80 {
81 let id = DataIdentifierBorrowed::for_marker_attributes_and_locale(attrs, locale);
82 let mut metadata = DataRequestMetadata::default();
83 metadata.silent = true;
84 let response = provider
85 .load(DataRequest { id, metadata })
86 .allow_identifier_not_found()?;
87 Ok(response.map(|r| r.payload.cast()))
88 }
89
90 macro_rules! impl_writeable_for_single_display_name_borrowed {
91 ($borrowed:ident) => {
92 impl<'a> writeable::Writeable for $borrowed<'a> {
93 #[inline]
94 fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
95 sink.write_str(self.value)
96 }
97
98 #[inline]
99 fn writeable_length_hint(&self) -> writeable::LengthHint {
100 writeable::LengthHint::exact(self.value.len())
101 }
102
103 #[inline]
104 fn writeable_borrow(&self) -> Option<&str> {
105 Some(self.value)
106 }
107 }
108
109 writeable::impl_display_with_writeable!($borrowed<'_>);
110 };
111 }
112
113 macro_rules! impl_writeable_for_single_display_name_owned {
114 ($owned:ident) => {
115 impl writeable::Writeable for $owned {
116 #[inline]
117 fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
118 self.as_borrowed().write_to(sink)
119 }
120
121 #[inline]
122 fn write_to_parts<S: writeable::PartsWrite + ?Sized>(
123 &self,
124 sink: &mut S,
125 ) -> core::fmt::Result {
126 self.as_borrowed().write_to_parts(sink)
127 }
128
129 #[inline]
130 fn writeable_length_hint(&self) -> writeable::LengthHint {
131 self.as_borrowed().writeable_length_hint()
132 }
133
134 #[inline]
135 fn writeable_borrow(&self) -> Option<&str> {
136 Some(self.borrow_str())
137 }
138 }
139
140 writeable::impl_display_with_writeable!($owned);
141 };
142 }
143
144 pub(crate) use impl_writeable_for_single_display_name_borrowed;
145 pub(crate) use impl_writeable_for_single_display_name_owned;
146
147 #[cfg(test)]
148 pub(crate) fn format_table_row<S: core::fmt::Display, E1, E2>(
149 name: &str,
150 items: impl IntoIterator<Item = Result<Result<S, E1>, E2>>,
151 ) -> String {
152 let row = items
153 .into_iter()
154 .map(|item| match item {
155 Ok(Ok(s)) => format!("\"{s}\""),
156 _ => "❌".to_string(),
157 })
158 .collect::<Vec<_>>()
159 .join(" | ");
160 format!("| [`{name}`](Self::{name}) | {row} |")
161 }