icu_pattern/lib.rs
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//! `icu_pattern` is a utility crate of the [`ICU4X`] project.
6//!
7//! It includes a [`Pattern`] type which supports patterns with various storage backends.
8//!
9//! The types are tightly coupled with the [`writeable`] crate.
10//!
11//! # Examples
12//!
13//! Parsing and interpolating with a single-placeholder pattern:
14//!
15//! ```
16//! use icu_pattern::SinglePlaceholderPattern;
17//! use writeable::assert_writeable_eq;
18//!
19//! // Parse a pattern string:
20//! let pattern = SinglePlaceholderPattern::try_from_str(
21//! "Hello, {0}!",
22//! Default::default(),
23//! )
24//! .unwrap();
25//!
26//! // Interpolate into the pattern string:
27//! assert_writeable_eq!(pattern.interpolate(["World"]), "Hello, World!");
28//! ```
29//!
30//! [`ICU4X`]: ../icu/index.html
31//! [`FromStr`]: core::str::FromStr
32
33// https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
34#![cfg_attr(not(any(test, doc)), no_std)]
35#![cfg_attr(
36 not(test),
37 deny(
38 clippy::indexing_slicing,
39 clippy::unwrap_used,
40 clippy::expect_used,
41 clippy::panic,
42 clippy::exhaustive_structs,
43 clippy::exhaustive_enums,
44 clippy::trivially_copy_pass_by_ref,
45 missing_debug_implementations,
46 )
47)]
48
49#[cfg(feature = "alloc")]
50extern crate alloc;
51
52#[cfg(feature = "alloc")]
53mod builder;
54mod common;
55mod double;
56mod error;
57mod frontend;
58mod multi_named;
59#[cfg(feature = "alloc")]
60mod parser;
61mod single;
62
63pub use common::PatternBackend;
64pub use common::PatternItem;
65#[cfg(feature = "alloc")]
66pub use common::PatternItemCow;
67pub use common::PlaceholderValueProvider;
68pub use double::DoublePlaceholder;
69pub use double::DoublePlaceholderKey;
70pub use error::PatternError;
71#[cfg(feature = "serde")]
72pub use frontend::serde::*;
73pub use frontend::Pattern;
74pub use multi_named::MissingNamedPlaceholderError;
75pub use multi_named::MultiNamedPlaceholder;
76pub use multi_named::MultiNamedPlaceholderKey;
77#[cfg(feature = "alloc")]
78pub use parser::ParsedPatternItem;
79#[cfg(feature = "alloc")]
80pub use parser::Parser;
81#[cfg(feature = "alloc")]
82pub use parser::ParserError;
83#[cfg(feature = "alloc")]
84pub use parser::ParserOptions;
85#[cfg(feature = "alloc")]
86pub use parser::QuoteMode;
87pub use single::SinglePlaceholder;
88pub use single::SinglePlaceholderKey;
89#[doc(no_inline)]
90pub use PatternError as Error;
91
92mod private {
93 pub trait Sealed {}
94}
95
96/// # Examples
97///
98/// ```
99/// use core::str::FromStr;
100/// use icu_pattern::SinglePlaceholderPattern;
101/// use writeable::assert_writeable_eq;
102///
103/// // Create a pattern from the string syntax:
104/// let pattern = SinglePlaceholderPattern::try_from_str(
105/// "Hello, {0}!",
106/// Default::default(),
107/// )
108/// .unwrap();
109///
110/// // Interpolate some values into the pattern:
111/// assert_writeable_eq!(pattern.interpolate(["Alice"]), "Hello, Alice!");
112/// ```
113pub type SinglePlaceholderPattern = Pattern<SinglePlaceholder>;
114
115/// # Examples
116///
117/// ```
118/// use core::str::FromStr;
119/// use icu_pattern::DoublePlaceholderPattern;
120/// use writeable::assert_writeable_eq;
121///
122/// // Create a pattern from the string syntax:
123/// let pattern = DoublePlaceholderPattern::try_from_str(
124/// "Hello, {0} and {1}!",
125/// Default::default(),
126/// )
127/// .unwrap();
128///
129/// // Interpolate some values into the pattern:
130/// assert_writeable_eq!(
131/// pattern.interpolate(["Alice", "Bob"]),
132/// "Hello, Alice and Bob!"
133/// );
134/// ```
135pub type DoublePlaceholderPattern = Pattern<DoublePlaceholder>;
136
137/// # Examples
138///
139/// ```
140/// use core::str::FromStr;
141/// use icu_pattern::MultiNamedPlaceholderPattern;
142/// use std::collections::BTreeMap;
143/// use writeable::assert_try_writeable_eq;
144///
145/// // Create a pattern from the string syntax:
146/// let pattern = MultiNamedPlaceholderPattern::try_from_str(
147/// "Hello, {person0} and {person1}!",
148/// Default::default(),
149/// )
150/// .unwrap();
151///
152/// // Interpolate some values into the pattern:
153/// assert_try_writeable_eq!(
154/// pattern.try_interpolate(
155/// [("person0", "Alice"), ("person1", "Bob")]
156/// .into_iter()
157/// .collect::<BTreeMap<&str, &str>>()
158/// ),
159/// "Hello, Alice and Bob!"
160/// );
161/// ```
162pub type MultiNamedPlaceholderPattern = Pattern<MultiNamedPlaceholder>;
163
164#[test]
165#[cfg(feature = "alloc")]
166fn test_single_placeholder_pattern_impls() {
167 let a = SinglePlaceholderPattern::try_from_str("{0}", Default::default()).unwrap();
168 let b = SinglePlaceholderPattern::try_from_str("{0}", Default::default()).unwrap();
169 assert_eq!(a, b);
170 let c = b.clone();
171 assert_eq!(a, c);
172}