| 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, 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 | #[deprecated (since = "1.5.0" , note = "if you need this, please file an issue" )] |
| 40 | pub enum SubtagOrderingResult<I> { |
| 41 | /// Potentially remaining subtags after the comparison operation. |
| 42 | #[deprecated (since = "1.5.0" , note = "if you need this, please file an issue" )] |
| 43 | Subtags(I), |
| 44 | /// Resolved ordering between the locale object and the subtags. |
| 45 | #[deprecated (since = "1.5.0" , note = "if you need this, please file an issue" )] |
| 46 | Ordering(Ordering), |
| 47 | } |
| 48 | |
| 49 | #[allow (deprecated)] |
| 50 | impl<I> SubtagOrderingResult<I> |
| 51 | where |
| 52 | I: Iterator, |
| 53 | { |
| 54 | /// Invoke this function if there are no remaining locale objects to chain in order to get |
| 55 | /// a fully resolved [`Ordering`]. |
| 56 | #[inline ] |
| 57 | pub fn end(self) -> Ordering { |
| 58 | match self { |
| 59 | Self::Subtags(mut it: I) => match it.next() { |
| 60 | Some(_) => Ordering::Less, |
| 61 | None => Ordering::Equal, |
| 62 | }, |
| 63 | Self::Ordering(o: Ordering) => o, |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |