nu_protocol/
casing.rs

1use std::cmp::Ordering;
2
3use nu_utils::IgnoreCaseExt;
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
7pub enum Casing {
8    #[default]
9    Sensitive,
10    Insensitive,
11}
12
13pub(crate) mod private {
14    pub trait Seal {}
15}
16
17pub trait CaseSensitivity: private::Seal + 'static {
18    fn eq(lhs: &str, rhs: &str) -> bool;
19
20    fn cmp(lhs: &str, rhs: &str) -> Ordering;
21}
22
23pub struct CaseSensitive;
24pub struct CaseInsensitive;
25
26impl private::Seal for CaseSensitive {}
27impl private::Seal for CaseInsensitive {}
28
29impl CaseSensitivity for CaseSensitive {
30    #[inline]
31    fn eq(lhs: &str, rhs: &str) -> bool {
32        lhs == rhs
33    }
34
35    #[inline]
36    fn cmp(lhs: &str, rhs: &str) -> Ordering {
37        lhs.cmp(rhs)
38    }
39}
40
41impl CaseSensitivity for CaseInsensitive {
42    #[inline]
43    fn eq(lhs: &str, rhs: &str) -> bool {
44        lhs.eq_ignore_case(rhs)
45    }
46
47    #[inline]
48    fn cmp(lhs: &str, rhs: &str) -> Ordering {
49        lhs.cmp_ignore_case(rhs)
50    }
51}
52
53/// Wraps `Self` in a type that affects the case sensitivity of operations
54///
55/// Using methods of [`CaseSensitivity`] in `Wrapper` implementations are not mandotary.
56/// They are provided mostly for convenience and to have a common implementation for comparisons
57pub trait WrapCased {
58    /// Wrapper type generic over case sensitivity.
59    type Wrapper<S: CaseSensitivity>;
60
61    fn case_sensitive(self) -> Self::Wrapper<CaseSensitive>;
62    fn case_insensitive(self) -> Self::Wrapper<CaseInsensitive>;
63}