Skip to main content

kodik_parser/
error.rs

1//! Error types for the Kodik parser library.
2use std::{error, fmt, string};
3
4/// Errors from kodik-parser.
5#[derive(Debug)]
6#[non_exhaustive]
7pub enum Error {
8    /// Reqwest HTTP client error.
9    #[cfg(feature = "async-impl")]
10    Reqwest(reqwest::Error),
11    /// Ureq HTTP client error.
12    #[cfg(feature = "blocking")]
13    Ureq(ureq::Error),
14    /// Base64 decoding error.
15    Decode(base64::DecodeError),
16    /// UTF-8 conversion error.
17    FromUtf8(string::FromUtf8Error),
18    /// Regex matching error.
19    Regex(&'static str),
20    /// Link cannot be decoded error.
21    LinkCannotBeDecoded(String),
22}
23
24impl error::Error for Error {}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            #[cfg(feature = "async-impl")]
30            Self::Reqwest(error) => write!(f, "Reqwest: {error}"),
31            #[cfg(feature = "blocking")]
32            Self::Ureq(error) => write!(f, "Ureq: {error}"),
33            Self::Decode(error) => write!(f, "Decode: {error}"),
34            Self::FromUtf8(error) => write!(f, "FromUtf8: {error}"),
35            Self::Regex(msg) => write!(f, "Regex: {msg}"),
36            Self::LinkCannotBeDecoded(v) => write!(f, "Src: {v} cannot be decoded"),
37        }
38    }
39}
40
41#[cfg(feature = "async-impl")]
42impl From<reqwest::Error> for Error {
43    fn from(value: reqwest::Error) -> Self {
44        Self::Reqwest(value)
45    }
46}
47
48#[cfg(feature = "blocking")]
49impl From<ureq::Error> for Error {
50    fn from(value: ureq::Error) -> Self {
51        Self::Ureq(value)
52    }
53}
54
55impl From<base64::DecodeError> for Error {
56    fn from(value: base64::DecodeError) -> Self {
57        Self::Decode(value)
58    }
59}
60
61impl From<string::FromUtf8Error> for Error {
62    fn from(value: string::FromUtf8Error) -> Self {
63        Self::FromUtf8(value)
64    }
65}