native_windows_gui/
errors.rs

1use std::fmt;
2use std::error::Error;
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
51    pub fn initialization<S: Into<String>>(e: S) -> NwgError {
52        NwgError::InitializationError(e.into())
53    }
54
55    pub fn control_create<S: Into<String>>(e: S) -> NwgError {
56        NwgError::ControlCreationError(e.into())
57    }
58
59    pub fn menu_create<S: Into<String>>(e: S) -> NwgError {
60        NwgError::MenuCreationError(e.into())
61    }
62
63    pub fn resource_create<S: Into<String>>(e: S) -> NwgError {
64        NwgError::ResourceCreationError(e.into())
65    }
66
67    pub fn layout_create<S: Into<String>>(e: S) -> NwgError {
68        NwgError::LayoutCreationError(e.into())
69    }
70
71    pub fn events_binding<S: Into<String>>(e: S) -> NwgError {
72        NwgError::EventsBinding(e.into())
73    }
74
75    #[cfg(feature = "file-dialog")]
76    pub fn file_dialog<S: Into<String>>(e: S) -> NwgError {
77        NwgError::FileDialogError(e.into())
78    }
79
80    #[cfg(feature = "winnls")]
81    pub fn bad_locale<S: Into<String>>(e: S) -> NwgError {
82        NwgError::BadLocale(e.into())
83    }
84
85    #[cfg(feature = "image-decoder")]
86    pub fn image_decoder<S: Into<String>>(code: i32, e: S) -> NwgError {
87        NwgError::ImageDecoderError(code, e.into())
88    }
89
90    pub fn no_parent(name: &'static str) -> NwgError {
91        NwgError::ControlCreationError(format!("No parent defined for {:?} control", name))
92    }
93
94    pub fn no_parent_menu() -> NwgError {
95        NwgError::MenuCreationError("No parent defined for menu".to_string())
96    }
97
98}
99
100impl fmt::Display for NwgError {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        use NwgError::*;
103
104        match self {
105            Unknown => write!(f, "Unknown error. This should never happen"),
106            InitializationError(reason) => write!(f, "Failed to initialize NWG: {:?}", reason),
107            ControlCreationError(reason) => write!(f, "Failed to create a control: {:?}", reason),
108            MenuCreationError(reason) => write!(f, "Failed to create a menu: {:?}", reason),
109            ResourceCreationError(reason) => write!(f, "Failed to create a resource: {:?}", reason),
110            LayoutCreationError(reason) => write!(f, "Failed to create a layout: {:?}", reason),
111            EventsBinding(reason) => write!(f, "Failed to bind events: {:?}", reason),
112            
113            #[cfg(feature = "file-dialog")]
114            FileDialogError(reason) => write!(f, "File dialog actions failed: {:?}", reason),
115
116            #[cfg(feature = "image-decoder")]
117            ImageDecoderError(_id, reason) => write!(f, "Image decoder failed: {:?}", reason),
118
119            #[cfg(feature = "winnls")]
120            BadLocale(reason) => write!(f, "Windows locale functions failed: {:?}", reason),
121
122            #[cfg(feature = "plotting")]
123            Plotters(reason) => write!(f, "Plotting canvas function failed: {}", reason),
124        }
125        
126    }
127}
128
129#[cfg(feature = "plotting")]
130impl From<PlottersError> for NwgError {
131    fn from(e: PlottersError) -> Self {
132        NwgError::Plotters(e)
133    }
134}
135
136impl Error for NwgError {}