1use std::{fmt, io, str::Utf8Error};
2
3#[derive(Debug)]
4pub enum Error {
6 IO(io::Error),
8 Utf8(Utf8Error),
10 MissingHeader(&'static str),
12 InvalidHeader(&'static str),
14 ParseSearchTargetError(ParseSearchTargetError),
16 InvalidHTTP(&'static str),
18 HTTPError(u32),
20}
21
22impl fmt::Display for Error {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 match self {
25 Error::IO(err) => write!(f, "io error: {err}"),
26 Error::Utf8(err) => write!(f, "utf8 decoding error: {err}"),
27 Error::MissingHeader(err) => write!(f, "missing header: {err}"),
28 Error::InvalidHeader(err) => write!(f, "invalid header: {err}"),
29 Error::ParseSearchTargetError(err) => write!(f, "{err}"),
30 Error::InvalidHTTP(err) => write!(f, "failed to parse http response: {err}"),
31 Error::HTTPError(err) => write!(
32 f,
33 "control point responded with non-zero exit code: {err}"
34 ),
35 }
36 }
37}
38
39impl std::error::Error for Error {
40 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
41 match self {
42 Error::IO(err) => Some(err),
43 Error::Utf8(err) => Some(err),
44 Error::ParseSearchTargetError(err) => Some(err),
45 _ => None,
46 }
47 }
48}
49
50impl From<io::Error> for Error {
51 fn from(err: io::Error) -> Self {
52 Error::IO(err)
53 }
54}
55impl From<Utf8Error> for Error {
56 fn from(err: Utf8Error) -> Self {
57 Error::Utf8(err)
58 }
59}
60impl From<ParseSearchTargetError> for Error {
61 fn from(err: ParseSearchTargetError) -> Self {
62 Error::ParseSearchTargetError(err)
63 }
64}
65
66#[derive(Debug, Eq, PartialEq)]
68pub struct ParseURNError;
69impl std::error::Error for ParseURNError {}
70impl fmt::Display for ParseURNError {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 write!(f, "failed to parse URN")
73 }
74}
75
76#[derive(Debug, Eq, PartialEq)]
78pub enum ParseSearchTargetError {
79 #[allow(clippy::upper_case_acronyms)]
81 URN(ParseURNError),
82 ST,
84}
85
86impl fmt::Display for ParseSearchTargetError {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 match self {
89 ParseSearchTargetError::URN(_) => write!(f, "invalid urn supplied"),
90 ParseSearchTargetError::ST => write!(f, "invalid search target format"),
91 }
92 }
93}
94impl std::error::Error for ParseSearchTargetError {
95 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
96 if let ParseSearchTargetError::URN(err) = self {
97 Some(err)
98 } else {
99 None
100 }
101 }
102}