Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mime partial eq to use in pattern match #152

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 10 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -99,7 +99,7 @@ impl Error for FromStrError {
}
}

#[derive(Clone)]
#[derive(Clone, PartialEq, Eq)]
enum Source {
Atom(u8, &'static str),
Dynamic(String),
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -243,6 +246,7 @@ impl Mime {
}
}

#[allow(dead_code)]
fn atom(&self) -> u8 {
match self.source {
Source::Atom(a, _) => a,
Expand Down Expand Up @@ -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()),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed.

(a, b) => a == b,
}
}
}

impl Eq for Mime {}

impl PartialOrd for Mime {
fn partial_cmp(&self, other: &Mime) -> Option<Ordering> {
Some(self.cmp(other))
Expand Down
15 changes: 9 additions & 6 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::{Indexed, Mime, MimeIter, ParamSource, Source, CHARSET, UTF_8};
use crate::IndexedCollection;
#[allow(unused, deprecated)]
use std::ascii::AsciiExt;
use std::error::Error;
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,
Expand Down Expand Up @@ -164,7 +164,7 @@ pub fn parse(s: &str) -> Result<Mime, ParseError> {
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
Expand Down Expand Up @@ -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] {
Expand All @@ -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)]));
}
}
}
Expand Down