1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! Code for the [`MultiNamedPlaceholder`] pattern backend.

#[cfg(feature = "alloc")]
use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, str::FromStr, string::String};
use core::fmt;
#[cfg(feature = "litemap")]
use litemap::LiteMap;
use writeable::Writeable;

use crate::common::*;
use crate::Error;

/// A string wrapper for the [`MultiNamedPlaceholder`] pattern backend.
///
/// # Examples
///
/// ```
/// use core::cmp::Ordering;
/// use core::str::FromStr;
/// use icu_pattern::MultiNamedPlaceholderKey;
/// use icu_pattern::MultiNamedPlaceholderPattern;
/// use icu_pattern::PatternItem;
///
/// // Parse the string syntax and check the resulting data store:
/// let pattern = MultiNamedPlaceholderPattern::try_from_str(
///     "Hello, {person0} and {person1}!", Default::default()
/// )
/// .unwrap();
///
/// assert_eq!(
///     pattern.iter().cmp(
///         [
///             PatternItem::Literal("Hello, "),
///             PatternItem::Placeholder(MultiNamedPlaceholderKey(
///                 "person0".into()
///             )),
///             PatternItem::Literal(" and "),
///             PatternItem::Placeholder(MultiNamedPlaceholderKey(
///                 "person1".into()
///             )),
///             PatternItem::Literal("!")
///         ]
///         .into_iter()
///     ),
///     Ordering::Equal
/// );
/// ```
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
#[allow(clippy::exhaustive_structs)] // transparent newtype
pub struct MultiNamedPlaceholderKey<'a>(pub &'a str);

/// Cowable version of [`MultiNamedPlaceholderKey`], used during construction.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(transparent)]
#[allow(clippy::exhaustive_structs)] // transparent newtype
#[cfg(feature = "alloc")]
pub struct MultiNamedPlaceholderKeyCow<'a>(pub Cow<'a, str>);

#[cfg(feature = "alloc")]
impl FromStr for MultiNamedPlaceholderKeyCow<'_> {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Can't borrow the str here unfortunately
        Ok(MultiNamedPlaceholderKeyCow(Cow::Owned(String::from(s))))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct MissingNamedPlaceholderError<'a> {
    pub name: &'a str,
}

impl Writeable for MissingNamedPlaceholderError<'_> {
    fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
        sink.write_char('{')?;
        sink.write_str(self.name)?;
        sink.write_char('}')?;
        Ok(())
    }
}

#[cfg(feature = "alloc")]
impl<'k, K, W> PlaceholderValueProvider<MultiNamedPlaceholderKey<'k>> for BTreeMap<K, W>
where
    K: Ord + core::borrow::Borrow<str>,
    W: Writeable,
{
    type Error = MissingNamedPlaceholderError<'k>;
    type W<'a>
        = Result<&'a W, Self::Error>
    where
        W: 'a,
        Self: 'a;
    const LITERAL_PART: writeable::Part = crate::PATTERN_LITERAL_PART;
    #[inline]
    fn value_for<'a>(
        &'a self,
        key: MultiNamedPlaceholderKey<'k>,
    ) -> (Self::W<'a>, writeable::Part) {
        let writeable = match self.get(key.0) {
            Some(value) => Ok(value),
            None => Err(MissingNamedPlaceholderError { name: key.0 }),
        };
        (writeable, crate::PATTERN_PLACEHOLDER_PART)
    }
}

#[cfg(feature = "litemap")]
impl<'k, K, W, S> PlaceholderValueProvider<MultiNamedPlaceholderKey<'k>> for LiteMap<K, W, S>
where
    K: Ord + core::borrow::Borrow<str>,
    W: Writeable,
    S: litemap::store::Store<K, W>,
{
    type Error = MissingNamedPlaceholderError<'k>;
    type W<'a>
        = Result<&'a W, Self::Error>
    where
        W: 'a,
        Self: 'a;
    const LITERAL_PART: writeable::Part = crate::PATTERN_LITERAL_PART;
    #[inline]
    fn value_for<'a>(
        &'a self,
        key: MultiNamedPlaceholderKey<'k>,
    ) -> (Self::W<'a>, writeable::Part) {
        let writeable = match self.get(key.0) {
            Some(value) => Ok(value),
            None => Err(MissingNamedPlaceholderError { name: key.0 }),
        };
        (writeable, crate::PATTERN_PLACEHOLDER_PART)
    }
}

/// Backend for patterns containing zero or more named placeholders.
///
/// This empty type is not constructible.
///
/// # Placeholder Keys
///
/// The placeholder is [`MultiNamedPlaceholderKey`].
///
/// In [`Pattern::interpolate()`], pass a map-like structure. Missing keys will be replaced
/// with the Unicode replacement character U+FFFD.
///
/// # Encoding Details
///
/// The literals and placeholders are stored in context. A placeholder is encoded as a name length
/// in octal code points followed by the placeholder name.
///
/// For example, consider the pattern: "Hello, {user} and {someone_else}!"
///
/// The encoding for this would be:
///
/// ```txt
/// Hello, \x00\x04user and \x01\x04someone_else!
/// ```
///
/// where `\x00\x04` and `\x01\x04` are a big-endian octal number representing the lengths of
/// their respective placeholder names.
///
/// Consequences of this encoding:
///
/// 1. The maximum placeholder name length is 64 bytes
/// 2. Code points in the range `\x00` through `\x07` are reserved for the placeholder name
///
/// # Examples
///
/// Example patterns supported by this backend:
///
/// ```
/// use core::str::FromStr;
/// use icu_pattern::MultiNamedPlaceholder;
/// use icu_pattern::Pattern;
/// use std::collections::BTreeMap;
///
/// let placeholder_value_map: BTreeMap<&str, &str> = [
///     ("num", "5"),
///     ("letter", "X"),
///     ("", "empty"),
///     ("unused", "unused"),
/// ]
/// .into_iter()
/// .collect();
///
/// // Single placeholder:
/// assert_eq!(
///     Pattern::<MultiNamedPlaceholder>::try_from_str("{num} days ago", Default::default())
///         .unwrap()
///         .try_interpolate_to_string(&placeholder_value_map)
///         .unwrap(),
///     "5 days ago",
/// );
///
/// // No placeholder (note, the placeholder value is never accessed):
/// assert_eq!(
///     Pattern::<MultiNamedPlaceholder>::try_from_str("yesterday", Default::default())
///         .unwrap()
///         .try_interpolate_to_string(&placeholder_value_map)
///         .unwrap(),
///     "yesterday",
/// );
///
/// // No literals, only placeholders:
/// assert_eq!(
///     Pattern::<MultiNamedPlaceholder>::try_from_str("{letter}{num}{}", Default::default())
///         .unwrap()
///         .try_interpolate_to_string(&placeholder_value_map)
///         .unwrap(),
///     "X5empty",
/// );
/// ```
///
/// Use [`LiteMap`] for alloc-free formatting:
///
/// ```
/// use core::str::FromStr;
/// use icu_pattern::MultiNamedPlaceholderPattern;
/// use litemap::LiteMap;
/// use writeable::TryWriteable;
///
/// static placeholder_value_map: LiteMap<&str, usize, &[(&str, usize)]> =
///     LiteMap::from_sorted_store_unchecked(&[("seven", 11)]);
///
/// // Note: String allocates, but this could be a non-allocating sink
/// let mut sink = String::new();
///
/// MultiNamedPlaceholderPattern::try_from_str("{seven}", Default::default())
///     .unwrap()
///     .try_interpolate(&placeholder_value_map)
///     .try_write_to(&mut sink)
///     .unwrap()
///     .unwrap();
///
/// assert_eq!(sink, "11");
/// ```
///
/// Missing placeholder values cause an error result to be returned. However,
/// based on the design of [`TryWriteable`], the error can be discarded to get
/// a best-effort interpolation with potential replacement characters.
///
/// ```should_panic
/// use core::str::FromStr;
/// use icu_pattern::MultiNamedPlaceholder;
/// use icu_pattern::Pattern;
/// use std::collections::BTreeMap;
///
/// let placeholder_value_map: BTreeMap<&str, &str> =
///     [("num", "5"), ("letter", "X")].into_iter().collect();
///
/// Pattern::<MultiNamedPlaceholder>::try_from_str("Your name is {your_name}", Default::default())
///     .unwrap()
///     .try_interpolate_to_string(&placeholder_value_map)
///     .unwrap();
/// ```
///
/// Recover the best-effort lossy string by directly using [`Pattern::try_interpolate()`]:
///
/// ```
/// use core::str::FromStr;
/// use icu_pattern::MissingNamedPlaceholderError;
/// use icu_pattern::MultiNamedPlaceholder;
/// use icu_pattern::Pattern;
/// use std::borrow::Cow;
/// use std::collections::BTreeMap;
/// use writeable::TryWriteable;
///
/// let placeholder_value_map: BTreeMap<&str, &str> =
///     [("num", "5"), ("letter", "X")].into_iter().collect();
///
/// let pattern = Pattern::<MultiNamedPlaceholder>::try_from_str(
///     "Your name is {your_name}", Default::default(),
/// )
/// .unwrap();
///
/// let mut buffer = String::new();
/// let result = pattern
///     .try_interpolate(&placeholder_value_map)
///     .try_write_to(&mut buffer)
///     .expect("infallible write to String");
///
/// assert!(matches!(result, Err(MissingNamedPlaceholderError { .. })));
/// assert_eq!(result.unwrap_err().name, "your_name");
/// assert_eq!(buffer, "Your name is {your_name}");
/// ```
///
/// [`Pattern::interpolate()`]: crate::Pattern::interpolate
/// [`Pattern::try_interpolate()`]: crate::Pattern::try_interpolate
/// [`TryWriteable`]: writeable::TryWriteable
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[allow(clippy::exhaustive_enums)] // Empty Enum
pub enum MultiNamedPlaceholder {}

impl crate::private::Sealed for MultiNamedPlaceholder {}

impl PatternBackend for MultiNamedPlaceholder {
    type PlaceholderKey<'a> = MultiNamedPlaceholderKey<'a>;
    #[cfg(feature = "alloc")]
    type PlaceholderKeyCow<'a> = MultiNamedPlaceholderKeyCow<'a>;
    type Error<'a> = MissingNamedPlaceholderError<'a>;
    type Store = str;
    type Iter<'a> = MultiNamedPlaceholderPatternIterator<'a>;

    fn validate_store(store: &Self::Store) -> Result<(), Error> {
        let mut iter = MultiNamedPlaceholderPatternIterator::new(store);
        while iter
            .try_next()
            .map_err(|e| match e {
                MultiNamedPlaceholderError::InvalidStore => Error::InvalidPattern,
                MultiNamedPlaceholderError::Unreachable => {
                    debug_assert!(false, "unreachable");
                    Error::InvalidPattern
                }
            })?
            .is_some()
        {}
        Ok(())
    }

    fn iter_items(store: &Self::Store) -> Self::Iter<'_> {
        MultiNamedPlaceholderPatternIterator::new(store)
    }

    #[cfg(feature = "alloc")]
    fn try_from_items<
        'cow,
        'ph,
        I: Iterator<Item = Result<PatternItemCow<'cow, Self::PlaceholderKeyCow<'ph>>, Error>>,
    >(
        items: I,
    ) -> Result<Box<str>, Error> {
        let mut string = String::new();
        for item in items {
            match item? {
                PatternItemCow::Literal(s) if s.contains(|x| (x as usize) <= 0x07) => {
                    // TODO: Should this be a different error type?
                    return Err(Error::InvalidPattern);
                }
                PatternItemCow::Literal(s) => string.push_str(&s),
                PatternItemCow::Placeholder(ph_key) => {
                    let name_length = ph_key.0.len();
                    if name_length >= 64 {
                        return Err(Error::InvalidPlaceholder);
                    }
                    let lead = (name_length >> 3) as u8;
                    let trail = (name_length & 0x7) as u8;
                    string.push(char::from(lead));
                    string.push(char::from(trail));
                    string.push_str(&ph_key.0);
                }
            }
        }
        Ok(string.into_boxed_str())
    }

    fn empty() -> &'static Self::Store {
        ""
    }
}

#[derive(Debug)]
pub struct MultiNamedPlaceholderPatternIterator<'a> {
    store: &'a str,
}

// Note: we don't implement ExactSizeIterator since we don't store that metadata in MultiNamed.

impl<'a> Iterator for MultiNamedPlaceholderPatternIterator<'a> {
    type Item = PatternItem<'a, MultiNamedPlaceholderKey<'a>>;
    fn next(&mut self) -> Option<Self::Item> {
        match self.try_next() {
            Ok(next) => next,
            Err(MultiNamedPlaceholderError::InvalidStore) => {
                debug_assert!(
                    false,
                    "invalid store with {} bytes remaining",
                    self.store.len()
                );
                None
            }
            Err(MultiNamedPlaceholderError::Unreachable) => {
                debug_assert!(false, "unreachable");
                None
            }
        }
    }
}

enum MultiNamedPlaceholderError {
    InvalidStore,
    Unreachable,
}

impl<'a> MultiNamedPlaceholderPatternIterator<'a> {
    fn new(store: &'a str) -> Self {
        Self { store }
    }

    fn try_next(
        &mut self,
    ) -> Result<Option<PatternItem<'a, MultiNamedPlaceholderKey<'a>>>, MultiNamedPlaceholderError>
    {
        match self.store.find(|x| (x as usize) <= 0x07) {
            Some(0) => {
                // Placeholder
                let Some(&[lead, trail]) = self.store.as_bytes().get(0..2) else {
                    return Err(MultiNamedPlaceholderError::InvalidStore);
                };
                debug_assert!(lead <= 7);
                if trail > 7 {
                    return Err(MultiNamedPlaceholderError::InvalidStore);
                }
                let placeholder_len = (lead << 3) + trail;
                let boundary = (placeholder_len as usize) + 2;
                // TODO: use .split_at_checked() when available:
                // https://github.com/rust-lang/rust/issues/119128
                let Some(placeholder_name) = self.store.get(2..boundary) else {
                    return Err(MultiNamedPlaceholderError::InvalidStore);
                };
                let Some(remainder) = self.store.get(boundary..) else {
                    debug_assert!(false, "should be a perfect slice");
                    return Err(MultiNamedPlaceholderError::Unreachable);
                };
                self.store = remainder;
                Ok(Some(PatternItem::Placeholder(MultiNamedPlaceholderKey(
                    placeholder_name,
                ))))
            }
            Some(i) => {
                // Literal
                // TODO: use .split_at_checked() when available:
                // https://github.com/rust-lang/rust/issues/119128
                let Some(literal) = self.store.get(..i) else {
                    debug_assert!(false, "should be a perfect slice");
                    return Err(MultiNamedPlaceholderError::Unreachable);
                };
                let Some(remainder) = self.store.get(i..) else {
                    debug_assert!(false, "should be a perfect slice");
                    return Err(MultiNamedPlaceholderError::Unreachable);
                };
                self.store = remainder;
                Ok(Some(PatternItem::Literal(literal)))
            }
            None if self.store.is_empty() => {
                // End of string
                Ok(None)
            }
            None => {
                // Closing literal
                let literal = self.store;
                self.store = "";
                Ok(Some(PatternItem::Literal(literal)))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{MultiNamedPlaceholder, MultiNamedPlaceholderPattern};

    #[test]
    fn test_invalid() {
        let long_str = "0123456789".repeat(1000000);
        let strings = [
            "{",    // invalid syntax
            "{@}",  // placeholder name too long
            "\x00", // invalid character
            "\x07", // invalid character
        ];
        for string in strings {
            let string = string.replace('@', &long_str);
            assert!(
                MultiNamedPlaceholderPattern::try_from_str(&string, Default::default()).is_err(),
                "{string:?}"
            );
        }
        let stores = [
            "\x00",      // too short
            "\x02",      // too short
            "\x00\x02",  // no placeholder name
            "\x00\x02a", // placeholder name too short
        ];
        for store in stores {
            assert!(
                MultiNamedPlaceholder::validate_store(store).is_err(),
                "{store:?}"
            );
        }
    }
}