summaryrefslogtreecommitdiffstats
path: root/vendor/icu_list/src/provider.rs
blob: 27f3e4fecd051404f32891624426cb023215251d (plain)
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
// 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 ).

// Provider structs must be stable
#![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]

//! Data provider struct definitions for this ICU4X component.
//!
//! Read more about data providers: [`icu_provider`]

use crate::ListLength;
use alloc::borrow::Cow;
use icu_provider::DataMarker;
use icu_provider::{yoke, zerofrom};
use writeable::{LengthHint, Writeable};

pub use crate::string_matcher::StringMatcher;

/// Symbols and metadata required for [`ListFormatter`](crate::ListFormatter).
#[icu_provider::data_struct(
    AndListV1Marker = "list/and@1",
    OrListV1Marker = "list/or@1",
    UnitListV1Marker = "list/unit@1"
)]
#[derive(Clone, Debug)]
#[cfg_attr(
    feature = "datagen",
    derive(serde::Serialize, databake::Bake),
    databake(path = icu_list::provider),
)]
pub struct ListFormatterPatternsV1<'data>(
    #[cfg_attr(feature = "datagen", serde(with = "deduplicating_array"))]
    /// The patterns in the order start, middle, end, pair, short_start, short_middle,
    /// short_end, short_pair, narrow_start, narrow_middle, narrow_end, narrow_pair,
    pub [ConditionalListJoinerPattern<'data>; 12],
);

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for ListFormatterPatternsV1<'de> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        #[cfg(not(feature = "serde_human"))]
        if deserializer.is_human_readable() {
            use serde::de::Error;
            return Err(D::Error::custom(
                    "Deserializing human-readable ListFormatter data requires the 'serde_human' feature",
                ));
        }

        Ok(ListFormatterPatternsV1(deduplicating_array::deserialize(
            deserializer,
        )?))
    }
}

pub(crate) struct ErasedListV1Marker;

impl DataMarker for ErasedListV1Marker {
    type Yokeable = ListFormatterPatternsV1<'static>;
}

impl<'data> ListFormatterPatternsV1<'data> {
    pub(crate) fn start(&self, style: ListLength) -> &ConditionalListJoinerPattern<'data> {
        #![allow(clippy::indexing_slicing)] // style as usize < 3
        &self.0[4 * (style as usize)]
    }

    pub(crate) fn middle(&self, style: ListLength) -> &ConditionalListJoinerPattern<'data> {
        #![allow(clippy::indexing_slicing)] // style as usize < 3
        &self.0[4 * (style as usize) + 1]
    }

    pub(crate) fn end(&self, style: ListLength) -> &ConditionalListJoinerPattern<'data> {
        #![allow(clippy::indexing_slicing)] // style as usize < 3
        &self.0[4 * (style as usize) + 2]
    }

    pub(crate) fn pair(&self, style: ListLength) -> &ConditionalListJoinerPattern<'data> {
        #![allow(clippy::indexing_slicing)] // style as usize < 3
        &self.0[4 * (style as usize) + 3]
    }

    /// The range of the number of bytes required by the list literals to join a
    /// list of length `len`. If none of the patterns are conditional, this is exact.
    pub(crate) fn size_hint(&self, style: ListLength, len: usize) -> LengthHint {
        match len {
            0 | 1 => LengthHint::exact(0),
            2 => self.pair(style).size_hint(),
            n => {
                self.start(style).size_hint()
                    + self.middle(style).size_hint() * (n - 3)
                    + self.end(style).size_hint()
            }
        }
    }
}

/// A pattern that can behave conditionally on the next element.
#[derive(Clone, Debug, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom)]
#[cfg_attr(
    feature = "datagen",
    derive(serde::Serialize, databake::Bake),
    databake(path = icu_list::provider),
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct ConditionalListJoinerPattern<'data> {
    /// The default pattern
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub default: ListJoinerPattern<'data>,
    /// And optional special case
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub special_case: Option<SpecialCasePattern<'data>>,
}

/// The special case of a [`ConditionalListJoinerPattern`]
#[derive(Clone, Debug, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom)]
#[cfg_attr(
    feature = "datagen",
    derive(serde::Serialize, databake::Bake),
    databake(path = icu_list::provider),
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct SpecialCasePattern<'data> {
    /// The condition on the following element
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub condition: StringMatcher<'data>,
    /// The pattern if the condition matches
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub pattern: ListJoinerPattern<'data>,
}

/// A pattern containing two numeric placeholders ("{0}, and {1}.")
#[derive(Clone, Debug, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize))]
pub struct ListJoinerPattern<'data> {
    /// The pattern string without the placeholders
    string: Cow<'data, str>,
    /// The index of the first placeholder. Always <= index_1.
    // Always 0 for CLDR data, so we don't need to serialize it.
    // In-memory we have free space for it as index_1 doesn't
    // fill a word.
    #[cfg_attr(feature = "datagen", serde(skip))]
    index_0: u8,
    /// The index of the second placeholder. Always < string.len().
    index_1: u8,
}

#[cfg(feature = "serde")]
impl<'de: 'data, 'data> serde::Deserialize<'de> for ListJoinerPattern<'data> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(serde::Deserialize)]
        struct Dummy<'data> {
            #[cfg_attr(feature = "serde", serde(borrow))]
            string: Cow<'data, str>,
            index_1: u8,
        }
        let Dummy { string, index_1 } = Dummy::deserialize(deserializer)?;

        if index_1 as usize > string.len() {
            use serde::de::Error;
            Err(D::Error::custom("invalid index_1"))
        } else {
            Ok(ListJoinerPattern {
                string,
                index_0: 0,
                index_1,
            })
        }
    }
}

impl<'a> ListJoinerPattern<'a> {
    /// Constructs a [`ListJoinerPattern`] from raw parts. Used by databake.
    ///
    /// # Safety
    /// index_1 may be at most string.len()
    pub const unsafe fn from_parts_unchecked(string: &'a str, index_1: u8) -> Self {
        Self {
            string: Cow::Borrowed(string),
            index_0: 0,
            index_1,
        }
    }
}

pub(crate) type PatternParts<'a> = (&'a str, &'a str, &'a str);

impl<'a> ConditionalListJoinerPattern<'a> {
    pub(crate) fn parts<'b, W: Writeable + ?Sized>(
        &'a self,
        following_value: &'b W,
    ) -> PatternParts<'a> {
        match &self.special_case {
            Some(SpecialCasePattern { condition, pattern })
                // TODO: Implement lookahead instead of materializing here.
                if condition.test(&*following_value.write_to_string()) =>
            {
                pattern.borrow_tuple()
            }
            _ => self.default.borrow_tuple(),
        }
    }

    /// The expected length of this pattern
    pub fn size_hint(&'a self) -> LengthHint {
        let mut hint = self.default.size_hint();
        if let Some(special_case) = &self.special_case {
            hint |= special_case.pattern.size_hint()
        }
        hint
    }
}

impl<'data> ListJoinerPattern<'data> {
    fn borrow_tuple(&'data self) -> PatternParts<'data> {
        #![allow(clippy::indexing_slicing)] // by invariant
        let index_0 = self.index_0 as usize;
        let index_1 = self.index_1 as usize;
        (
            &self.string[0..index_0],
            &self.string[index_0..index_1],
            &self.string[index_1..],
        )
    }

    fn size_hint(&self) -> LengthHint {
        LengthHint::exact(self.string.len())
    }
}

#[cfg(feature = "datagen")]
mod datagen {
    #![allow(clippy::indexing_slicing)] // datagen

    use super::*;
    use icu_provider::DataError;

    impl<'data> ListFormatterPatternsV1<'data> {
        /// The patterns in the order start, middle, end, pair, short_start, short_middle,
        /// short_end, short_pair, narrow_start, narrow_middle, narrow_end, narrow_pair,
        pub fn try_new(patterns: [&str; 12]) -> Result<Self, DataError> {
            Ok(Self([
                ListJoinerPattern::from_str(patterns[0], true, false)?.into(),
                ListJoinerPattern::from_str(patterns[1], false, false)?.into(),
                ListJoinerPattern::from_str(patterns[2], false, true)?.into(),
                ListJoinerPattern::from_str(patterns[3], true, true)?.into(),
                ListJoinerPattern::from_str(patterns[4], true, false)?.into(),
                ListJoinerPattern::from_str(patterns[5], false, false)?.into(),
                ListJoinerPattern::from_str(patterns[6], false, true)?.into(),
                ListJoinerPattern::from_str(patterns[7], true, true)?.into(),
                ListJoinerPattern::from_str(patterns[8], true, false)?.into(),
                ListJoinerPattern::from_str(patterns[9], false, false)?.into(),
                ListJoinerPattern::from_str(patterns[10], false, true)?.into(),
                ListJoinerPattern::from_str(patterns[11], true, true)?.into(),
            ]))
        }

        /// Adds a special case to all `pattern`s that will evaluate to
        /// `alternative_pattern` when `regex` matches the following element.
        /// The regex is interpreted case-insensitive and anchored to the beginning, but
        /// to improve efficiency does not search for full matches. If a full match is
        /// required, use `$`.
        pub fn make_conditional(
            &mut self,
            pattern: &str,
            regex: &StringMatcher<'static>,
            alternative_pattern: &str,
        ) -> Result<(), DataError> {
            let old = ListJoinerPattern::from_str(pattern, true, true)?;
            for i in 0..12 {
                if self.0[i].default == old {
                    self.0[i].special_case = Some(SpecialCasePattern {
                        condition: regex.clone(),
                        pattern: ListJoinerPattern::from_str(
                            alternative_pattern,
                            i % 4 == 0 || i % 4 == 3, // allow_prefix = start or pair
                            i % 4 == 2 || i % 4 == 3, // allow_suffix = end or pair
                        )?,
                    });
                }
            }
            Ok(())
        }
    }

    impl<'data> ListJoinerPattern<'data> {
        /// Construct the pattern from a CLDR pattern string
        pub fn from_str(
            pattern: &str,
            allow_prefix: bool,
            allow_suffix: bool,
        ) -> Result<Self, DataError> {
            match (pattern.find("{0}"), pattern.find("{1}")) {
                (Some(index_0), Some(index_1))
                    if index_0 < index_1
                        && (allow_prefix || index_0 == 0)
                        && (allow_suffix || index_1 == pattern.len() - 3) =>
                {
                    if (index_0 > 0 && !cfg!(test)) || index_1 - 3 >= 256 {
                        return Err(DataError::custom(
                            "Found valid pattern that cannot be stored in ListFormatterPatternsV1",
                        )
                        .with_debug_context(pattern));
                    }
                    Ok(ListJoinerPattern {
                        string: Cow::Owned(alloc::format!(
                            "{}{}{}",
                            &pattern[0..index_0],
                            &pattern[index_0 + 3..index_1],
                            &pattern[index_1 + 3..]
                        )),
                        index_0: index_0 as u8,
                        index_1: (index_1 - 3) as u8,
                    })
                }
                _ => Err(DataError::custom("Invalid list pattern").with_debug_context(pattern)),
            }
        }
    }

    impl<'data> From<ListJoinerPattern<'data>> for ConditionalListJoinerPattern<'data> {
        fn from(default: ListJoinerPattern<'data>) -> Self {
            Self {
                default,
                special_case: None,
            }
        }
    }

    impl databake::Bake for ListJoinerPattern<'_> {
        fn bake(&self, env: &databake::CrateEnv) -> databake::TokenStream {
            env.insert("icu_list");
            let string = (&*self.string).bake(env);
            let index_1 = self.index_1.bake(env);
            // Safe because our own data is safe
            databake::quote! { unsafe {
                ::icu_list::provider::ListJoinerPattern::from_parts_unchecked(#string, #index_1)
            }}
        }
    }
}

#[cfg(all(test, feature = "datagen"))]
pub(crate) mod test {
    use super::*;

    pub fn test_patterns() -> ListFormatterPatternsV1<'static> {
        let mut patterns = ListFormatterPatternsV1::try_new([
            // Wide: general
            "@{0}:{1}",
            "{0},{1}",
            "{0}.{1}!",
            "${0};{1}+",
            // Short: different pattern lengths
            "{0}1{1}",
            "{0}12{1}",
            "{0}12{1}34",
            "{0}123{1}456",
            // Narrow: conditionals
            "{0}: {1}",
            "{0}, {1}",
            "{0}. {1}",
            "{0}. {1}",
        ])
        .unwrap();
        patterns
            .make_conditional("{0}. {1}", &StringMatcher::new("A").unwrap(), "{0} :o {1}")
            .unwrap();
        patterns
    }

    #[test]
    fn rejects_bad_patterns() {
        assert!(ListJoinerPattern::from_str("{0} and", true, true).is_err());
        assert!(ListJoinerPattern::from_str("and {1}", true, true).is_err());
        assert!(ListJoinerPattern::from_str("{1} and {0}", true, true).is_err());
        assert!(ListJoinerPattern::from_str("{1{0}}", true, true).is_err());
        assert!(ListJoinerPattern::from_str("{0\u{202e}} and {1}", true, true).is_err());
        assert!(ListJoinerPattern::from_str("{{0}} {{1}}", true, true).is_ok());

        assert!(ListJoinerPattern::from_str("{0} and {1} ", true, true).is_ok());
        assert!(ListJoinerPattern::from_str("{0} and {1} ", true, false).is_err());
        assert!(ListJoinerPattern::from_str(" {0} and {1}", true, true).is_ok());
        assert!(ListJoinerPattern::from_str(" {0} and {1}", false, true).is_err());
    }

    #[test]
    fn produces_correct_parts() {
        assert_eq!(
            test_patterns().pair(ListLength::Wide).parts(""),
            ("$", ";", "+")
        );
    }

    #[test]
    fn produces_correct_parts_conditionally() {
        assert_eq!(
            test_patterns().end(ListLength::Narrow).parts("A"),
            ("", " :o ", "")
        );
        assert_eq!(
            test_patterns().end(ListLength::Narrow).parts("a"),
            ("", " :o ", "")
        );
        assert_eq!(
            test_patterns().end(ListLength::Narrow).parts("ab"),
            ("", " :o ", "")
        );
        assert_eq!(
            test_patterns().end(ListLength::Narrow).parts("B"),
            ("", ". ", "")
        );
        assert_eq!(
            test_patterns().end(ListLength::Narrow).parts("BA"),
            ("", ". ", "")
        );
    }

    #[test]
    fn size_hint_works() {
        let pattern = test_patterns();

        assert_eq!(
            pattern.size_hint(ListLength::Short, 0),
            LengthHint::exact(0)
        );
        assert_eq!(
            pattern.size_hint(ListLength::Short, 1),
            LengthHint::exact(0)
        );

        // pair pattern "{0}123{1}456"
        assert_eq!(
            pattern.size_hint(ListLength::Short, 2),
            LengthHint::exact(6)
        );

        // patterns "{0}1{1}", "{0}12{1}" (x197), and "{0}12{1}34"
        assert_eq!(
            pattern.size_hint(ListLength::Short, 200),
            LengthHint::exact(1 + 2 * 197 + 4)
        );

        // patterns "{0}: {1}", "{0}, {1}" (x197), and "{0} :o {1}" or "{0}. {1}"
        assert_eq!(
            pattern.size_hint(ListLength::Narrow, 200),
            LengthHint::exact(2 + 197 * 2) + LengthHint::between(2, 4)
        );
    }

    #[test]
    fn databake() {
        databake::test_bake!(
            ListJoinerPattern,
            const: unsafe { crate::provider::ListJoinerPattern::from_parts_unchecked(", ", 2u8) },
            icu_list
        );
    }
}