1#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
3#[non_exhaustive]
4pub enum ConnectError {
5 #[error("unauthorized")]
8 Unauthorized,
9
10 #[error("forbidden")]
13 Forbidden,
14}
15
16impl ConnectError {
17 pub(crate) fn from_status_u16(status: u16) -> Option<Self> {
18 match status {
19 401 => Some(Self::Unauthorized),
20 403 => Some(Self::Forbidden),
21 _ => None,
22 }
23 }
24
25 pub fn is_auth(&self) -> bool {
28 matches!(self, Self::Unauthorized | Self::Forbidden)
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35
36 #[test]
37 fn auth_statuses_are_terminal() {
38 assert_eq!(ConnectError::from_status_u16(401), Some(ConnectError::Unauthorized));
39 assert_eq!(ConnectError::from_status_u16(403), Some(ConnectError::Forbidden));
40 }
41
42 #[test]
43 fn non_auth_statuses_are_not_terminal() {
44 for status in [400, 404, 500] {
45 assert_eq!(ConnectError::from_status_u16(status), None);
46 }
47 }
48}