clipboard_watcher/
error.rs1use std::convert::Infallible;
2
3use thiserror::Error;
4
5use crate::*;
6
7pub(crate) trait WithContext<T> {
8 fn context(self, msg: &str) -> Result<T, String>;
9}
10
11impl<T, E: Display> WithContext<T> for Result<T, E> {
12 fn context(self, msg: &str) -> Result<T, String> {
13 self.map_err(|e| format!("{msg}: {e}"))
14 }
15}
16
17impl<T> WithContext<T> for Option<T> {
18 fn context(self, msg: &str) -> Result<T, String> {
19 self.ok_or_else(|| msg.to_string())
20 }
21}
22
23#[derive(Clone, Debug, Error)]
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26#[cfg_attr(feature = "serde", serde(transparent))]
27#[error("Failed to start clipboard monitor: {0}")]
28pub struct InitializationError(pub String);
29
30impl From<Infallible> for InitializationError {
31 #[inline(never)]
32 #[cold]
33 fn from(value: Infallible) -> Self {
34 match value {}
35 }
36}
37
38#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
40#[derive(Clone, Debug, Error)]
41#[non_exhaustive]
42pub enum ClipboardError {
43 #[error("Failed to monitor the clipboard: {0}")]
44 MonitorFailed(String),
45
46 #[error("Failed to read the clipboard: {0}")]
47 ReadError(String),
48
49 #[error("The content of the clipboard did not match any supported format")]
50 NoMatchingFormat,
51}
52
53impl From<Infallible> for ClipboardError {
54 fn from(value: Infallible) -> Self {
55 match value {}
56 }
57}
58
59pub(crate) enum ErrorWrapper {
60 EmptyContent,
61 SizeTooLarge,
62 ReadError(ClipboardError),
63 UserSkipped,
64}
65
66impl From<ClipboardError> for ErrorWrapper {
67 #[inline]
68 fn from(value: ClipboardError) -> Self {
69 Self::ReadError(value)
70 }
71}
72
73pub type ClipboardResult = Result<Arc<Body>, ClipboardError>;