Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
torkleyy committed Oct 4, 2017
1 parent e4eab5f commit db117a3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/de/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub enum ParseError {
ExpectedUnit,
ExpectedStructName,
ExpectedString,
ExpectedStringEnd,
ExpectedIdentifier,

InvalidEscape,
Expand Down
13 changes: 13 additions & 0 deletions src/de/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,16 @@ fn untagged() {

assert_eq!(from_str::<Untagged>("true").unwrap(), Untagged::Bool(true));
}

#[test]
fn forgot_apostrophes() {
let de: Result<(i32, String)> = from_str("(4, \"Hello)");

assert!({
if let Err(Error::Parser(ParseError::ExpectedStringEnd, _)) = de {
true
} else {
false
}
});
}
14 changes: 7 additions & 7 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ impl<'a> Bytes<'a> {
}

pub fn next_bytes_contained_in(&self, allowed: &[u8]) -> usize {
(0..self.bytes.len())
.flat_map(|i| self.bytes.get(i))
self.bytes
.iter()
.take_while(|b| allowed.contains(b))
.fold(0, |acc, _| acc + 1)
}
Expand Down Expand Up @@ -224,11 +224,11 @@ impl<'a> Bytes<'a> {
return self.err(ParseError::ExpectedString);
}

let (i, end_or_escape) = (0..)
.flat_map(|i| self.bytes.get(i))
let (i, end_or_escape) = self.bytes
.iter()
.enumerate()
.find(|&(_, &b)| b == b'\\' || b == b'"')
.ok_or(self.error(ParseError::Eof))?;
.ok_or(self.error(ParseError::ExpectedStringEnd))?;

if *end_or_escape == b'"' {
let s = from_utf8(&self.bytes[..i]).map_err(|e| self.error(e.into()))?;
Expand All @@ -246,8 +246,8 @@ impl<'a> Bytes<'a> {
let _ = self.advance(i + 1);
self.parse_str_escape(&mut s)?;

let (new_i, end_or_escape) = (0..)
.flat_map(|i| self.bytes.get(i))
let (new_i, end_or_escape) = self.bytes
.iter()
.enumerate()
.find(|&(_, &b)| b == b'\\' || b == b'"')
.ok_or(ParseError::Eof)
Expand Down

0 comments on commit db117a3

Please sign in to comment.