regex_chunker/
err.rs

1/*!
2Error types returned by the various chunkers.
3*/
4use std::{error::Error, fmt::Display, string::FromUtf8Error};
5
6/**
7Wraps various types of errors that can happen in the internals of a
8Chunker. The way Chunkers respond to and report these errors can be
9controlled through builder-pattern methods that take the
10[`ErrorResponse`](crate::ErrorResponse) and
11[`Utf8FailureMode`](crate::Utf8FailureMode) types.
12*/
13#[derive(Debug)]
14pub enum RcErr {
15    /// Error returned during creation of a regex.
16    Regex(regex::Error),
17    /// Error returned during reading from a `*Chunker`'s source.
18    Read(std::io::Error),
19    /// Error returned by a
20    // [`CustomChunker<StringAdapter>`](crate::StringChunker)
21    /// upon encountering non-UTF-8 data.
22    Utf8(FromUtf8Error),
23}
24
25impl Display for RcErr {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        match self {
28            RcErr::Regex(e) => write!(f, "regex error: {}", &e),
29            RcErr::Read(e) => write!(f, "read error: {}", &e),
30            RcErr::Utf8(e) => write!(f, "UTF-8 decoding error: {}", &e),
31        }
32    }
33}
34
35impl From<regex::Error> for RcErr {
36    fn from(e: regex::Error) -> Self {
37        RcErr::Regex(e)
38    }
39}
40
41impl From<std::io::Error> for RcErr {
42    fn from(e: std::io::Error) -> Self {
43        RcErr::Read(e)
44    }
45}
46
47impl From<FromUtf8Error> for RcErr {
48    fn from(e: FromUtf8Error) -> Self {
49        RcErr::Utf8(e)
50    }
51}
52
53impl Error for RcErr {
54    fn source<'a>(&'a self) -> Option<&(dyn Error + 'static)> {
55        match self {
56            RcErr::Regex(e) => Some(e),
57            RcErr::Read(e) => Some(e),
58            RcErr::Utf8(e) => Some(e),
59        }
60    }
61}