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;
58#[cfg(all(feature = "zerovec", feature = "alloc"))]
59mod implementations;
60mod multi_named;
61#[cfg(feature = "alloc")]
62mod parser;
63mod single;
64
65pub use common::PatternBackend;
66pub use common::PatternItem;
67#[cfg(feature = "alloc")]
68pub use common::PatternItemCow;
69pub use common::PlaceholderValueProvider;
70pub use double::DoublePlaceholder;
71pub use double::DoublePlaceholderKey;
72pub use error::PatternError;
73#[cfg(feature = "serde")]
74pub use frontend::serde::*;
75pub use frontend::Pattern;
76pub use multi_named::MissingNamedPlaceholderError;
77pub use multi_named::MultiNamedPlaceholder;
78pub use multi_named::MultiNamedPlaceholderKey;
79#[cfg(feature = "alloc")]
80pub use parser::ParsedPatternItem;
81#[cfg(feature = "alloc")]
82pub use parser::Parser;
83#[cfg(feature = "alloc")]
84pub use parser::ParserError;
85#[cfg(feature = "alloc")]
86pub use parser::ParserOptions;
87#[cfg(feature = "alloc")]
88pub use parser::QuoteMode;
89pub use single::SinglePlaceholder;
90pub use single::SinglePlaceholderKey;
91#[doc(no_inline)]
92pub use PatternError as Error;
93
94mod private {
95    pub trait Sealed {}
96}
97
98/// # Examples
99///
100/// ```
101/// use core::str::FromStr;
102/// use icu_pattern::SinglePlaceholderPattern;
103/// use writeable::assert_writeable_eq;
104///
105/// // Create a pattern from the string syntax:
106/// let pattern = SinglePlaceholderPattern::try_from_str(
107///     "Hello, {0}!",
108///     Default::default(),
109/// )
110/// .unwrap();
111///
112/// // Interpolate some values into the pattern:
113/// assert_writeable_eq!(pattern.interpolate(["Alice"]), "Hello, Alice!");
114/// ```
115pub type SinglePlaceholderPattern = Pattern<SinglePlaceholder>;
116
117/// # Examples
118///
119/// ```
120/// use core::str::FromStr;
121/// use icu_pattern::DoublePlaceholderPattern;
122/// use writeable::assert_writeable_eq;
123///
124/// // Create a pattern from the string syntax:
125/// let pattern = DoublePlaceholderPattern::try_from_str(
126///     "Hello, {0} and {1}!",
127///     Default::default(),
128/// )
129/// .unwrap();
130///
131/// // Interpolate some values into the pattern:
132/// assert_writeable_eq!(
133///     pattern.interpolate(["Alice", "Bob"]),
134///     "Hello, Alice and Bob!"
135/// );
136/// ```
137pub type DoublePlaceholderPattern = Pattern<DoublePlaceholder>;
138
139/// # Examples
140///
141/// ```
142/// use core::str::FromStr;
143/// use icu_pattern::MultiNamedPlaceholderPattern;
144/// use std::collections::BTreeMap;
145/// use writeable::assert_try_writeable_eq;
146///
147/// // Create a pattern from the string syntax:
148/// let pattern = MultiNamedPlaceholderPattern::try_from_str(
149///     "Hello, {person0} and {person1}!",
150///     Default::default(),
151/// )
152/// .unwrap();
153///
154/// // Interpolate some values into the pattern:
155/// assert_try_writeable_eq!(
156///     pattern.try_interpolate(
157///         [("person0", "Alice"), ("person1", "Bob")]
158///             .into_iter()
159///             .collect::<BTreeMap<&str, &str>>()
160///     ),
161///     "Hello, Alice and Bob!"
162/// );
163/// ```
164pub type MultiNamedPlaceholderPattern = Pattern<MultiNamedPlaceholder>;
165
166#[test]
167#[cfg(feature = "alloc")]
168fn test_single_placeholder_pattern_impls() {
169    let a = SinglePlaceholderPattern::try_from_str("{0}", Default::default()).unwrap();
170    let b = SinglePlaceholderPattern::try_from_str("{0}", Default::default()).unwrap();
171    assert_eq!(a, b);
172    let c = b.clone();
173    assert_eq!(a, c);
174}