nvdialog_rs/error.rs
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2022-2025 Aggelos Tselios
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25use thiserror::Error;
26
27/// `Error` is the main type for handling errors in the crate. It's direct C equivalent is
28/// `NvdError` (Located at `include/nvdialog_types.h`, line 53). Errors are converted from the
29/// C side into this crate using the `From<i32>` implementation.
30///
31/// When a function returns this type as an error, you should probably handle it, as NvDialog's design
32/// makes one error persist until fixed.
33/// ## Example
34/// ```rust
35/// use nvdialog_rs::DialogBox;
36/// use nvdialog_rs::DialogType;
37/// use nvdialog_rs::Error;
38///
39/// fn main() {
40/// // notice how we do not call nvdialog_rs::init() ?
41///
42/// if let Err(e) = DialogBox::new("Title", "Message", DialogType::Simple) {
43/// // Will print 'Error creating dialog: Library has not been initialized yet'
44/// eprintln!("Error creating dialog: {}", e.to_string());
45/// }
46/// }
47/// ```
48///
49/// ## Notes
50/// - Stringified explanations of the errors are not done with NvDialog's `nvd_stringify_error()` function,
51/// but using the [`thiserror`](https://crates.io/crates/thiserror) crate instead.
52/// - While you could do this, you shouldn't manually call `Error::from(your_number)`, because the trait is
53/// implemented for internal usage mainly.
54#[derive(Debug, Error, Clone, Copy)]
55pub enum Error {
56 #[error("No error")]
57 NoError = 0x0,
58 #[error("No display found")]
59 NoDisplay = 0xff,
60 #[error("Backend failure occured")]
61 BackendFailed,
62 #[error("Invalid parameter passed")]
63 ParametersError,
64 #[error("Library has not been initialized yet")]
65 NotYetInitialized,
66 #[error("Invalid backend chosen")]
67 InvalidBackend,
68 #[error("Inaccessible file")]
69 InaccessibleFile,
70 #[error("Empty string passed")]
71 EmptyString,
72 #[error("Host has ran out of memory")]
73 OutOfMemory,
74 #[error("Internal NvDialog error")]
75 InternalError,
76 #[error("Already initialized NvDialog")]
77 AlreadyInitialized,
78}
79
80impl From<i32> for Error {
81 fn from(value: i32) -> Error {
82 let mut result: Error = Error::NoError;
83 [
84 Self::NoError,
85 Self::NoDisplay,
86 Self::BackendFailed,
87 Self::ParametersError,
88 Self::NotYetInitialized,
89 Self::InvalidBackend,
90 Self::InaccessibleFile,
91 Self::EmptyString,
92 Self::OutOfMemory,
93 Self::InternalError,
94 Self::AlreadyInitialized,
95 ]
96 .into_iter()
97 .for_each(|member| {
98 if member as i32 == value {
99 result = member;
100 }
101 });
102 result
103 }
104}