Skip to main content

native_windows_gui2/
errors.rs

1use std::error::Error;
2use std::fmt;
3
4#[cfg(feature = "plotting")]
5use crate::win32::plotters_d2d::PlottersError;
6
7/**
8    Error enums used in the native window gui crate
9*/
10#[derive(Debug, Clone)]
11pub enum NwgError {
12    Unknown,
13
14    /// Fatal error raised when calling low level winapi functionalities
15    InitializationError(String),
16
17    /// Error raised when creating a control.
18    ControlCreationError(String),
19
20    /// Error raised when creating a menu.
21    MenuCreationError(String),
22
23    /// Error raised when creating a resource.
24    ResourceCreationError(String),
25
26    /// Error raised when the creation of a layout failed
27    LayoutCreationError(String),
28
29    /// Error raised when an event handler could not be bound
30    EventsBinding(String),
31
32    /// Error raised by the FileDialog object
33    #[cfg(feature = "file-dialog")]
34    FileDialogError(String),
35
36    /// Error raised by the ImageDecoder feature
37    #[cfg(feature = "image-decoder")]
38    ImageDecoderError(i32, String),
39
40    /// Error raised by one of the locale functions
41    #[cfg(feature = "winnls")]
42    BadLocale(String),
43
44    /// Error raised by one of the locale functions
45    #[cfg(feature = "plotting")]
46    Plotters(PlottersError),
47}
48
49impl NwgError {
50    pub fn initialization<S: Into<String>>(e: S) -> NwgError {
51        NwgError::InitializationError(e.into())
52    }
53
54    pub fn control_create<S: Into<String>>(e: S) -> NwgError {
55        NwgError::ControlCreationError(e.into())
56    }
57
58    pub fn menu_create<S: Into<String>>(e: S) -> NwgError {
59        NwgError::MenuCreationError(e.into())
60    }
61
62    pub fn resource_create<S: Into<String>>(e: S) -> NwgError {
63        NwgError::ResourceCreationError(e.into())
64    }
65
66    pub fn layout_create<S: Into<String>>(e: S) -> NwgError {
67        NwgError::LayoutCreationError(e.into())
68    }
69
70    pub fn events_binding<S: Into<String>>(e: S) -> NwgError {
71        NwgError::EventsBinding(e.into())
72    }
73
74    #[cfg(feature = "file-dialog")]
75    pub fn file_dialog<S: Into<String>>(e: S) -> NwgError {
76        NwgError::FileDialogError(e.into())
77    }
78
79    #[cfg(feature = "winnls")]
80    pub fn bad_locale<S: Into<String>>(e: S) -> NwgError {
81        NwgError::BadLocale(e.into())
82    }
83
84    #[cfg(feature = "image-decoder")]
85    pub fn image_decoder<S: Into<String>>(code: i32, e: S) -> NwgError {
86        NwgError::ImageDecoderError(code, e.into())
87    }
88
89    pub fn no_parent(name: &'static str) -> NwgError {
90        NwgError::ControlCreationError(format!("No parent defined for {:?} control", name))
91    }
92
93    pub fn no_parent_menu() -> NwgError {
94        NwgError::MenuCreationError("No parent defined for menu".to_string())
95    }
96}
97
98impl fmt::Display for NwgError {
99    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100        use NwgError::*;
101
102        match self {
103            Unknown => write!(f, "Unknown error. This should never happen"),
104            InitializationError(reason) => write!(f, "Failed to initialize NWG: {:?}", reason),
105            ControlCreationError(reason) => write!(f, "Failed to create a control: {:?}", reason),
106            MenuCreationError(reason) => write!(f, "Failed to create a menu: {:?}", reason),
107            ResourceCreationError(reason) => write!(f, "Failed to create a resource: {:?}", reason),
108            LayoutCreationError(reason) => write!(f, "Failed to create a layout: {:?}", reason),
109            EventsBinding(reason) => write!(f, "Failed to bind events: {:?}", reason),
110
111            #[cfg(feature = "file-dialog")]
112            FileDialogError(reason) => write!(f, "File dialog actions failed: {:?}", reason),
113
114            #[cfg(feature = "image-decoder")]
115            ImageDecoderError(_id, reason) => write!(f, "Image decoder failed: {:?}", reason),
116
117            #[cfg(feature = "winnls")]
118            BadLocale(reason) => write!(f, "Windows locale functions failed: {:?}", reason),
119
120            #[cfg(feature = "plotting")]
121            Plotters(reason) => write!(f, "Plotting canvas function failed: {}", reason),
122        }
123    }
124}
125
126#[cfg(feature = "plotting")]
127impl From<PlottersError> for NwgError {
128    fn from(e: PlottersError) -> Self {
129        NwgError::Plotters(e)
130    }
131}
132
133impl Error for NwgError {}