hypersig_desktop/
error.rs1use std::error::Error as StdError;
4use std::fmt;
5use std::io;
6
7pub struct Error {
14 kind: ErrorKind,
15 source: Option<Box<dyn StdError + Send + Sync>>,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20#[non_exhaustive]
21pub enum ErrorKind {
22 NoDataDir,
24 AlreadyRunning,
26 Setup,
28 Window,
30 Io,
32 Closed,
34 Smoke,
36 Clipboard,
38 Notification,
40 Autostart,
43 Tray,
45 Shortcut,
48 NotExternal,
52 Update,
54 InRuntime,
58}
59
60impl Error {
61 #[must_use]
63 pub fn kind(&self) -> ErrorKind {
64 self.kind
65 }
66
67 pub(crate) fn no_data_dir() -> Self {
68 Self {
69 kind: ErrorKind::NoDataDir,
70 source: None,
71 }
72 }
73
74 pub(crate) fn already_running() -> Self {
75 Self {
76 kind: ErrorKind::AlreadyRunning,
77 source: None,
78 }
79 }
80
81 pub(crate) fn in_runtime() -> Self {
82 Self {
83 kind: ErrorKind::InRuntime,
84 source: None,
85 }
86 }
87
88 pub(crate) fn closed() -> Self {
89 Self {
90 kind: ErrorKind::Closed,
91 source: None,
92 }
93 }
94
95 pub(crate) fn smoke(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
96 Self {
97 kind: ErrorKind::Smoke,
98 source: Some(why.into()),
99 }
100 }
101
102 pub(crate) fn clipboard(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
103 Self {
104 kind: ErrorKind::Clipboard,
105 source: Some(why.into()),
106 }
107 }
108
109 pub(crate) fn shortcut(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
110 Self {
111 kind: ErrorKind::Shortcut,
112 source: Some(why.into()),
113 }
114 }
115
116 pub(crate) fn autostart(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
117 Self {
118 kind: ErrorKind::Autostart,
119 source: Some(why.into()),
120 }
121 }
122
123 pub(crate) fn tray(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
124 Self {
125 kind: ErrorKind::Tray,
126 source: Some(why.into()),
127 }
128 }
129
130 pub(crate) fn notification(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
131 Self {
132 kind: ErrorKind::Notification,
133 source: Some(why.into()),
134 }
135 }
136
137 #[cfg(feature = "updater")]
138 pub(crate) fn update(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
139 Self {
140 kind: ErrorKind::Update,
141 source: Some(why.into()),
142 }
143 }
144
145 pub(crate) fn not_external() -> Self {
146 Self {
147 kind: ErrorKind::NotExternal,
148 source: None,
149 }
150 }
151
152 pub(crate) fn setup(source: Box<dyn StdError + Send + Sync>) -> Self {
153 Self {
154 kind: ErrorKind::Setup,
155 source: Some(source),
156 }
157 }
158
159 pub(crate) fn window(source: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
160 Self {
161 kind: ErrorKind::Window,
162 source: Some(source.into()),
163 }
164 }
165}
166
167impl From<io::Error> for Error {
168 fn from(source: io::Error) -> Self {
169 Self {
170 kind: ErrorKind::Io,
171 source: Some(source.into()),
172 }
173 }
174}
175
176impl fmt::Display for Error {
177 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178 let what = match self.kind {
179 ErrorKind::NoDataDir => {
180 "no directory for the application's files: HOME, LOCALAPPDATA, or XDG_DATA_HOME is unset or not an absolute path"
181 }
182 ErrorKind::AlreadyRunning => "another copy of this application is running",
183 ErrorKind::Setup => "the application's setup failed",
184 ErrorKind::Window => "the window could not be shown",
185 ErrorKind::Io => "the application's files or server failed",
186 ErrorKind::Closed => "the application is closing",
187 ErrorKind::InRuntime => {
188 "`App::run` was called inside a Tokio runtime; call it from a `main` without `#[tokio::main]`"
189 }
190 ErrorKind::Smoke => "the smoke run failed",
191 ErrorKind::Clipboard => "the clipboard could not be used",
192 ErrorKind::Notification => "the notification could not be shown",
193 ErrorKind::Tray => "the tray icon could not be shown",
194 ErrorKind::Autostart => "the start at login could not be read or changed",
195 ErrorKind::Shortcut => "the global shortcut could not be registered",
196 ErrorKind::Update => "the new version could not be found or installed",
197 ErrorKind::NotExternal => {
198 "the URL names no scheme, so it is not opened in another program"
199 }
200 };
201 match &self.source {
202 Some(source) => write!(f, "{what}: {source}"),
203 None => f.write_str(what),
204 }
205 }
206}
207
208impl fmt::Debug for Error {
211 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212 fmt::Display::fmt(self, f)
213 }
214}
215
216impl StdError for Error {
217 fn source(&self) -> Option<&(dyn StdError + 'static)> {
218 Some(self.source.as_deref()?)
219 }
220}