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 ).
5 //! Utilities for performing ordering operations on locales.
7 use core::cmp::Ordering;
9 /// The result of a subtag iterator comparison operation.
11 /// See [`Locale::strict_cmp_iter`].
15 /// Check whether a stream of subtags contains two expected locales back-to-back:
18 /// use icu::locid::{locale, Locale, SubtagOrderingResult};
19 /// use std::cmp::Ordering;
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() {
26 /// SubtagOrderingResult::Subtags(it) => {
27 /// result = loc.strict_cmp_iter(it);
29 /// SubtagOrderingResult::Ordering(ord) => break,
33 /// assert_eq!(Ordering::Equal, result.end());
36 /// [`Locale::strict_cmp_iter`]: crate::Locale::strict_cmp_iter
37 #[allow(clippy::exhaustive_enums)] // well-defined exhaustive enum semantics
39 pub enum SubtagOrderingResult<I> {
40 /// Potentially remaining subtags after the comparison operation.
42 /// Resolved ordering between the locale object and the subtags.
46 impl<I> SubtagOrderingResult<I>
50 /// Invoke this function if there are no remaining locale objects to chain in order to get
51 /// a fully resolved [`Ordering`].
53 pub fn end(self) -> Ordering {
55 Self::Subtags(mut it) => match it.next() {
56 Some(_) => Ordering::Less,
57 None => Ordering::Equal,
59 Self::Ordering(o) => o,