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

Handle escapes #23

Merged
merged 3 commits into from
Jul 31, 2017
Merged
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
2 changes: 1 addition & 1 deletion examples/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {
float: 2.18,
map: HashMap::from_iter(vec![(0, '1'), (1, '2'), (3, '5'), (8, '1')]),
nested: Nested {
a: "Hello from RON".to_string(),
a: "Hello from \"RON\"".to_string(),
b: 'b',
},
}).expect("Serialization failed");
Expand Down
35 changes: 31 additions & 4 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ pub enum Error {
ExpectedMapEnd,
ExpectedStruct,
ExpectedStructEnd,
ExpectetUnit,
ExpectedUnit,
ExpectedStructName,
ExpectedString,
ExpectedIdentifier,

InvalidEscape,

/// A custom error emitted by the deserializer.
Message(String),
TrailingCharacters,
Expand Down Expand Up @@ -228,9 +230,24 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>
{
let parser = sym(b'\'') * take(1) - sym(b'\'');
let parser = sym(b'\'') * take(1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is most likely a better way to do this with POM, but it didn't work for me and this is somehow easier to read. I wonder if there is a parser library that is fast and nice to read.

match parser.parse(&mut self.input) {
Ok(c) => visitor.visit_char(c[0] as char),
Ok(c) => {
let rv = if c[0] == b'\\' {
match take(1).parse(&mut self.input) {
Ok(ref c) if c[0] == b'\'' => visitor.visit_char('\''),
Ok(ref c) if c[0] == b'\\' => visitor.visit_char('\\'),
Ok(_) => Err(Error::InvalidEscape),
Err(_) => Err(Error::InvalidEscape),
}
} else {
visitor.visit_char(c[0] as char)
};

sym(b'\'').parse(&mut self.input).map_err(|_| Error::ExpectedChar)?;

rv
},
Err(_) => Err(Error::ExpectedChar)
}
}
Expand Down Expand Up @@ -296,7 +313,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
{
match self.consume("()") {
Ok(_) => visitor.visit_unit(),
Err(_) => Err(Error::ExpectetUnit),
Err(_) => Err(Error::ExpectedUnit),
}
}

Expand Down Expand Up @@ -692,4 +709,14 @@ mod tests {
fn test_char() {
assert_eq!(Ok('c'), from_str("'c'"));
}

#[test]
fn test_escape_char() {
assert_eq!('\'', from_str::<char>("'\\''").unwrap());
}

#[test]
fn test_escape() {
assert_eq!("\"Quoted\"", from_str::<String>(r#""\"Quoted\"""#).unwrap());
}
}
15 changes: 14 additions & 1 deletion src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,22 @@ impl<'a> ser::Serializer for &'a mut Serializer {

fn serialize_char(self, v: char) -> Result<()> {
self.output += "'";
if v == '\\' || v == '\'' {
self.output.push('\\');
}
self.output.push(v);
self.output += "'";
Ok(())
}

fn serialize_str(self, v: &str) -> Result<()> {
self.output += "\"";
self.output += v;
for char in v.chars() {
if char == '\\' || char == '"' {
self.output.push('\\');
}
self.output.push(char);
}
self.output += "\"";
Ok(())
}
Expand Down Expand Up @@ -512,4 +520,9 @@ mod tests {
fn test_char() {
assert_eq!(to_string(&'c').unwrap(), "'c'");
}

#[test]
fn test_escape() {
assert_eq!(to_string(&r#""Quoted""#).unwrap(), r#""\"Quoted\"""#);
}
}