1use std::error::Error;
2use std::fmt::{self, Display};
3
4#[derive(Debug)]
7#[non_exhaustive]
8pub enum EventLoopError {
9 RecreationAttempt,
11 ExitFailure(i32),
13 Os(OsError),
15 NotSupported(NotSupportedError),
17}
18
19impl fmt::Display for EventLoopError {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 match self {
22 Self::RecreationAttempt => write!(f, "EventLoop can't be recreated"),
23 Self::Os(err) => err.fmt(f),
24 Self::ExitFailure(status) => write!(f, "Exit Failure: {status}"),
25 Self::NotSupported(err) => err.fmt(f),
26 }
27 }
28}
29
30impl Error for EventLoopError {
31 fn source(&self) -> Option<&(dyn Error + 'static)> {
32 if let Self::Os(err) = self {
33 err.source()
34 } else {
35 None
36 }
37 }
38}
39
40impl From<OsError> for EventLoopError {
41 fn from(value: OsError) -> Self {
42 Self::Os(value)
43 }
44}
45
46impl From<NotSupportedError> for EventLoopError {
47 fn from(value: NotSupportedError) -> Self {
48 Self::NotSupported(value)
49 }
50}
51
52#[derive(Debug)]
54#[non_exhaustive]
55pub enum RequestError {
56 NotSupported(NotSupportedError),
58 Ignored,
60 Os(OsError),
62}
63
64impl Display for RequestError {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 match self {
67 Self::NotSupported(err) => err.fmt(f),
68 Self::Ignored => write!(f, "The request was ignored"),
69 Self::Os(err) => err.fmt(f),
70 }
71 }
72}
73impl Error for RequestError {
74 fn source(&self) -> Option<&(dyn Error + 'static)> {
75 if let Self::Os(err) = self {
76 err.source()
77 } else {
78 None
79 }
80 }
81}
82
83impl From<NotSupportedError> for RequestError {
84 fn from(value: NotSupportedError) -> Self {
85 Self::NotSupported(value)
86 }
87}
88
89impl From<OsError> for RequestError {
90 fn from(value: OsError) -> Self {
91 Self::Os(value)
92 }
93}
94
95#[derive(Debug)]
97pub struct NotSupportedError {
98 reason: &'static str,
100}
101
102impl NotSupportedError {
103 pub fn new(reason: &'static str) -> Self {
104 Self { reason }
105 }
106}
107
108impl fmt::Display for NotSupportedError {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 write!(f, "Operation is not supported: {}", self.reason)
111 }
112}
113impl Error for NotSupportedError {}
114
115#[derive(Debug)]
117pub struct OsError {
118 line: u32,
119 file: &'static str,
120 error: Box<dyn Error + Send + Sync + 'static>,
121}
122
123impl OsError {
124 #[allow(dead_code)]
125 pub(crate) fn new(
126 line: u32,
127 file: &'static str,
128 error: impl Into<Box<dyn Error + Send + Sync + 'static>>,
129 ) -> Self {
130 Self { line, file, error: error.into() }
131 }
132}
133
134impl Display for OsError {
135 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136 f.pad(&format!("os error at {}:{}: {}", self.file, self.line, self.error))
137 }
138}
139impl Error for OsError {
140 fn source(&self) -> Option<&(dyn Error + 'static)> {
141 Some(self.error.as_ref())
142 }
143}
144
145#[allow(unused_macros)]
146macro_rules! os_error {
147 ($error:expr) => {{
148 crate::error::OsError::new(line!(), file!(), $error)
149 }};
150}