1 use std::{error, fmt};
2 
3 use crate::platform_impl;
4 
5 /// An error whose cause it outside Winit's control.
6 #[derive(Debug)]
7 pub enum ExternalError {
8     /// The operation is not supported by the backend.
9     NotSupported(NotSupportedError),
10     /// The OS cannot perform the operation.
11     Os(OsError),
12 }
13 
14 /// The error type for when the requested operation is not supported by the backend.
15 #[derive(Clone)]
16 pub struct NotSupportedError {
17     _marker: (),
18 }
19 
20 /// The error type for when the OS cannot perform the requested operation.
21 #[derive(Debug)]
22 pub struct OsError {
23     line: u32,
24     file: &'static str,
25     error: platform_impl::OsError,
26 }
27 
28 impl NotSupportedError {
29     #[inline]
30     #[allow(dead_code)]
new() -> NotSupportedError31     pub(crate) fn new() -> NotSupportedError {
32         NotSupportedError { _marker: () }
33     }
34 }
35 
36 impl OsError {
37     #[allow(dead_code)]
new(line: u32, file: &'static str, error: platform_impl::OsError) -> OsError38     pub(crate) fn new(line: u32, file: &'static str, error: platform_impl::OsError) -> OsError {
39         OsError { line, file, error }
40     }
41 }
42 
43 #[allow(unused_macros)]
44 macro_rules! os_error {
45     ($error:expr) => {{
46         crate::error::OsError::new(line!(), file!(), $error)
47     }};
48 }
49 
50 impl fmt::Display for OsError {
fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error>51     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
52         f.pad(&format!(
53             "os error at {}:{}: {}",
54             self.file, self.line, self.error
55         ))
56     }
57 }
58 
59 impl fmt::Display for ExternalError {
fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error>60     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
61         match self {
62             ExternalError::NotSupported(e) => e.fmt(f),
63             ExternalError::Os(e) => e.fmt(f),
64         }
65     }
66 }
67 
68 impl fmt::Debug for NotSupportedError {
fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error>69     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
70         f.debug_struct("NotSupportedError").finish()
71     }
72 }
73 
74 impl fmt::Display for NotSupportedError {
fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error>75     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
76         f.pad("the requested operation is not supported by Winit")
77     }
78 }
79 
80 impl error::Error for OsError {}
81 impl error::Error for ExternalError {}
82 impl error::Error for NotSupportedError {}
83