ecma402_traits/
displaynames.rs

1// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt;
16
17/// Contains the API configuration as prescribed by [ECMA 402][ecma].
18///
19///    [ecma]: https://www.ecma-international.org/publications/standards/Ecma-402.htm
20///
21/// The meaning of the options is the same as in the similarly named
22/// options in the JS version.
23///
24/// See [Options] for the contents of the options.  See the [DisplayNames::try_new] for the use of
25/// the options.
26pub mod options {
27
28    /// The formatting style to use
29    #[derive(Eq, PartialEq, Debug, Clone)]
30    pub enum Style {
31        /// Example: "US".
32        Narrow,
33        /// Example: "USA".
34        Short,
35        /// Default.  Example: "United States".
36        Long,
37    }
38
39    /// The type of the spellout.  Spelled in the language of the
40    /// requested locale.
41    #[derive(Eq, PartialEq, Debug, Clone)]
42    pub enum Type {
43        /// The name of the language, e.g. "US English"
44        Language,
45        /// The name of the region, e.g. "United States".
46        Region,
47        /// The name of the script, e.g. "Latin",
48        Script,
49        /// The name of the currency, e.g. "US Dollar",
50        Currency,
51    }
52
53    /// The fallback to use.
54    #[derive(Eq, PartialEq, Debug, Clone)]
55    pub enum Fallback {
56        /// Default: the fallback is the region code, e.g. "us"
57        Code,
58        /// No fallback.
59        None,
60    }
61}
62
63/// The options set by the user at construction time.  Provides as a "bag of options" since we
64/// don't expect any implementations to be attached to this struct.
65///
66/// The default values of all the options are prescribed by the TC39 report.
67pub struct Options {
68    /// The formatting style to use.
69    pub style: options::Style,
70    /// The type of information to format.
71    pub in_type: options::Type,
72    /// Sets what to do if the information is not availabe.
73    pub fallback: options::Fallback,
74}
75
76impl Default for Options {
77    fn default() -> Options {
78        Options {
79            style: options::Style::Long,
80            in_type: options::Type::Region,
81            fallback: options::Fallback::Code,
82        }
83    }
84}
85
86/// Displays a region, language, script or currency using the language of
87/// a specific locale.
88pub trait DisplayNames {
89    /// The type of error reported, if any.
90    type Error: std::error::Error;
91
92    /// Creates a new [DisplayNames].
93    ///
94    /// Creation may fail, for example, if the locale-specific data is not loaded, or if
95    /// the supplied options are inconsistent.
96    fn try_new<L>(l: L, opts: Options) -> Result<Self, Self::Error>
97    where
98        L: crate::Locale,
99        Self: Sized;
100
101    /// Formats the information about the given locale in the language used by
102    /// this [DisplayNames].
103    ///
104    /// The function implements [`Intl.DisplayNames`][nfmt] from [ECMA 402][ecma].
105    ///
106    ///    [nfmt]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames
107    ///    [ecma]: https://www.ecma-international.org/publications/standards/Ecma-402.htm
108    fn format<W, L>(&self, locale: L, writer: &mut W) -> fmt::Result
109    where
110        W: fmt::Write,
111        L: crate::Locale;
112}