Skip to content

Commit

Permalink
custom error for end of input parsing integer
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 8, 2024
1 parent 09615e7 commit 2bcfd62
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/parsers/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ pub enum Error {
#[error("R/W error: {0}")]
Rw(#[from] rw::error::Error),

#[error("Unrecognized first byte for new bencoded value: {0}, char: {1}")]
UnrecognizedFirstBencodeValueByte(u8, char),

#[error("Unexpected byte parsing integer: {0}, char: {1}")]
UnexpectedByteParsingInteger(u8, char),

#[error("Unrecognized first byte for new bencoded value: {0}, char: {1}")]
UnrecognizedFirstBencodeValueByte(u8, char),
#[error("Unexpected end of input parsing integer")]
UnexpectedEndOfInputParsingInteger,

#[error("Unexpected end of list or dict. No matching start for the list or dict end")]
NoMatchingStartForListOrDictEnd,
Expand Down
12 changes: 10 additions & 2 deletions src/parsers/integer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Bencoded integer parser.
//!
//! It reads bencoded bytes from the input and writes JSON bytes to the output.
use std::io::Read;
use std::io::{self, Read};

use crate::rw::{byte_reader::ByteReader, writer::Writer};

Expand Down Expand Up @@ -39,7 +39,15 @@ pub fn parse<R: Read, W: Writer>(
loop {
let byte = match reader.read_byte() {
Ok(byte) => byte,
Err(err) => return Err(err.into()),
Err(err) => match err {
crate::rw::error::Error::Io(ref io_error) => {
if io_error.kind() == io::ErrorKind::UnexpectedEof {
return Err(Error::UnexpectedEndOfInputParsingInteger);
}
return Err(err.into());
}
crate::rw::error::Error::Fmt(_fmt_error) => return Err(err.into()),
},
};

let char = byte as char;
Expand Down

0 comments on commit 2bcfd62

Please sign in to comment.