| 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 |
use alloc::borrow::Cow; |
| 6 |
|
| 7 |
/// A [`PatternItem`] with additional detail returned by the [`Parser`]. |
| 8 |
/// |
| 9 |
/// ✨ *Enabled with the `alloc` Cargo feature.* |
| 10 |
/// |
| 11 |
/// # Examples |
| 12 |
/// |
| 13 |
/// ``` |
| 14 |
/// use icu_pattern::{ParsedPatternItem, Parser, ParserOptions}; |
| 15 |
/// |
| 16 |
/// let input = "{0}, {1}"; |
| 17 |
/// |
| 18 |
/// let mut parser = Parser::new(input, ParserOptions::default()); |
| 19 |
/// |
| 20 |
/// let mut result = vec![]; |
| 21 |
/// |
| 22 |
/// while let Some(element) = |
| 23 |
/// parser.try_next().expect("Failed to advance iterator") |
| 24 |
/// { |
| 25 |
/// result.push(element); |
| 26 |
/// } |
| 27 |
/// |
| 28 |
/// assert_eq!( |
| 29 |
/// result, |
| 30 |
/// &[ |
| 31 |
/// ParsedPatternItem::Placeholder(0), |
| 32 |
/// ParsedPatternItem::Literal { |
| 33 |
/// content: ", ".into(), |
| 34 |
/// quoted: false |
| 35 |
/// }, |
| 36 |
/// ParsedPatternItem::Placeholder(1), |
| 37 |
/// ] |
| 38 |
/// ); |
| 39 |
/// ``` |
| 40 |
/// |
| 41 |
/// # Type parameters |
| 42 |
/// |
| 43 |
/// - `P`: A placeholder type which implements [`FromStr`]. |
| 44 |
/// |
| 45 |
/// # Lifetimes |
| 46 |
/// |
| 47 |
/// - `s`: The life time of an input string slice being parsed. |
| 48 |
/// |
| 49 |
/// [`Parser`]: crate::Parser |
| 50 |
/// [`PatternItem`]: crate::PatternItem |
| 51 |
/// [`FromStr`]: core::str::FromStr |
| 52 |
#[derive(PartialEq, Debug, Clone)] |
| 53 |
#[non_exhaustive] |
| 54 |
pub enum ParsedPatternItem<'s, P> { |
| 55 |
Placeholder(P), |
| 56 |
Literal { content: Cow<'s, str>, quoted: bool }, |
| 57 |
} |
| 58 |
|
| 59 |
impl<'s, P> From<(&'s str, bool)> for ParsedPatternItem<'s, P> { |
| 60 |
fn from(input: (&'s str, bool)) -> Self { |
| 61 |
Self::Literal { |
| 62 |
content: Cow::Borrowed(input.0), |
| 63 |
quoted: input.1, |
| 64 |
} |
| 65 |
} |
| 66 |
} |