Skip to content

Commit

Permalink
impl Error for text::ReaderError
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbabcock committed Dec 21, 2023
1 parent c7c59a4 commit f2c4bd3
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/text/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,31 @@ pub struct ReaderError {
kind: ReaderErrorKind,
}

impl std::fmt::Display for ReaderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.kind() {
ReaderErrorKind::Read { .. } => {
write!(f, "failed to read past position: {}", self.position)
}
ReaderErrorKind::BufferFull => {
write!(f, "max buffer size exceeded at position: {}", self.position)
}
ReaderErrorKind::Eof => {
write!(f, "unexpected end of file at position: {}", self.position)
}
}
}
}

impl std::error::Error for ReaderError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self.kind() {
ReaderErrorKind::Read(x) => Some(x),
_ => None,
}
}
}

impl ReaderError {
/// Return the byte position where the error occurred
pub fn position(&self) -> usize {
Expand Down

0 comments on commit f2c4bd3

Please sign in to comment.