Skip to main content

winconsole/errors/
win_error.rs

1use super::*;
2use std::io::Error as IoError;
3use std::string::{FromUtf16Error, FromUtf8Error};
4
5macro_rules! win_errs {
6	($($(#[$item_attrs:meta])* $name:ident : $err_name:ident),*) => (
7		use std::{
8			error::Error,
9			fmt::{Display, Formatter, Result}
10		};
11
12		/// Contains wrapped error types.
13		#[derive(Debug)]
14		pub enum WinError {
15			$(
16				$(#[$item_attrs])*
17				$name($err_name),
18			)*
19		}
20
21		impl Error for WinError {
22			fn description(&self) -> &str {
23				match *self {
24					$(
25						WinError::$name(ref err) => Error::description(err as &dyn Error),
26					)*
27				}
28			}
29			fn cause(&self) -> Option<&dyn Error> {
30				match *self {
31					$(
32						WinError::$name(ref err) => Some(err as &dyn Error),
33					)*
34				}
35			}
36		}
37		impl Display for WinError {
38			fn fmt(&self, f: &mut Formatter) -> Result {
39				match *self {
40					$(
41						WinError::$name(ref err) => Display::fmt(&err, f),
42					)*
43				}
44			}
45		}
46
47		$(
48			impl From<$err_name> for WinError {
49				fn from(value: $err_name) -> WinError {
50					WinError::$name(value)
51				}
52			}
53		)*
54	);
55}
56
57win_errs! {
58    /// An argument error.
59    Argument: ArgumentError,
60    /// An error which occurred while converting to a string from a
61    /// UTF-8 byte vector.
62    FromUtf8: FromUtf8Error,
63    /// An error which occurred while converting to a string from a
64    /// UTF-16 byte vector.
65    FromUtf16: FromUtf16Error,
66    /// An invalid handle error.
67    InvalidHandle: InvalidHandleError,
68    /// An IO or OS error.
69    Io: IoError
70}