diff --git a/src/uu/wc/src/utf8/read.rs b/src/uu/wc/src/utf8/read.rs index 3556436f9..af10cbb53 100644 --- a/src/uu/wc/src/utf8/read.rs +++ b/src/uu/wc/src/utf8/read.rs @@ -4,9 +4,8 @@ // file that was distributed with this source code. // spell-checker:ignore bytestream use super::*; -use std::error::Error; -use std::fmt; use std::io::{self, BufRead}; +use thiserror::Error; /// Wraps a `std::io::BufRead` buffered byte stream and decode it as UTF-8. pub struct BufReadDecoder { @@ -15,36 +14,18 @@ pub struct BufReadDecoder { incomplete: Incomplete, } -#[derive(Debug)] +#[derive(Debug, Error)] pub enum BufReadDecoderError<'a> { /// Represents one UTF-8 error in the byte stream. /// /// In lossy decoding, each such error should be replaced with U+FFFD. /// (See `BufReadDecoder::next_lossy` and `BufReadDecoderError::lossy`.) + #[error("invalid byte sequence: {0:02x?}")] InvalidByteSequence(&'a [u8]), /// An I/O error from the underlying byte stream - Io(io::Error), -} - -impl fmt::Display for BufReadDecoderError<'_> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - BufReadDecoderError::InvalidByteSequence(bytes) => { - write!(f, "invalid byte sequence: {bytes:02x?}") - } - BufReadDecoderError::Io(ref err) => write!(f, "underlying bytestream error: {err}"), - } - } -} - -impl Error for BufReadDecoderError<'_> { - fn source(&self) -> Option<&(dyn Error + 'static)> { - match *self { - BufReadDecoderError::InvalidByteSequence(_) => None, - BufReadDecoderError::Io(ref err) => Some(err), - } - } + #[error("underlying bytestream error: {0}")] + Io(#[source] io::Error), } impl BufReadDecoder {