|
6 | 6 | mod databake; |
7 | 7 | #[cfg(feature = "serde")] |
8 | 8 | mod serde; |
9 | | - |
10 | | -use core::{ |
11 | | - convert::Infallible, |
12 | | - fmt::{self, Write}, |
13 | | - marker::PhantomData, |
14 | | -}; |
15 | | - |
16 | | -use writeable::{adapters::TryWriteableInfallibleAsWriteable, PartsWrite, TryWriteable, Writeable}; |
17 | | - |
18 | 9 | use crate::common::*; |
19 | 10 | use crate::Error; |
20 | | - |
| 11 | +use crate::PatternOrUtf8Error; |
21 | 12 | #[cfg(feature = "alloc")] |
22 | 13 | use crate::{Parser, ParserOptions}; |
23 | 14 | #[cfg(feature = "alloc")] |
24 | 15 | use alloc::{borrow::ToOwned, str::FromStr, string::String}; |
| 16 | +use core::{ |
| 17 | + convert::Infallible, |
| 18 | + fmt::{self, Write}, |
| 19 | + marker::PhantomData, |
| 20 | +}; |
| 21 | +use writeable::{adapters::TryWriteableInfallibleAsWriteable, PartsWrite, TryWriteable, Writeable}; |
25 | 22 |
|
26 | 23 | /// A string pattern with placeholders. |
27 | 24 | /// |
@@ -167,6 +164,33 @@ where |
167 | 164 | } |
168 | 165 | } |
169 | 166 |
|
| 167 | +impl<'a, B> Pattern<B, &'a B::Store> |
| 168 | +where |
| 169 | + B: PatternBackend, |
| 170 | +{ |
| 171 | + /// Creates a pattern from its store encoded as bytes. |
| 172 | + /// |
| 173 | + /// # Examples |
| 174 | + /// |
| 175 | + /// ``` |
| 176 | + /// use icu_pattern::Pattern; |
| 177 | + /// use icu_pattern::SinglePlaceholder; |
| 178 | + /// |
| 179 | + /// Pattern::<SinglePlaceholder, _>::try_from_bytes_store(b"\x01 days") |
| 180 | + /// .expect("single placeholder pattern"); |
| 181 | + /// ``` |
| 182 | + pub fn try_from_bytes_store( |
| 183 | + bytes: &'a [u8], |
| 184 | + ) -> Result<Self, PatternOrUtf8Error<B::StoreFromBytesError>> { |
| 185 | + let store = B::try_store_from_bytes(bytes).map_err(PatternOrUtf8Error::Utf8)?; |
| 186 | + B::validate_store(store).map_err(PatternOrUtf8Error::Pattern)?; |
| 187 | + Ok(Self { |
| 188 | + _backend: PhantomData, |
| 189 | + store, |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
| 193 | + |
170 | 194 | #[cfg(feature = "alloc")] |
171 | 195 | impl<B> Pattern<B, <B::Store as ToOwned>::Owned> |
172 | 196 | where |
|
0 commit comments