Skip to main content

moq_native/
connect.rs

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