Skip to content

Commit

Permalink
feat: escape json
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 9, 2024
1 parent 8e1678a commit 50070b7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
clap = { version = "4.5.17", features = ["derive"] }
hex = "0.4"
serde_json = "1.0.128"
thiserror = "1.0.64"

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fooe
fooi
llee
llleee
ñandú
spame
spamee
tempdir
Expand Down
35 changes: 31 additions & 4 deletions src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,31 @@ mod tests {
assert_eq!(to_json(b"1:9"), r#""9""#.to_string());
}

/* todo:
- String containing special chars like : `"`, `\`, '\\'
- String containing JSON
*/
mod should_escape_json {
use crate::parsers::tests::{to_bencode, to_json};

#[test]
fn containing_a_double_quote() {
assert_eq!(to_json("1:\"".as_bytes()), r#""\"""#.to_string());
}

#[test]
fn containing_backslashes() {
assert_eq!(to_json("1:\\".as_bytes()), r#""\\""#.to_string());
}

#[test]
fn containing_control_characters() {
assert_eq!(to_json("1:\n".as_bytes()), r#""\n""#.to_string());
assert_eq!(to_json("1:\r".as_bytes()), r#""\r""#.to_string());
assert_eq!(to_json("1:\t".as_bytes()), r#""\t""#.to_string());
}

#[test]
fn containing_unicode_characters() {
assert_eq!(to_json(&to_bencode("ñandú")), r#""ñandú""#.to_string());
}
}

mod it_should_fail_parsing_when {
use crate::parsers::{error::Error, tests::parse};
Expand Down Expand Up @@ -735,6 +756,12 @@ mod tests {
output
}

/// Helper to convert a string into a bencoded string.
fn to_bencode(value: &str) -> Vec<u8> {
let bencoded_str = format!("{}:{}", value.len(), value);
bencoded_str.as_bytes().to_vec()
}

/// Wrapper to easily use the parser in tests
fn parse(input_buffer: &[u8]) -> Result<String, error::Error> {
let mut output = String::new();
Expand Down
11 changes: 3 additions & 8 deletions src/parsers/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ impl StringParser {
self.parsed_value.clone()
}

/// It adds the double quotes to the string it's the JSON delimiter for
/// string values.
/// It serializes the parsed value into JSON.
#[must_use]
fn json(&self) -> String {
format!("\"{}\"", self.parsed_value())
serde_json::to_string(&self.parsed_value()).unwrap()
}
}

Expand Down Expand Up @@ -184,11 +183,7 @@ impl Value {

fn parse<R: Read>(&mut self, reader: &mut ByteReader<R>) -> Result<(), Error> {
for _i in 1..=self.length {
let byte = Self::next_byte(reader)?;

self.add_byte(byte);

// todo: escape '"' and '\\' with '\\';
self.add_byte(Self::next_byte(reader)?);
}

Ok(())
Expand Down

0 comments on commit 50070b7

Please sign in to comment.