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 | //! Utilities for performing ordering operations on locales. |
6 | |
7 | use core::cmp::Ordering; |
8 | |
9 | /// The result of a subtag iterator comparison operation. |
10 | /// |
11 | /// See [`Locale::strict_cmp_iter`]. |
12 | /// |
13 | /// # Examples |
14 | /// |
15 | /// Check whether a stream of subtags contains two expected locales back-to-back: |
16 | /// |
17 | /// ``` |
18 | /// use icu::locid::{locale, Locale, SubtagOrderingResult}; |
19 | /// use std::cmp::Ordering; |
20 | /// |
21 | /// let subtags = b"en-US-it-IT" .split(|b| *b == b'-' ); |
22 | /// let locales = [locale!("en-US" ), locale!("it-IT" )]; |
23 | /// let mut result = SubtagOrderingResult::Subtags(subtags); |
24 | /// for loc in locales.iter() { |
25 | /// match result { |
26 | /// SubtagOrderingResult::Subtags(it) => { |
27 | /// result = loc.strict_cmp_iter(it); |
28 | /// } |
29 | /// SubtagOrderingResult::Ordering(ord) => break, |
30 | /// } |
31 | /// } |
32 | /// |
33 | /// assert_eq!(Ordering::Equal, result.end()); |
34 | /// ``` |
35 | /// |
36 | /// [`Locale::strict_cmp_iter`]: crate::Locale::strict_cmp_iter |
37 | #[allow (clippy::exhaustive_enums)] // well-defined exhaustive enum semantics |
38 | #[derive (Debug)] |
39 | pub enum SubtagOrderingResult<I> { |
40 | /// Potentially remaining subtags after the comparison operation. |
41 | Subtags(I), |
42 | /// Resolved ordering between the locale object and the subtags. |
43 | Ordering(Ordering), |
44 | } |
45 | |
46 | impl<I> SubtagOrderingResult<I> |
47 | where |
48 | I: Iterator, |
49 | { |
50 | /// Invoke this function if there are no remaining locale objects to chain in order to get |
51 | /// a fully resolved [`Ordering`]. |
52 | #[inline ] |
53 | pub fn end(self) -> Ordering { |
54 | match self { |
55 | Self::Subtags(mut it: I) => match it.next() { |
56 | Some(_) => Ordering::Less, |
57 | None => Ordering::Equal, |
58 | }, |
59 | Self::Ordering(o: Ordering) => o, |
60 | } |
61 | } |
62 | } |
63 | |