summaryrefslogtreecommitdiffstats
path: root/vendor/icu_locid/src/ordering.rs
blob: c877c60c3958380a27e9fc30bcba0945e86967d2 (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
// 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 ).

//! Utilities for performing ordering operations on locales.

use core::cmp::Ordering;

/// The result of a subtag iterator comparison operation.
///
/// See [`Locale::strict_cmp_iter`].
///
/// # Examples
///
/// Check whether a stream of subtags contains two expected locales back-to-back:
///
/// ```
/// use icu::locid::{locale, Locale, SubtagOrderingResult};
/// use std::cmp::Ordering;
///
/// let subtags = b"en-US-it-IT".split(|b| *b == b'-');
/// let locales = [locale!("en-US"), locale!("it-IT")];
/// let mut result = SubtagOrderingResult::Subtags(subtags);
/// for loc in locales.iter() {
///     match result {
///         SubtagOrderingResult::Subtags(it) => {
///             result = loc.strict_cmp_iter(it);
///         }
///         SubtagOrderingResult::Ordering(ord) => break,
///     }
/// }
///
/// assert_eq!(Ordering::Equal, result.end());
/// ```
///
/// [`Locale::strict_cmp_iter`]: crate::Locale::strict_cmp_iter
#[allow(clippy::exhaustive_enums)] // well-defined exhaustive enum semantics
#[derive(Debug)]
pub enum SubtagOrderingResult<I> {
    /// Potentially remaining subtags after the comparison operation.
    Subtags(I),
    /// Resolved ordering between the locale object and the subtags.
    Ordering(Ordering),
}

impl<I> SubtagOrderingResult<I>
where
    I: Iterator,
{
    /// Invoke this function if there are no remaining locale objects to chain in order to get
    /// a fully resolved [`Ordering`].
    #[inline]
    pub fn end(self) -> Ordering {
        match self {
            Self::Subtags(mut it) => match it.next() {
                Some(_) => Ordering::Less,
                None => Ordering::Equal,
            },
            Self::Ordering(o) => o,
        }
    }
}