cat_dev/mion/
errors.rs

1//! Error types specifically for interacting with MION CGI's/protos.
2
3use crate::{
4	errors::{APIError, CatBridgeError, NetworkError},
5	mion::firmware::MionFirmwareAPIError,
6};
7use miette::Diagnostic;
8use thiserror::Error;
9
10#[cfg(any(feature = "clients", feature = "servers"))]
11use crate::{
12	errors::NetworkParseError,
13	mion::proto::{
14		cgis::MionCGIErrors,
15		control::MionControlProtocolError,
16		parameter::{MionParamProtocolError, MionParameterAPIError},
17	},
18};
19
20/// Errors that come from MION APIs specifically.
21#[derive(Error, Diagnostic, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub enum MionAPIError {
24	/// You attempted to set the default host bridge to a bridge that does not exist.
25	#[error("You cannot set a default bridge that does not exist.")]
26	#[diagnostic(code(cat_dev::api::mion::default_device_must_exist))]
27	DefaultDeviceMustExist,
28	/// You attempted to create a MION device name, but it was empty.
29	///
30	/// MION Identities must have at LEAST 1 byte.
31	#[error("The device name cannot be empty, it must be at least one byte long.")]
32	#[diagnostic(code(cat_dev::api::mion::device_name_cannot_be_empty))]
33	DeviceNameCannotBeEmpty,
34	/// You attempted to create a MION device name, which did not contain ASCII
35	/// characters.
36	///
37	/// MION Identities must contain all ascii characters.
38	#[error("A device name has to be completely ASCII! But it wasn't ASCII!")]
39	#[diagnostic(code(cat_dev::api::mion::device_name_not_ascii))]
40	DeviceNameMustBeAscii,
41	/// You attempted to create a MION device name, but it was longer than 255
42	/// bytes.
43	///
44	/// A MION device name has to be serialized into a packet, where it's length
45	/// is represented as a [`u8`] which means it can only be [`u8::MAX`], aka
46	/// 255 bytes.
47	#[error("A Device Name can only be 255 bytes long, but you specified one: {0} bytes long.")]
48	#[diagnostic(code(cat_dev::api::mion::device_name_too_long))]
49	DeviceNameTooLong(usize),
50	/// An error occured handling firmware.
51	#[error(transparent)]
52	#[diagnostic(transparent)]
53	Firmware(#[from] MionFirmwareAPIError),
54	#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
55	#[cfg(any(feature = "clients", feature = "servers"))]
56	#[error(transparent)]
57	#[diagnostic(transparent)]
58	ParameterSpace(#[from] MionParameterAPIError),
59}
60
61impl From<MionFirmwareAPIError> for APIError {
62	fn from(value: MionFirmwareAPIError) -> Self {
63		Self::Mion(value.into())
64	}
65}
66impl From<MionFirmwareAPIError> for CatBridgeError {
67	fn from(value: MionFirmwareAPIError) -> Self {
68		Self::API(value.into())
69	}
70}
71
72#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
73#[cfg(any(feature = "clients", feature = "servers"))]
74impl From<MionParameterAPIError> for APIError {
75	fn from(value: MionParameterAPIError) -> Self {
76		Self::Mion(value.into())
77	}
78}
79#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
80#[cfg(any(feature = "clients", feature = "servers"))]
81impl From<MionParameterAPIError> for CatBridgeError {
82	fn from(value: MionParameterAPIError) -> Self {
83		Self::API(value.into())
84	}
85}
86
87/// Errors dealing with various MION Protocols.
88#[derive(Error, Diagnostic, Debug, PartialEq, Eq)]
89#[non_exhaustive]
90pub enum MionProtocolError {
91	#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
92	#[cfg(any(feature = "clients", feature = "servers"))]
93	/// Errors related to CGI, and HTML pages.
94	#[error(transparent)]
95	#[diagnostic(transparent)]
96	CGI(#[from] MionCGIErrors),
97	#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
98	#[cfg(any(feature = "clients", feature = "servers"))]
99	/// Errors related to the CONTROL protocol for MION.
100	#[error(transparent)]
101	#[diagnostic(transparent)]
102	Control(#[from] MionControlProtocolError),
103	#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
104	#[cfg(any(feature = "clients", feature = "servers"))]
105	/// Errors related to the PARAMETER SPACE protocol for MION.
106	#[error(transparent)]
107	#[diagnostic(transparent)]
108	Params(#[from] MionParamProtocolError),
109}
110
111impl From<MionProtocolError> for NetworkError {
112	fn from(value: MionProtocolError) -> Self {
113		Self::Parse(value.into())
114	}
115}
116impl From<MionProtocolError> for CatBridgeError {
117	fn from(value: MionProtocolError) -> Self {
118		Self::Network(value.into())
119	}
120}
121
122#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
123#[cfg(any(feature = "clients", feature = "servers"))]
124impl From<MionCGIErrors> for NetworkParseError {
125	fn from(value: MionCGIErrors) -> Self {
126		Self::Mion(value.into())
127	}
128}
129#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
130#[cfg(any(feature = "clients", feature = "servers"))]
131impl From<MionCGIErrors> for NetworkError {
132	fn from(value: MionCGIErrors) -> Self {
133		Self::Parse(value.into())
134	}
135}
136#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
137#[cfg(any(feature = "clients", feature = "servers"))]
138impl From<MionCGIErrors> for CatBridgeError {
139	fn from(value: MionCGIErrors) -> Self {
140		Self::Network(value.into())
141	}
142}
143
144#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
145#[cfg(any(feature = "clients", feature = "servers"))]
146impl From<MionParamProtocolError> for NetworkParseError {
147	fn from(value: MionParamProtocolError) -> Self {
148		Self::Mion(value.into())
149	}
150}
151#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
152#[cfg(any(feature = "clients", feature = "servers"))]
153impl From<MionParamProtocolError> for NetworkError {
154	fn from(value: MionParamProtocolError) -> Self {
155		Self::Parse(value.into())
156	}
157}
158#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
159#[cfg(any(feature = "clients", feature = "servers"))]
160impl From<MionParamProtocolError> for CatBridgeError {
161	fn from(value: MionParamProtocolError) -> Self {
162		Self::Network(value.into())
163	}
164}
165
166#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
167#[cfg(any(feature = "clients", feature = "servers"))]
168impl From<MionControlProtocolError> for NetworkParseError {
169	fn from(value: MionControlProtocolError) -> Self {
170		Self::Mion(value.into())
171	}
172}
173#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
174#[cfg(any(feature = "clients", feature = "servers"))]
175impl From<MionControlProtocolError> for NetworkError {
176	fn from(value: MionControlProtocolError) -> Self {
177		Self::Parse(value.into())
178	}
179}
180#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
181#[cfg(any(feature = "clients", feature = "servers"))]
182impl From<MionControlProtocolError> for CatBridgeError {
183	fn from(value: MionControlProtocolError) -> Self {
184		Self::Network(value.into())
185	}
186}