Skip to content

Commit

Permalink
fix parsing OWS around parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Jan 16, 2019
1 parent 6eae72d commit 2e0268e
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 69 deletions.
25 changes: 23 additions & 2 deletions mime-parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,29 @@ pub enum ParseError {
MissingQuote,
InvalidToken {
pos: usize,
byte: u8,
byte: Byte,
},
InvalidRange,
TooLong,
}

#[derive(Clone, Copy)]
pub struct Byte(u8);

impl fmt::Debug for Byte {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
b'\n' => f.write_str("'\\n'"),
b'\r' => f.write_str("'\\r'"),
b'\t' => f.write_str("'\\t'"),
b'\\' => f.write_str("'\\'"),
b'\0' => f.write_str("'\\0'"),
0x20...0x7f => write!(f, "'{}'", self.0 as char),
_ => write!(f, "'\\x{:02x}'", self.0),
}
}
}

impl Error for ParseError {
fn description(&self) -> &str {
match self {
Expand All @@ -85,7 +102,7 @@ impl Error for ParseError {
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let ParseError::InvalidToken { pos, byte } = *self {
write!(f, "{}, {:X} at position {}", self.description(), byte, pos)
write!(f, "{}, {:?} at position {}", self.description(), byte, pos)
} else {
f.write_str(self.description())
}
Expand Down Expand Up @@ -145,6 +162,10 @@ impl Mime {
&self.params
}

pub fn param<'a>(&'a self, attr: &str) -> Option<&'a str> {
self.params().find(|e| attr == e.0).map(|e| e.1)
}

#[inline]
pub fn has_params(&self) -> bool {
self.semicolon().is_some()
Expand Down
Loading

0 comments on commit 2e0268e

Please sign in to comment.