diff --git a/src/lib.rs b/src/lib.rs index 4cf2fbe5..f485d4c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,7 +38,7 @@ use std::str::FromStr; mod parse; /// A parsed mime or media type. -#[derive(Clone)] +#[derive(Clone, PartialEq, Eq)] pub struct Mime { source: Source, slash: usize, @@ -99,7 +99,7 @@ impl Error for FromStrError { } } -#[derive(Clone)] +#[derive(Clone, PartialEq, Eq)] enum Source { Atom(u8, &'static str), Dynamic(String), @@ -114,14 +114,17 @@ impl Source { } } -#[derive(Clone)] +#[derive(Clone, PartialEq, Eq)] enum ParamSource { Utf8(usize), - Custom(usize, Vec<(Indexed, Indexed)>), + Custom(usize, IndexedCollection), None, } -#[derive(Clone, Copy)] +#[derive(Clone, PartialEq, Eq)] +struct IndexedCollection(Vec<(Indexed, Indexed)>); + +#[derive(Clone, Copy, PartialEq, Eq)] struct Indexed(usize, usize); impl Mime { @@ -210,7 +213,7 @@ impl Mime { ParamSource::Utf8(_) => ParamsInner::Utf8, ParamSource::Custom(_, ref params) => ParamsInner::Custom { source: &self.source, - params: params.iter(), + params: params.0.iter(), }, ParamSource::None => ParamsInner::None, }; @@ -243,6 +246,7 @@ impl Mime { } } + #[allow(dead_code)] fn atom(&self) -> u8 { match self.source { Source::Atom(a, _) => a, @@ -373,23 +377,6 @@ fn params_eq(semicolon: usize, a: &str, b: &str) -> bool { } } -impl PartialEq for Mime { - #[inline] - fn eq(&self, other: &Mime) -> bool { - match (self.atom(), other.atom()) { - // TODO: - // This could optimize for when there are no customs parameters. - // Any parsed mime has already been lowercased, so if there aren't - // any parameters that are case sensistive, this can skip the - // eq_ascii, and just use a memcmp instead. - (0, _) | (_, 0) => mime_eq_str(self, other.source.as_ref()), - (a, b) => a == b, - } - } -} - -impl Eq for Mime {} - impl PartialOrd for Mime { fn partial_cmp(&self, other: &Mime) -> Option { Some(self.cmp(other)) diff --git a/src/parse.rs b/src/parse.rs index 3cdb5f07..38ed44c5 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,3 +1,5 @@ +use super::{Indexed, Mime, MimeIter, ParamSource, Source, CHARSET, UTF_8}; +use crate::IndexedCollection; #[allow(unused, deprecated)] use std::ascii::AsciiExt; use std::error::Error; @@ -5,8 +7,6 @@ use std::fmt; use std::iter::Enumerate; use std::str::Bytes; -use super::{Indexed, Mime, MimeIter, ParamSource, Source, CHARSET, UTF_8}; - #[derive(Debug)] pub enum ParseError { MissingSlash, @@ -164,7 +164,7 @@ pub fn parse(s: &str) -> Result { let src = match params { ParamSource::Utf8(_) => s.to_ascii_lowercase(), ParamSource::Custom(semicolon, ref indices) => { - lower_ascii_with_params(s, semicolon, indices) + lower_ascii_with_params(s, semicolon, indices.0.as_slice()) } ParamSource::None => { // Chop off the empty list @@ -292,10 +292,13 @@ fn params_from_str( let i = i + 2; let charset = Indexed(i, "charset".len() + i); let utf8 = Indexed(charset.1 + 1, charset.1 + "utf-8".len() + 1); - params = ParamSource::Custom(semicolon, vec![(charset, utf8), (name, value)]); + params = ParamSource::Custom( + semicolon, + IndexedCollection(vec![(charset, utf8), (name, value)]), + ); } ParamSource::Custom(_, ref mut vec) => { - vec.push((name, value)); + vec.0.push((name, value)); } ParamSource::None => { if semicolon + 2 == name.0 && CHARSET == &s[name.0..name.1] { @@ -304,7 +307,7 @@ fn params_from_str( continue 'params; } } - params = ParamSource::Custom(semicolon, vec![(name, value)]); + params = ParamSource::Custom(semicolon, IndexedCollection(vec![(name, value)])); } } }