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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/// A single HeaderValue that can hold Data
/// in a variety of forms allowing for easier
/// and more flexible use
#[derive(Debug, PartialEq, Clone)]
pub enum HeaderValue<'a> {
    /// Stores the Value as a reference to a String
    StrRef(&'a str),
    /// Stores the Value as an owned String
    Str(String),
    /// Stores the Value in its raw Number format
    NumberUsize(usize),
}

impl<'a> Into<HeaderValue<'a>> for &'a str {
    fn into(self) -> HeaderValue<'a> {
        HeaderValue::StrRef(self)
    }
}
impl<'a> Into<HeaderValue<'a>> for String {
    fn into(self) -> HeaderValue<'a> {
        HeaderValue::Str(self)
    }
}
impl<'a> Into<HeaderValue<'a>> for usize {
    fn into(self) -> HeaderValue<'a> {
        HeaderValue::NumberUsize(self)
    }
}

impl<'a> HeaderValue<'a> {
    /// Serializes the Value into the given Buffer by
    /// appending the Data to it
    pub fn serialize(&self, buf: &mut Vec<u8>) {
        match *self {
            Self::StrRef(ref value) => {
                buf.extend_from_slice(value.as_bytes());
            }
            Self::Str(ref value) => {
                buf.extend_from_slice(value.as_bytes());
            }
            Self::NumberUsize(ref value) => {
                buf.extend_from_slice(value.to_string().as_bytes());
            }
        }
    }

    /// Turns the given Value, regardless of how it is stored,
    /// into an owned String
    pub fn to_string(&self) -> String {
        match *self {
            Self::StrRef(ref value) => value.to_string(),
            Self::Str(ref value) => value.clone(),
            Self::NumberUsize(ref value) => value.to_string(),
        }
    }

    /// Compares the Two values without case
    ///
    /// Any number type in either of them immediately
    /// returns false
    pub fn eq_ignore_case(&self, other: &Self) -> bool {
        let own_ref = match self.try_as_str_ref() {
            Some(r) => r,
            None => return false,
        };

        let other_ref = match other.try_as_str_ref() {
            Some(r) => r,
            None => return false,
        };

        caseless::default_caseless_match_str(own_ref, other_ref)
    }

    /// Tries to return a reference to the underlying String,
    /// if it is a String, otherwise returns None
    pub fn try_as_str_ref(&self) -> Option<&str> {
        match self {
            Self::StrRef(value) => Some(value),
            Self::Str(value) => Some(&value),
            Self::NumberUsize(_) => None,
        }
    }
}

impl PartialEq<std::string::String> for HeaderValue<'_> {
    fn eq(&self, other: &std::string::String) -> bool {
        match *self {
            Self::StrRef(ref value) => value == other,
            Self::Str(ref value) => value == other,
            _ => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn serialize_str() {
        let mut result: Vec<u8> = Vec::new();
        HeaderValue::Str("test-value".to_owned()).serialize(&mut result);

        assert_eq!("test-value".as_bytes(), &result);
    }
    #[test]
    fn serialize_str_ref() {
        let mut result: Vec<u8> = Vec::new();
        HeaderValue::StrRef("test-value").serialize(&mut result);

        assert_eq!("test-value".as_bytes(), &result);
    }
    #[test]
    fn serialize_number_usize() {
        let mut result: Vec<u8> = Vec::new();
        HeaderValue::NumberUsize(80).serialize(&mut result);

        assert_eq!("80".as_bytes(), &result);
    }

    #[test]
    fn equals_ignore_case() {
        assert_eq!(
            true,
            HeaderValue::StrRef("test").eq_ignore_case(&HeaderValue::StrRef("TEST"))
        );
        assert_eq!(
            true,
            HeaderValue::StrRef("test").eq_ignore_case(&HeaderValue::StrRef("test"))
        );
        assert_eq!(
            true,
            HeaderValue::StrRef("TeSt").eq_ignore_case(&HeaderValue::StrRef("test"))
        );
    }
}