hyper_simple_server/
errors.rs

1
2
3#![ allow (dead_code) ]
4
5
6
7
8use crate::prelude::*;
9
10
11
12
13pub(crate) mod exports {
14	
15	#[ cfg (feature = "hss-errors") ]
16	pub use super::{
17			
18			ServerError,
19			ServerResult,
20			
21			ResultExtPanic,
22			ErrorExtPanic,
23			
24			ResultExtWrap,
25			ResultExtWrapFrom,
26			ErrorExtWrap,
27			ErrorExtWrapFrom,
28			
29			error_with_format,
30			error_with_message,
31			error_with_code,
32			error_wrap,
33			
34			panic_with_format,
35			panic_with_message,
36			panic_with_code,
37			
38		};
39}
40
41
42
43
44pub type ServerError = io::Error;
45pub type ServerResult<V = ()> = Result<V, ServerError>;
46
47
48
49
50pub trait ResultExtPanic <V> : Sized {
51	
52	fn or_panic (self, _code : u32) -> V;
53	
54	fn infallible (self, _code : u32) -> V;
55}
56
57
58impl <V, EX : ErrorExtPanic> ResultExtPanic<V> for Result<V, EX> {
59	
60	fn or_panic (self, _code : u32) -> V {
61		match self {
62			Ok (_value) =>
63				_value,
64			Err (_error) =>
65				_error.panic (_code),
66		}
67	}
68	
69	fn infallible (self, _code : u32) -> V {
70		match self {
71			Ok (_value) =>
72				_value,
73			Err (_error) =>
74				_error.panic (_code),
75		}
76	}
77}
78
79
80impl <V> ResultExtPanic<V> for Option<V> {
81	
82	fn or_panic (self, _code : u32) -> V {
83		match self {
84			Some (_value) =>
85				_value,
86			None =>
87				panic_with_code (_code),
88		}
89	}
90	
91	fn infallible (self, _code : u32) -> V {
92		match self {
93			Some (_value) =>
94				_value,
95			None =>
96				panic_with_code (_code),
97		}
98	}
99}
100
101
102
103
104pub trait ErrorExtPanic : Sized {
105	
106	fn panic (self, _code : u32) -> !;
107}
108
109
110impl <E : Error> ErrorExtPanic for E {
111	
112	fn panic (self, _code : u32) -> ! {
113		panic! ("[{:08x}]  unexpected error encountered!  //  {}", _code, self);
114	}
115}
116
117
118
119
120pub trait ResultExtWrap <V, E> : Sized {
121	
122	fn or_wrap (self, _code : u32) -> Result<V, E>;
123}
124
125
126impl <V, E : Error> ResultExtWrap<V, io::Error> for Result<V, E> {
127	
128	fn or_wrap (self, _code : u32) -> Result<V, io::Error> {
129		match self {
130			Ok (_value) =>
131				Ok (_value),
132			Err (_error) =>
133				Err (io::Error::wrap_from (_code, _error)),
134		}
135	}
136}
137
138
139impl <V> ResultExtWrap<V, io::Error> for Option<V> {
140	
141	fn or_wrap (self, _code : u32) -> Result<V, io::Error> {
142		if let Some (_value) = self {
143			Ok (_value)
144		} else {
145			Err (error_with_code (_code))
146		}
147	}
148}
149
150
151
152
153pub trait ResultExtWrapFrom <V, E> : Sized {
154	
155	fn or_wrap_from (_code : u32, _result : Result<V, E>) -> Self;
156}
157
158
159impl <V, E : Error, EX : ErrorExtWrapFrom<E>> ResultExtWrapFrom<V, E> for Result<V, EX> {
160	
161	fn or_wrap_from (_code : u32, _result : Result<V, E>) -> Result<V, EX> {
162		match _result {
163			Ok (_value) =>
164				Ok (_value),
165			Err (_error) =>
166				Err (EX::wrap_from (_code, _error)),
167		}
168	}
169}
170
171
172
173
174pub trait ErrorExtWrapFrom <E> : Sized {
175	
176	fn wrap_from (_code : u32, _error : E) -> Self;
177}
178
179
180impl <E : Error> ErrorExtWrapFrom<E> for io::Error {
181	
182	fn wrap_from (_code : u32, _error : E) -> Self {
183		io::Error::new (io::ErrorKind::Other, format! ("[{:08x}]  {}", _code, _error))
184	}
185}
186
187
188
189
190pub trait ErrorExtWrap <E> : Sized {
191	
192	fn wrap (self, _code : u32) -> E;
193}
194
195
196impl <EI, EO : ErrorExtWrapFrom<EI>> ErrorExtWrap<EO> for EI {
197	
198	fn wrap (self, _code : u32) -> EO {
199		EO::wrap_from (_code, self)
200	}
201}
202
203
204
205
206pub fn error_with_format (_code : u32, _message : fmt::Arguments<'_>) -> io::Error {
207	io::Error::new (io::ErrorKind::Other, format! ("[{:08x}]  {}", _code, _message))
208}
209
210pub fn error_with_message (_code : u32, _message : &str) -> io::Error {
211	if ! _message.is_empty () {
212		io::Error::new (io::ErrorKind::Other, format! ("[{:08x}]  {}", _code, _message))
213	} else {
214		error_with_code (_code)
215	}
216}
217
218pub fn error_with_code (_code : u32) -> io::Error {
219	io::Error::new (io::ErrorKind::Other, format! ("[{:08x}]  unexpected error encountered!", _code))
220}
221
222pub fn error_wrap <E : Error> (_code : u32, _error : E) -> io::Error {
223	io::Error::wrap_from (_code, _error)
224}
225
226
227
228
229pub fn panic_with_format (_code : u32, _message : fmt::Arguments<'_>) -> ! {
230	panic! ("[{:08x}]  {}", _code, _message)
231}
232
233pub fn panic_with_message (_code : u32, _message : &str) -> ! {
234	if ! _message.is_empty () {
235		panic! ("[{:08x}]  {}", _code, _message)
236	} else {
237		panic_with_code (_code)
238	}
239}
240
241pub fn panic_with_code (_code : u32) -> ! {
242	panic! ("[{:08x}]  unexpected error encountered!", _code)
243}
244
245
246
247
248#[ cfg (feature = "hss-errors") ]
249#[ macro_export ]
250macro_rules! fail_with_format {
251	( $_code : literal, $_format : literal, $( $_argument : tt )* ) => {
252		return ::std::result::Result::Err ($crate::error_with_format ($_code, ::std::format_args! ($_format, $( $_argument )* )))
253	}
254}
255
256#[ cfg (feature = "hss-errors") ]
257#[ macro_export ]
258macro_rules! fail_with_message {
259	( $_code : literal, $_message : literal ) => {
260		return ::std::result::Result::Err ($crate::error_with_message ($_code, $_message))
261	};
262}
263
264#[ cfg (feature = "hss-errors") ]
265#[ macro_export ]
266macro_rules! fail_with_code {
267	( $_code : literal ) => {
268		return ::std::result::Result::Err ($crate::error_with_code ($_code))
269	};
270}
271
272#[ cfg (feature = "hss-errors") ]
273#[ macro_export ]
274macro_rules! fail_wrap {
275	( $_code : literal, $_error : expr ) => {
276		return ::std::result::Result::Err ($crate::error_wrap ($_code, $_error))
277	};
278}
279