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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
// License: see LICENSE file at root directory of `master` branch

//! # Case-insensitive string (owned)

use std::{
    cmp::Ordering,
    fmt,
    hash::{Hash, Hasher},
};

/// # Case-insensitive string (owned)
///
/// ## Notes
///
/// - Implementations of [`PartialEq`][rust::PartialEq], [`Ord`][rust::Ord], [`PartialOrd`][rust::PartialOrd], [`Hash`][rust::Hash] call
///   [`::to_lowercase()`][rust::to_lowercase()] on source string. So they are _slow_.
///
/// [rust::Eq]: https://doc.rust-lang.org/std/cmp/trait.Eq.html
/// [rust::PartialEq]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
/// [rust::Ord]: https://doc.rust-lang.org/std/cmp/trait.Ord.html
/// [rust::PartialOrd]: https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html
/// [rust::Hash]: https://doc.rust-lang.org/std/hash/trait.Hash.html
/// [rust::Display]: https://doc.rust-lang.org/std/fmt/trait.Display.html
/// [rust::to_lowercase()]: https://doc.rust-lang.org/std/string/struct.String.html#method.to_lowercase
#[derive(Debug, Eq)]
pub struct CainString {

    /// # Source string
    src: String,

}

impl PartialEq for CainString {

    fn eq(&self, other: &Self) -> bool {
        self.src.to_lowercase().eq(&other.src.to_lowercase())
    }

}

impl Ord for CainString {

    fn cmp(&self, other: &Self) -> Ordering {
        self.src.to_lowercase().cmp(&other.src.to_lowercase())
    }

}

impl PartialOrd for CainString {

    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }

}

impl Hash for CainString {

    fn hash<H>(&self, h: &mut H) where H: Hasher {
        self.src.to_lowercase().hash(h);
    }

}

impl fmt::Display for CainString {

    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        f.write_str(&self.src)
    }

}

impl From<String> for CainString {

    fn from(src: String) -> Self {
        Self { src }
    }

}

impl From<CainString> for String {

    fn from(cain_str: CainString) -> String {
        cain_str.src
    }

}

impl AsRef<str> for CainString {

    fn as_ref(&self) -> &str {
        &self.src
    }

}

impl AsMut<String> for CainString {

    fn as_mut(&mut self) -> &mut String {
        &mut self.src
    }

}