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:
parent
9099f342e0
commit
047d9a930b
1 changed files with 5 additions and 24 deletions
|
@ -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> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue