]>
Commit | Line | Data |
---|---|---|
487cf647 FG |
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 | //! Formatting lists in a locale-sensitive way. | |
6 | //! | |
7 | //! This module is published as its own crate ([`icu_list`](https://docs.rs/icu_list/latest/icu_list/)) | |
8 | //! and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project. | |
9 | //! | |
10 | //! # Examples | |
11 | //! | |
12 | //! ## Formatting *and* lists in Spanish | |
13 | //! | |
14 | //! ``` | |
15 | //! # use icu::list::{ListFormatter, ListLength}; | |
16 | //! # use icu::locid::locale; | |
17 | //! # use writeable::*; | |
18 | //! # | |
19 | //! let list_formatter = ListFormatter::try_new_and_with_length_unstable( | |
20 | //! &icu_testdata::unstable(), | |
21 | //! &locale!("es").into(), | |
22 | //! ListLength::Wide, | |
23 | //! ) | |
24 | //! .expect("Data should load successfully"); | |
25 | //! | |
26 | //! assert_writeable_eq!( | |
27 | //! list_formatter.format(["España", "Suiza"].iter()), | |
28 | //! "España y Suiza", | |
29 | //! ); | |
30 | //! | |
31 | //! // The Spanish 'y' sometimes becomes an 'e': | |
32 | //! assert_writeable_eq!( | |
33 | //! list_formatter.format(["España", "Suiza", "Italia"].iter()), | |
34 | //! "España, Suiza e Italia", | |
35 | //! ); | |
36 | //! ``` | |
37 | //! | |
38 | //! ## Formatting *or* lists in Thai | |
39 | //! | |
40 | //! ``` | |
41 | //! # use icu::list::{ListFormatter, ListLength}; | |
42 | //! # use icu::locid::locale; | |
43 | //! # use writeable::*; | |
44 | //! # | |
45 | //! let list_formatter = ListFormatter::try_new_or_with_length_unstable( | |
46 | //! &icu_testdata::unstable(), | |
47 | //! &locale!("th").into(), | |
48 | //! ListLength::Short, | |
49 | //! ) | |
50 | //! .expect("Data should load successfully"); | |
51 | //! | |
52 | //! // We can use any Writeables as inputs | |
53 | //! assert_writeable_eq!(list_formatter.format(1..=3), "1, 2 หรือ 3",); | |
54 | //! ``` | |
55 | //! | |
56 | //! ## Formatting unit lists in English | |
57 | //! | |
58 | //! ``` | |
59 | //! # use icu::list::{ListFormatter, ListLength}; | |
60 | //! # use icu::locid::locale; | |
61 | //! # use writeable::*; | |
62 | //! # | |
63 | //! let list_formatter = ListFormatter::try_new_unit_with_length_unstable( | |
64 | //! &icu_testdata::unstable(), | |
65 | //! &locale!("en").into(), | |
66 | //! ListLength::Wide, | |
67 | //! ) | |
68 | //! .expect("Data should load successfully"); | |
69 | //! | |
70 | //! assert_writeable_eq!( | |
71 | //! list_formatter.format(["1ft", "2in"].iter()), | |
72 | //! "1ft, 2in", | |
73 | //! ); | |
74 | //! ``` | |
75 | //! Note: this last example is not fully internationalized. See [icu4x/2192](https://github.com/unicode-org/icu4x/issues/2192) | |
76 | //! for full unit handling. | |
77 | ||
78 | #![cfg_attr(not(feature = "std"), no_std)] | |
79 | #![cfg_attr( | |
80 | not(test), | |
81 | deny( | |
82 | clippy::indexing_slicing, | |
83 | clippy::unwrap_used, | |
84 | clippy::expect_used, | |
85 | clippy::panic, | |
86 | clippy::exhaustive_structs, | |
87 | clippy::exhaustive_enums, | |
88 | // TODO(#2266): enable missing_debug_implementations, | |
89 | ) | |
90 | )] | |
91 | #![warn(missing_docs)] | |
92 | ||
93 | extern crate alloc; | |
94 | ||
95 | mod error; | |
96 | mod list_formatter; | |
97 | mod string_matcher; | |
98 | ||
99 | pub mod provider; | |
100 | ||
101 | pub use list_formatter::*; | |
102 | ||
103 | pub use error::ListError; | |
104 | ||
105 | #[doc(inline)] | |
106 | pub use ListError as Error; | |
107 | ||
108 | /// Represents the style of a list. See the | |
109 | /// [CLDR spec](https://unicode.org/reports/tr35/tr35-general.html#ListPatterns) | |
110 | /// for an explanation of the different styles. | |
111 | #[derive(Copy, Clone, PartialEq, Debug)] | |
112 | #[non_exhaustive] | |
113 | pub enum ListLength { | |
114 | /// A typical list | |
115 | Wide, | |
116 | /// A shorter list | |
117 | Short, | |
118 | /// The shortest type of list | |
119 | Narrow, | |
120 | // *Important*: When adding a variant here, make sure the code in | |
121 | // ListFormatterPatterns::{start, middle, end, pair} stays panic-free! | |
122 | } |