use std::cmp::PartialEq;
use std::fmt;
use unicase;
#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct Name<'a> {
pub(crate) source: &'a str,
}
impl<'a> Name<'a> {
pub fn as_str(&self) -> &'a str {
self.source
}
}
impl<'a> PartialEq<str> for Name<'a> {
#[inline]
fn eq(&self, other: &str) -> bool {
unicase::eq_ascii(self.source, other)
}
}
impl<'a, 'b> PartialEq<&'b str> for Name<'a> {
#[inline]
fn eq(&self, other: & &'b str) -> bool {
self == *other
}
}
impl<'a> PartialEq<Name<'a>> for str {
#[inline]
fn eq(&self, other: &Name<'a>) -> bool {
other == self
}
}
impl<'a, 'b> PartialEq<Name<'a>> for &'b str {
#[inline]
fn eq(&self, other: &Name<'a>) -> bool {
other == self
}
}
impl<'a> AsRef<str> for Name<'a> {
#[inline]
fn as_ref(&self) -> &str {
self.source
}
}
impl<'a> From<Name<'a>> for &'a str {
#[inline]
fn from(name: Name<'a>) -> &'a str {
name.source
}
}
impl<'a> fmt::Debug for Name<'a> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self.source, f)
}
}
impl<'a> fmt::Display for Name<'a> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self.source, f)
}
}
#[cfg(test)]
mod test {
use std::str::FromStr;
use super::Name;
use super::super::Mime;
#[test]
fn test_name_eq_str() {
let param = Name { source: "ABC" };
assert_eq!(param, param);
assert_eq!(param, "ABC");
assert_eq!("ABC", param);
assert_eq!(param, "abc");
assert_eq!("abc", param);
}
#[test]
fn test_name_eq_name() {
let mime1 = Mime::from_str(r#"text/x-custom; abc=a"#).unwrap();
let mime2 = Mime::from_str(r#"text/x-custom; aBc=a"#).unwrap();
let param1 = mime1.params().next().unwrap().0;
let param2 = mime2.params().next().unwrap().0;
assert_eq!(param1, param2);
}
}