Skip to content

Commit

Permalink
feat: allow line breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 7, 2024
1 parent 643aa09 commit 0e793a0
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ impl<R: Read> BencodeParser<R> {
// End of list or dictionary (not end of integer)
self.end_bencoded_value(writer)?;
}
b'\n' => {
// Ignore line breaks at the beginning, the end or between values
}
_ => {
panic!("{}", format!("unexpected byte {} ({})", byte, byte as char));
}
Expand Down Expand Up @@ -272,6 +275,45 @@ mod tests {
assert_eq!(output, String::new());
}

#[test]
fn it_should_allow_line_breaks_at_the_beginning_of_the_input_stream() {
let mut output = String::new();

let mut parser = BencodeParser::new(&b"\ni0e"[..]);

parser
.write_str(&mut output)
.expect("Bencode to JSON conversion failed");

assert_eq!(output, "0".to_string());
}

#[test]
fn it_should_allow_line_breaks_at_the_end_of_the_input_stream() {
let mut output = String::new();

let mut parser = BencodeParser::new(&b"i0e\n"[..]);

parser
.write_str(&mut output)
.expect("Bencode to JSON conversion failed");

assert_eq!(output, "0".to_string());
}

#[test]
fn it_should_allow_line_breaks_between_bencoded_values() {
let mut output = String::new();

let mut parser = BencodeParser::new(&b"li0e\ni1ee"[..]);

parser
.write_str(&mut output)
.expect("Bencode to JSON conversion failed");

assert_eq!(output, "[0,1]".to_string());
}

mod integers {
use crate::parsers::tests::to_json;

Expand Down

0 comments on commit 0e793a0

Please sign in to comment.