• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zbraniecki / icu4x / 9357137046

03 Jun 2024 08:51PM UTC coverage: 75.121% (-1.1%) from 76.254%
9357137046

push

github

web-flow
Switch locid Value to use Subtag (#4941)

This is part of #1833 switching Value API to use Subtag.

61 of 71 new or added lines in 11 files covered. (85.92%)

3224 existing lines in 178 files now uncovered.

52958 of 70497 relevant lines covered (75.12%)

572757.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/utils/pattern/src/common.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
use crate::Error;
6
use writeable::{Part, TryWriteable};
7

8
#[cfg(feature = "alloc")]
9
use alloc::{borrow::Cow, borrow::ToOwned};
10

11
/// A borrowed item in a [`Pattern`]. Items are either string literals or placeholders.
12
///
13
/// [`Pattern`]: crate::Pattern
14
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
26✔
15
#[allow(clippy::exhaustive_enums)] // Part of core data model
16
pub enum PatternItem<'a, T> {
17
    /// A placeholder of the type specified on this [`PatternItem`].
18
    Placeholder(T),
5✔
19
    /// A string literal. This can occur in one of three places:
20
    ///
21
    /// 1. Between the start of the string and the first placeholder (prefix)
22
    /// 2. Between two placeholders (infix)
23
    /// 3. Between the final placeholder and the end of the string (suffix)
24
    Literal(&'a str),
8✔
25
}
26

27
/// A borrowed-or-owned item in a [`Pattern`]. Items are either string literals or placeholders.
28
///
29
/// ✨ *Enabled with the `alloc` Cargo feature.*
30
///
31
/// [`Pattern`]: crate::Pattern
32
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
25✔
33
#[allow(clippy::exhaustive_enums)] // Part of core data model
34
#[cfg(feature = "alloc")]
35
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
76✔
36
pub enum PatternItemCow<'a, T> {
37
    /// A placeholder of the type specified on this [`PatternItemCow`].
38
    Placeholder(T),
29✔
39
    /// A string literal. This can occur in one of three places:
40
    ///
41
    /// 1. Between the start of the string and the first placeholder (prefix)
42
    /// 2. Between two placeholders (infix)
43
    /// 3. Between the final placeholder and the end of the string (suffix)
44
    #[cfg_attr(feature = "serde", serde(borrow))]
UNCOV
45
    Literal(Cow<'a, str>),
×
46
}
47

48
#[cfg(feature = "alloc")]
49
impl<'a, T, U> From<PatternItem<'a, U>> for PatternItemCow<'a, T>
50
where
51
    T: From<U>,
52
{
53
    fn from(value: PatternItem<'a, U>) -> Self {
12✔
54
        match value {
12✔
55
            PatternItem::Placeholder(t) => Self::Placeholder(t.into()),
4✔
56
            PatternItem::Literal(s) => Self::Literal(Cow::Borrowed(s)),
8✔
57
        }
58
    }
12✔
59
}
60

61
/// Types that implement backing data models for [`Pattern`] implement this trait.
62
///
63
/// The trait has no public methods and is not implementable outside of this crate.
64
///
65
/// [`Pattern`]: crate::Pattern
66
pub trait PatternBackend: crate::private::Sealed + 'static {
67
    /// The type to be used as the placeholder key in code.
68
    type PlaceholderKey<'a>;
69

70
    /// Cowable version of the type to be used as the placeholder key in code.
71
    // Note: it is not good practice to feature-gate trait methods, but this trait is sealed
72
    #[cfg(feature = "alloc")]
73
    type PlaceholderKeyCow<'a>;
74

75
    /// The type of error that the [`TryWriteable`] for this backend can return.
76
    type Error<'a>;
77

78
    /// The unsized type of the store required for this backend, usually `str` or `[u8]`.
79
    type Store: ?Sized;
80

81
    /// The iterator type returned by [`Self::try_from_items`].
82
    #[doc(hidden)] // TODO(#4467): Should be internal
83
    type Iter<'a>: Iterator<Item = PatternItem<'a, Self::PlaceholderKey<'a>>>;
84

85
    /// Checks a store for validity, returning an error if invalid.
86
    #[doc(hidden)] // TODO(#4467): Should be internal
87
    fn validate_store(store: &Self::Store) -> Result<(), Error>;
88

89
    /// Constructs a store from pattern items.
90
    #[doc(hidden)]
91
    // TODO(#4467): Should be internal
92
    // Note: it is not good practice to feature-gate trait methods, but this trait is sealed
93
    #[cfg(feature = "alloc")]
94
    fn try_from_items<
95
        'cow,
96
        'ph,
97
        I: Iterator<Item = Result<PatternItemCow<'cow, Self::PlaceholderKeyCow<'ph>>, Error>>,
98
    >(
99
        items: I,
100
    ) -> Result<<Self::Store as ToOwned>::Owned, Error>
101
    where
102
        Self::Store: ToOwned;
103

104
    /// Iterates over the pattern items in a store.
105
    #[doc(hidden)] // TODO(#4467): Should be internal
106
    fn iter_items(store: &Self::Store) -> Self::Iter<'_>;
107
}
108

109
/// Default annotation for the literal portion of a pattern.
110
///
111
/// For more information, see [`PlaceholderValueProvider`]. For an example, see [`Pattern`].
112
///
113
/// [`Pattern`]: crate::Pattern
114
pub const PATTERN_LITERAL_PART: Part = Part {
115
    category: "pattern",
116
    value: "literal",
117
};
118

119
/// Default annotation for the placeholder portion of a pattern.
120
///
121
/// For more information, see [`PlaceholderValueProvider`]. For an example, see [`Pattern`].
122
///
123
/// [`Pattern`]: crate::Pattern
124
pub const PATTERN_PLACEHOLDER_PART: Part = Part {
125
    category: "pattern",
126
    value: "placeholder",
127
};
128

129
/// Trait implemented on collections that can produce [`TryWriteable`]s for interpolation.
130
///
131
/// This trait determines the [`Part`]s produced by the writeable. In this crate, implementations
132
/// of this trait default to using [`PATTERN_LITERAL_PART`] and [`PATTERN_PLACEHOLDER_PART`].
133
pub trait PlaceholderValueProvider<K> {
134
    type Error;
135

136
    type W<'a>: TryWriteable<Error = Self::Error>
137
    where
138
        Self: 'a;
139

140
    const LITERAL_PART: Part;
141

142
    /// Returns the [`TryWriteable`] to substitute for the given placeholder
143
    /// and the [`Part`] representing it.
144
    fn value_for(&self, key: K) -> (Self::W<'_>, Part);
145
}
146

147
impl<'b, K, T> PlaceholderValueProvider<K> for &'b T
148
where
149
    T: PlaceholderValueProvider<K> + ?Sized,
150
{
151
    type Error = T::Error;
152
    type W<'a> = T::W<'a> where T: 'a, 'b: 'a;
153
    const LITERAL_PART: Part = T::LITERAL_PART;
154
    fn value_for(&self, key: K) -> (Self::W<'_>, Part) {
155
        (*self).value_for(key)
156
    }
157
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc