1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

wc/BufReadDecoderError: move to thiserror

This commit is contained in:
Solomon Victorino 2025-03-22 18:18:16 -06:00 committed by Sylvestre Ledru
parent 9099f342e0
commit 047d9a930b

View file

@ -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<B: BufRead> {
@ -15,36 +14,18 @@ pub struct BufReadDecoder<B: BufRead> {
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<B: BufRead> BufReadDecoder<B> {