1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::char::DecodeUtf16Error;
use std::fmt;
use std::io;

/// The error raised by this library.
#[derive(Debug)]
pub struct Error {
    kind: ErrorKind,
}

impl Error {
    /// Construct a new error.
    pub(super) fn new<K>(kind: K) -> Self
    where
        ErrorKind: From<K>,
    {
        Self { kind: kind.into() }
    }
}

impl From<ErrorKind> for Error {
    #[inline]
    fn from(kind: ErrorKind) -> Self {
        Self { kind }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            ErrorKind::WindowSetup(..) => write!(f, "Failed to set up window"),
            ErrorKind::ThreadError(..) => write!(f, "Error in window thread"),
            ErrorKind::ClipboardPoll(..) => write!(f, "Failed to poll clipboard"),
            ErrorKind::DeleteRegistryKey(..) => write!(f, "Failed to delete registry key"),
            ErrorKind::GetRegistryValue(..) => write!(f, "Failed to get registry value"),
            ErrorKind::SetRegistryKey(..) => write!(f, "Failed to set registry key"),
            ErrorKind::CurrentExecutable(..) => write!(f, "Could not get current executable"),
            ErrorKind::BuildPopupMenu(..) => write!(f, "Failed to build popup menu"),
            ErrorKind::SetupIcons(..) => write!(f, "Failed to setup icons"),
            ErrorKind::SetupMenu(..) => write!(f, "Failed to setup menu"),
            ErrorKind::ModifyMenuItem(..) => write!(f, "Failed to modify menu item"),
            ErrorKind::AddNotification(..) => write!(f, "Failed to add notification area"),
            ErrorKind::ModifyNotification(..) => write!(f, "Failed to modify notification area"),
            ErrorKind::SendNotification(..) => write!(f, "Failed to send notification"),
            ErrorKind::CreateMutex(..) => write!(f, "Failed to construct mutex"),
            ErrorKind::OpenRegistryKey(..) => write!(f, "Failed to open registry key"),
            ErrorKind::MissingNotification => write!(f, "Missing notification state"),
            ErrorKind::BadAutoStartExecutable(..) => write!(f, "Bad autostart executable"),
            ErrorKind::BadAutoStartArgument(..) => write!(f, "Bad autostart argument"),
            ErrorKind::WindowClosed => write!(f, "Window has been closed"),
            ErrorKind::PostMessageDestroy => write!(f, "Failed to post destroy window message"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.kind {
            ErrorKind::WindowSetup(error) => Some(error),
            ErrorKind::ThreadError(error) => Some(error),
            ErrorKind::ClipboardPoll(error) => Some(error),
            ErrorKind::DeleteRegistryKey(error) => Some(error),
            ErrorKind::GetRegistryValue(error) => Some(error),
            ErrorKind::SetRegistryKey(error) => Some(error),
            ErrorKind::CurrentExecutable(error) => Some(error),
            ErrorKind::BuildPopupMenu(error) => Some(error),
            ErrorKind::SetupIcons(error) => Some(error),
            ErrorKind::SetupMenu(error) => Some(error),
            ErrorKind::ModifyMenuItem(error) => Some(error),
            ErrorKind::AddNotification(error) => Some(error),
            ErrorKind::ModifyNotification(error) => Some(error),
            ErrorKind::SendNotification(error) => Some(error),
            ErrorKind::CreateMutex(error) => Some(error),
            ErrorKind::OpenRegistryKey(error) => Some(error),
            ErrorKind::BadAutoStartExecutable(error) => Some(error),
            ErrorKind::BadAutoStartArgument(error) => Some(error),
            _ => None,
        }
    }
}

#[derive(Debug)]
pub(super) enum WindowError {
    Init(io::Error),
    AddClipboardFormatListener(io::Error),
    OpenClipboard(io::Error),
    GetClipboardData(io::Error),
    LockClipboardData(io::Error),
    ClassNameTooLong(usize),
    ThreadPanicked,
    ThreadExited,
}

impl fmt::Display for WindowError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            WindowError::Init(..) => write!(f, "Failed to initialize window"),
            WindowError::AddClipboardFormatListener(..) => {
                write!(f, "Failed to add clipboard format listener")
            }
            WindowError::OpenClipboard(..) => write!(f, "Failed to open clipboard"),
            WindowError::GetClipboardData(..) => write!(f, "Failed to get clipboard data"),
            WindowError::LockClipboardData(..) => write!(f, "Failed to lock clipboard data"),
            WindowError::ClassNameTooLong(len) => write!(
                f,
                "Class name of length {len} is longer than maximum of 256 bytes"
            ),
            WindowError::ThreadPanicked => write!(f, "Window thread panicked"),
            WindowError::ThreadExited => write!(f, "Window thread unexpectedly exited"),
        }
    }
}

impl std::error::Error for WindowError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            WindowError::Init(error) => Some(error),
            WindowError::AddClipboardFormatListener(error) => Some(error),
            WindowError::OpenClipboard(error) => Some(error),
            WindowError::GetClipboardData(error) => Some(error),
            WindowError::LockClipboardData(error) => Some(error),
            WindowError::ClassNameTooLong(..) => None,
            WindowError::ThreadPanicked => None,
            WindowError::ThreadExited => None,
        }
    }
}

#[derive(Debug)]
pub(super) enum ErrorKind {
    WindowSetup(WindowError),
    ThreadError(WindowError),
    ClipboardPoll(WindowError),
    DeleteRegistryKey(io::Error),
    GetRegistryValue(io::Error),
    SetRegistryKey(io::Error),
    CurrentExecutable(io::Error),
    BuildPopupMenu(io::Error),
    SetupIcons(SetupIconsError),
    SetupMenu(SetupMenuError),
    ModifyMenuItem(io::Error),
    AddNotification(io::Error),
    ModifyNotification(io::Error),
    SendNotification(io::Error),
    CreateMutex(io::Error),
    OpenRegistryKey(io::Error),
    MissingNotification,
    BadAutoStartExecutable(DecodeUtf16Error),
    BadAutoStartArgument(DecodeUtf16Error),
    WindowClosed,
    PostMessageDestroy,
}

#[derive(Debug)]
pub(super) enum SetupIconsError {
    BuildIcon(io::Error),
}

impl fmt::Display for SetupIconsError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::BuildIcon(..) => write!(f, "Failed to construct icon"),
        }
    }
}

impl std::error::Error for SetupIconsError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::BuildIcon(error) => Some(error),
        }
    }
}

#[derive(Debug)]
pub(super) enum SetupMenuError {
    AddMenuEntry(usize, io::Error),
    AddMenuSeparator(usize, io::Error),
}

impl fmt::Display for SetupMenuError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::AddMenuEntry(index, ..) => {
                write!(f, "Failed to add menu entry {index}")
            }
            Self::AddMenuSeparator(index, ..) => {
                write!(f, "Failed to add menu separator {index}")
            }
        }
    }
}

impl std::error::Error for SetupMenuError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::AddMenuEntry(_, error) => Some(error),
            Self::AddMenuSeparator(_, error) => Some(error),
        }
    }
}