ironrdp_core/error.rs
1#[cfg(feature = "alloc")]
2use alloc::string::String;
3
4use ironrdp_error::{Error, Source};
5
6/// Trait for adding a source to an error type.
7pub trait WithSource {
8 /// Adds a source to the error.
9 ///
10 /// # Arguments
11 ///
12 /// * `source` - The source error to add.
13 ///
14 /// # Returns
15 ///
16 /// The error with the added source.
17 #[must_use]
18 fn with_source<E: Source>(self, source: E) -> Self;
19}
20
21impl<T> WithSource for Error<T> {
22 fn with_source<E: Source>(self, source: E) -> Self {
23 self.with_source(source)
24 }
25}
26
27/// Trait for creating "not enough bytes" errors.
28pub trait NotEnoughBytesErr {
29 /// Creates a new "not enough bytes" error.
30 ///
31 /// # Arguments
32 ///
33 /// * `context` - The context in which the error occurred.
34 /// * `received` - The number of bytes received.
35 /// * `expected` - The number of bytes expected.
36 ///
37 /// # Returns
38 ///
39 /// A new error instance.
40 fn not_enough_bytes(context: &'static str, received: usize, expected: usize) -> Self;
41}
42
43/// Helper function to create a "not enough bytes" error.
44///
45/// This function is a convenience wrapper around the `NotEnoughBytesErr` trait.
46///
47/// # Arguments
48///
49/// * `context` - The context in which the error occurred.
50/// * `received` - The number of bytes received.
51/// * `expected` - The number of bytes expected.
52///
53/// # Returns
54///
55/// A new error instance of type `T` that implements `NotEnoughBytesErr`.
56pub fn not_enough_bytes_err<T: NotEnoughBytesErr>(context: &'static str, received: usize, expected: usize) -> T {
57 T::not_enough_bytes(context, received, expected)
58}
59
60/// Trait for creating "invalid field" errors.
61pub trait InvalidFieldErr {
62 /// Creates a new "invalid field" error.
63 ///
64 /// # Arguments
65 ///
66 /// * `context` - The context in which the error occurred.
67 /// * `field` - The name of the invalid field.
68 /// * `reason` - The reason why the field is invalid.
69 ///
70 /// # Returns
71 ///
72 /// A new error instance.
73 fn invalid_field(context: &'static str, field: &'static str, reason: &'static str) -> Self;
74}
75
76/// Helper function to create an "invalid field" error with a source.
77///
78/// This function is a convenience wrapper around the `InvalidFieldErr` and `WithSource` traits.
79///
80/// # Arguments
81///
82/// * `context` - The context in which the error occurred.
83/// * `field` - The name of the invalid field.
84/// * `reason` - The reason why the field is invalid.
85/// * `source` - The source error to add.
86///
87/// # Returns
88///
89/// A new error instance of type `T` that implements both `InvalidFieldErr` and `WithSource`.
90pub fn invalid_field_err_with_source<T: InvalidFieldErr + WithSource, E: Source>(
91 context: &'static str,
92 field: &'static str,
93 reason: &'static str,
94 source: E,
95) -> T {
96 T::invalid_field(context, field, reason).with_source(source)
97}
98
99/// Helper function to create an "invalid field" error.
100pub fn invalid_field_err<T: InvalidFieldErr>(context: &'static str, field: &'static str, reason: &'static str) -> T {
101 T::invalid_field(context, field, reason)
102}
103
104/// Trait for creating "unexpected message type" errors.
105pub trait UnexpectedMessageTypeErr {
106 /// Creates a new "unexpected message type" error.
107 ///
108 /// # Arguments
109 ///
110 /// * `context` - The context in which the error occurred.
111 /// * `got` - The unexpected message type received.
112 ///
113 /// # Returns
114 ///
115 /// A new error instance.
116 fn unexpected_message_type(context: &'static str, got: u8) -> Self;
117}
118
119/// Helper function to create an "unexpected message type" error.
120///
121/// This function is a convenience wrapper around the `UnexpectedMessageTypeErr` trait.
122///
123/// # Arguments
124///
125/// * `context` - The context in which the error occurred.
126/// * `got` - The unexpected message type received.
127///
128/// # Returns
129///
130/// A new error instance of type `T` that implements `UnexpectedMessageTypeErr`.
131pub fn unexpected_message_type_err<T: UnexpectedMessageTypeErr>(context: &'static str, got: u8) -> T {
132 T::unexpected_message_type(context, got)
133}
134
135/// Trait for creating "unsupported version" errors.
136pub trait UnsupportedVersionErr {
137 /// Creates a new "unsupported version" error.
138 ///
139 /// # Arguments
140 ///
141 /// * `context` - The context in which the error occurred.
142 /// * `got` - The unsupported version received.
143 ///
144 /// # Returns
145 ///
146 /// A new error instance.
147 fn unsupported_version(context: &'static str, got: u8) -> Self;
148}
149
150/// Helper function to create an "unsupported version" error.
151///
152/// This function is a convenience wrapper around the `UnsupportedVersionErr` trait.
153///
154/// # Arguments
155///
156/// * `context` - The context in which the error occurred.
157/// * `got` - The unsupported version received.
158///
159/// # Returns
160///
161/// A new error instance of type `T` that implements `UnsupportedVersionErr`.
162pub fn unsupported_version_err<T: UnsupportedVersionErr>(context: &'static str, got: u8) -> T {
163 T::unsupported_version(context, got)
164}
165
166/// Trait for creating "unsupported value" errors.
167pub trait UnsupportedValueErr {
168 /// Creates a new "unsupported value" error when the "alloc" feature is enabled.
169 ///
170 /// # Arguments
171 ///
172 /// * `context` - The context in which the error occurred.
173 /// * `name` - The name of the unsupported value.
174 /// * `value` - The unsupported value.
175 ///
176 /// # Returns
177 ///
178 /// A new error instance.
179 #[cfg(feature = "alloc")]
180 fn unsupported_value(context: &'static str, name: &'static str, value: String) -> Self;
181
182 /// Creates a new "unsupported value" error when the "alloc" feature is disabled.
183 ///
184 /// # Arguments
185 ///
186 /// * `context` - The context in which the error occurred.
187 /// * `name` - The name of the unsupported value.
188 ///
189 /// # Returns
190 ///
191 /// A new error instance.
192 #[cfg(not(feature = "alloc"))]
193 fn unsupported_value(context: &'static str, name: &'static str) -> Self;
194}
195
196/// Helper function to create an "unsupported value" error when the "alloc" feature is enabled.
197///
198/// This function is a convenience wrapper around the `UnsupportedValueErr` trait.
199///
200/// # Arguments
201///
202/// * `context` - The context in which the error occurred.
203/// * `name` - The name of the unsupported value.
204/// * `value` - The unsupported value.
205///
206/// # Returns
207///
208/// A new error instance of type `T` that implements `UnsupportedValueErr`.]
209#[cfg(feature = "alloc")]
210pub fn unsupported_value_err<T: UnsupportedValueErr>(context: &'static str, name: &'static str, value: String) -> T {
211 T::unsupported_value(context, name, value)
212}
213
214/// Helper function to create an "unsupported value" error.
215#[cfg(not(feature = "alloc"))]
216pub fn unsupported_value_err<T: UnsupportedValueErr>(context: &'static str, name: &'static str) -> T {
217 T::unsupported_value(context, name)
218}
219
220/// Trait for creating generic "other" errors.
221pub trait OtherErr {
222 /// Creates a new generic "other" error.
223 ///
224 /// # Arguments
225 ///
226 /// * `context` - The context in which the error occurred.
227 /// * `description` - A description of the error.
228 ///
229 /// # Returns
230 ///
231 /// A new error instance.
232 fn other(context: &'static str, description: &'static str) -> Self;
233}
234
235/// Helper function to create a generic "other" error.
236///
237/// This function is a convenience wrapper around the `OtherErr` trait.
238///
239/// # Arguments
240///
241/// * `context` - The context in which the error occurred.
242/// * `description` - A description of the error.
243///
244/// # Returns
245///
246/// A new error instance of type `T` that implements `OtherErr`.
247pub fn other_err<T: OtherErr>(context: &'static str, description: &'static str) -> T {
248 T::other(context, description)
249}
250
251/// Helper function to create a generic "other" error with a source.
252///
253/// This function is a convenience wrapper around the `OtherErr` and `WithSource` traits.
254///
255/// # Arguments
256///
257/// * `context` - The context in which the error occurred.
258/// * `description` - A description of the error.
259/// * `source` - The source error to add.
260///
261/// # Returns
262///
263/// A new error instance of type `T` that implements both `OtherErr` and `WithSource`.
264pub fn other_err_with_source<T: OtherErr + WithSource, E: Source>(
265 context: &'static str,
266 description: &'static str,
267 source: E,
268) -> T {
269 T::other(context, description).with_source(source)
270}