1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Errors that can occur during client-server lookup

/// Errors that can occur during lookup. Refer to [the specification] to see how
/// the two variants should be handled.
///
/// [the spec]: https://matrix.org/docs/spec/client_server/latest#well-known-uri
#[derive(Debug)]
pub enum Error {
	/// Corresponds to the `FAIL_PROMPT` code in the spec.
	Prompt(reqwest::Error),
	/// Corresponds to the `FAIL_ERROR` code in the spec.
	Fail(FailError),
}

impl std::error::Error for Error {
	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
		match *self {
			Self::Prompt(ref e) => Some(e),
			Self::Fail(ref e) => Some(e),
		}
	}
}

impl std::fmt::Display for Error {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::Prompt(e) => write!(f, "{}", e),
			Self::Fail(e) => write!(f, "{}", e),
		}
	}
}

impl From<reqwest::Error> for Error {
	fn from(e: reqwest::Error) -> Self {
		Error::Prompt(e)
	}
}

impl From<url::ParseError> for Error {
	fn from(e: url::ParseError) -> Self {
		Error::Fail(FailError::Url(e))
	}
}

impl From<FailError> for Error {
	fn from(e: FailError) -> Self {
		Error::Fail(e)
	}
}

/// Corresponds to the `FAIL_PROMPT` code in the spec.
#[derive(Debug)]
pub enum FailError {
	/// URL parsing error
	Url(url::ParseError),
	/// HTTP error
	Http(reqwest::Error),
}

impl std::error::Error for FailError {
	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
		match *self {
			Self::Http(ref e) => Some(e),
			Self::Url(ref e) => Some(e),
		}
	}
}

impl std::fmt::Display for FailError {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::Http(e) => write!(f, "{}", e),
			Self::Url(e) => write!(f, "{}", e),
		}
	}
}

impl From<reqwest::Error> for FailError {
	fn from(e: reqwest::Error) -> Self {
		FailError::Http(e)
	}
}

impl From<url::ParseError> for FailError {
	fn from(e: url::ParseError) -> Self {
		FailError::Url(e)
	}
}