Skip to content

Commit

Permalink
feat: error strign length parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 8, 2024
1 parent 99a0da2 commit 12745ee
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
11 changes: 8 additions & 3 deletions src/parsers/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,22 @@ pub enum Error {
UnexpectedEndOfInputParsingInteger,

// Strings
#[error("Invalid string length byte, expected a digit: {0}, char: {1}")]
InvalidStringLengthByte(u8, char),

#[error("Unexpected end of input parsing string length")]
UnexpectedEndOfInputParsingStringLength,

#[error("Unexpected end of input parsing string value")]
UnexpectedEndOfInputParsingStringValue,

#[error("Unexpected end of list or dict. No matching start for the list or dict end")]
NoMatchingStartForListOrDictEnd,

// Dictionaries
#[error("Unexpected end of dictionary. Premature end of dictionary")]
PrematureEndOfDict,

// List and dictionaries
#[error("Unexpected end of list or dict. No matching start for the list or dict end")]
NoMatchingStartForListOrDictEnd,
}

impl From<std::io::Error> for Error {
Expand Down
55 changes: 34 additions & 21 deletions src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,7 @@ mod tests {
}

mod strings {
use crate::parsers::{
error::Error,
tests::{parse, to_json},
};
use crate::parsers::tests::to_json;

#[test]
fn empty_string() {
Expand Down Expand Up @@ -417,28 +414,44 @@ mod tests {
- String containing JSON
*/

#[test]
fn it_should_fail_when_it_reaches_the_end_of_input_parsing_the_string_length() {
let incomplete_string_length = b"4";
mod it_should_fail_parsing_when {
use crate::parsers::{error::Error, tests::parse};

let result = parse(incomplete_string_length);
#[test]
fn it_reaches_the_end_of_the_input_parsing_the_string_length() {
let incomplete_string_length = b"4";

assert!(matches!(
result,
Err(Error::UnexpectedEndOfInputParsingStringLength)
));
}
let result = parse(incomplete_string_length);

#[test]
fn it_should_fail_when_it_reaches_the_end_of_input_parsing_the_string_value() {
let incomplete_string_value = b"4:123";
assert!(matches!(
result,
Err(Error::UnexpectedEndOfInputParsingStringLength)
));
}

let result = parse(incomplete_string_value);
#[test]
fn it_reaches_the_end_of_the_input_parsing_the_string_value() {
let incomplete_string_value = b"4:123";

assert!(matches!(
result,
Err(Error::UnexpectedEndOfInputParsingStringValue)
));
let result = parse(incomplete_string_value);

assert!(matches!(
result,
Err(Error::UnexpectedEndOfInputParsingStringValue)
));
}

#[test]
fn it_receives_a_non_digit_byte_in_the_string_length() {
let incomplete_string_value = b"4a:1234";

let result = parse(incomplete_string_value);

assert!(matches!(
result,
Err(Error::InvalidStringLengthByte(b'a', 'a'))
));
}
}
}

Expand Down
19 changes: 14 additions & 5 deletions src/parsers/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,19 @@ impl Length {
2. Convert next digit and increase temp_length: temp_length = temp_length * 10 + new_digit.
*/

fn add_byte(&mut self, byte: u8) {
// todo: should we fail here is the byte is not a digit (0..9)?
// or we can wait until we try to convert all bytes in the into a number?
/// It adds a new byte (digit) to the string length.
///
/// # Errors
///
/// Will return an error if the byte is not a digit (0..9).
fn add_byte(&mut self, byte: u8) -> Result<(), Error> {
if !byte.is_ascii_digit() {
return Err(Error::InvalidStringLengthByte(byte, byte as char));
}

self.bytes.push(byte);

Ok(())
}

/// This function convert the current bytes representing the length to a
Expand Down Expand Up @@ -142,7 +151,7 @@ impl StringParser {
) -> Result<(), Error> {
// code-review: length can be calculated on the fly as the original C implementation.

self.length.add_byte(initial_byte);
self.length.add_byte(initial_byte)?;

loop {
let byte = match reader.read_byte() {
Expand All @@ -166,7 +175,7 @@ impl StringParser {
break;
}
_ => {
self.length.add_byte(byte);
self.length.add_byte(byte)?;
}
}
}
Expand Down

0 comments on commit 12745ee

Please sign in to comment.