ssdp_client/
error.rs

1use std::{fmt, io, str::Utf8Error};
2
3#[derive(Debug)]
4/// The Error type
5pub enum Error {
6    /// IO Error
7    IO(io::Error),
8    /// SSDP is not encoded properly
9    Utf8(Utf8Error),
10    /// Missing header in the SSDP Response
11    MissingHeader(&'static str),
12    /// Invalid header in the SSDP Response
13    InvalidHeader(&'static str),
14    /// Malformed search target in SSDP header
15    ParseSearchTargetError(ParseSearchTargetError),
16    /// Failed to parse HTTP response
17    InvalidHTTP(&'static str),
18    /// Non-200 HTTP Status Code
19    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/// An error returned when parsing a search target using `from_str` fails
67#[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/// An error returned when parsing a search target using `from_str` fails
77#[derive(Debug, Eq, PartialEq)]
78pub enum ParseSearchTargetError {
79    /// Failed to parse URN in Search Target
80    #[allow(clippy::upper_case_acronyms)]
81    URN(ParseURNError),
82    /// Failed to parse Search Target
83    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}