-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patherror.rs
129 lines (107 loc) · 3.58 KB
/
error.rs
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
use std::cell::{BorrowError, BorrowMutError};
use std::error::Error as StdError;
use std::fmt::{Debug, Display};
pub type MyResult<T> = Result<T, MyError>;
#[derive(thiserror::Error, Debug)]
pub enum MyError {
/// Only use this catchall to deal with unexpected errors that are very
/// difficult to deal with in another way.
#[error("Unknown error: {0}")]
Generic(#[from] Box<dyn StdError>),
/// High level error to represent the idea that the application is crashing,
/// indicating the cause(s) of that crash.
#[error("Application crash '{msg}': {cause:#?}")]
Crash { msg: String, cause: Vec<MyError> },
#[error("failed to get wlr clipboard: {0}")]
WlcrsPaste(#[from] wl_clipboard_rs::paste::Error),
#[error("failed to set wlr clipboard: {0}")]
WlcrsCopy(#[from] wl_clipboard_rs::copy::Error),
#[error("{0}")]
Io(#[from] std::io::Error),
#[error("{0}")]
TerminalClipboard(#[from] StandardizedError<terminal_clipboard::ClipboardError>),
#[error("{0}")]
Arboard(#[from] arboard::Error),
#[error("No clipboards.")]
NoClipboards,
#[error("{0}")]
BorrowError(#[from] BorrowError),
#[error("{0}")]
BorrowMutError(#[from] BorrowMutError),
}
#[derive(Debug)]
pub struct StandardizedError<E: Debug> {
pub inner: E,
pub stdio: Option<StdIo>,
}
impl<E: Debug> Display for StandardizedError<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(self, f)
}
}
impl<E: Debug> StdError for StandardizedError<E> {}
pub trait Standardize<T, E: Sized + Debug>: Sized {
/// Convert any Sized + Debug into a std::error::Error
fn standardize(self) -> Result<T, StandardizedError<E>>;
}
impl<T, E: Sized + Debug> Standardize<T, E> for Result<T, E> {
fn standardize(self) -> Result<T, StandardizedError<E>> {
match self {
Ok(ok) => Ok(ok),
Err(err) => Err(StandardizedError {
inner: err,
stdio: None,
}),
}
}
}
impl<T, E: Sized + Debug> Standardize<T, E> for (Result<T, E>, StdIo) {
fn standardize(self) -> Result<T, StandardizedError<E>> {
match self.0 {
Ok(ok) => Ok(ok),
Err(err) => Err(StandardizedError {
inner: err,
stdio: Some(self.1),
}),
}
}
}
pub trait Generify<T, E: 'static + StdError> {
/// Convert any std::error::Error into MyError::Generic
fn generify(self) -> Result<T, MyError>;
}
impl<T, E: 'static + StdError> Generify<T, E> for Result<T, E> {
fn generify(self) -> Result<T, MyError> {
match self {
Ok(ok) => Ok(ok),
Err(err) => Err(MyError::Generic(Box::new(err))),
}
}
}
#[allow(unused)]
macro_rules! stdio {
($block:expr) => {{
let mut stdout_redirect = gag::BufferRedirect::stdout().unwrap();
let mut stderr_redirect = gag::BufferRedirect::stderr().unwrap();
let ret = $block;
let mut stdout = String::new();
let mut stderr = String::new();
stdout_redirect.read_to_string(&mut stdout).unwrap();
stderr_redirect.read_to_string(&mut stderr).unwrap();
(ret, crate::error::StdIo { stdout, stderr })
}};
}
#[allow(unused)]
pub(crate) use stdio;
#[derive(Debug, Default, PartialEq, Eq)]
pub struct StdIo {
pub stdout: String,
pub stderr: String,
}
impl StdIo {
#[allow(unused)]
pub fn extend(&mut self, other: StdIo) {
self.stdout = format!("{}{}", self.stdout, other.stdout);
self.stderr = format!("{}{}", self.stderr, other.stderr);
}
}