1use crate::{PromptError, minus_core::commands::Command};
4use std::io;
5
6#[derive(Debug, thiserror::Error)]
8#[error(transparent)]
9#[allow(clippy::module_name_repetitions)]
10pub struct TermError(
11 #[from] io::Error,
13);
14
15#[derive(Debug, thiserror::Error)]
17#[error(transparent)]
18#[allow(clippy::module_name_repetitions)]
19#[cfg(feature = "search")]
20#[cfg_attr(docsrs, doc(cfg(feature = "search")))]
21pub struct RegexError(
22 #[from] regex::Error,
24);
25
26#[derive(Debug, thiserror::Error)]
28#[allow(clippy::module_name_repetitions)]
29pub enum SetupError {
30 #[error("The standard output is not a valid terminal")]
31 InvalidTerminal,
32
33 #[error("Failed to switch to alternate screen")]
34 AlternateScreen(#[source] TermError),
35
36 #[error("Failed to enable raw mode")]
37 RawMode(#[source] TermError),
38
39 #[error("Failed to hide the cursor")]
40 HideCursor(#[source] TermError),
41
42 #[error("Failed to enable mouse capture")]
43 EnableMouseCapture(#[source] TermError),
44
45 #[error("Couldn't determine the terminal size")]
46 TerminalSize(#[source] TermError),
47}
48
49#[derive(Debug, thiserror::Error)]
51#[allow(clippy::module_name_repetitions)]
52pub enum CleanupError {
53 #[error("Failed to disable mouse capture")]
54 DisableMouseCapture(#[source] TermError),
55
56 #[error("Failed to show the cursor")]
57 ShowCursor(#[source] TermError),
58
59 #[error("Failed to disable raw mode")]
60 DisableRawMode(#[source] TermError),
61
62 #[error("Failed to switch back to main screen")]
63 LeaveAlternateScreen(#[source] TermError),
64}
65
66#[derive(Debug, thiserror::Error)]
68#[allow(clippy::module_name_repetitions)]
69pub enum MinusError {
70 #[error("Failed to initialize the terminal")]
71 Setup(#[from] SetupError),
72
73 #[error("Failed to clean up the terminal")]
74 Cleanup(#[from] CleanupError),
75
76 #[error("Failed to draw the new data")]
77 Draw(#[from] std::io::Error),
78
79 #[error("Failed to handle terminal event")]
80 HandleEvent(#[source] TermError),
81
82 #[error("Failed to do an operation on the cursor")]
83 Cursor(#[from] TermError),
84
85 #[error("Failed to send formatted data to the pager")]
86 FmtWriteError(#[from] std::fmt::Error),
87
88 #[error("Failed to send data to the receiver")]
89 Communication(#[from] crossbeam_channel::SendError<Command>),
90
91 #[error("Failed to convert between some primitives")]
92 Conversion,
93
94 #[error(transparent)]
95 Prompt(#[from] PromptError),
96
97 #[error(transparent)]
98 #[cfg(feature = "search")]
99 #[cfg_attr(docsrs, doc(cfg(feature = "search")))]
100 SearchExpError(#[from] RegexError),
101
102 #[cfg(test)]
103 #[error(transparent)]
104 #[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
105 JoinError(#[from] tokio::task::JoinError),
106}
107
108#[cfg(feature = "search")]
109impl From<regex::Error> for MinusError {
110 fn from(e: regex::Error) -> Self {
111 Self::SearchExpError(RegexError::from(e))
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118 use std::error::Error;
119
120 #[test]
121 fn terminal_error_chains_preserve_the_system_cause() {
122 let failure = || {
123 TermError::from(io::Error::new(
124 io::ErrorKind::PermissionDenied,
125 "access denied",
126 ))
127 };
128 let setup = [
129 SetupError::AlternateScreen,
130 SetupError::RawMode,
131 SetupError::HideCursor,
132 SetupError::EnableMouseCapture,
133 SetupError::TerminalSize,
134 ]
135 .map(|wrap| MinusError::Setup(wrap(failure())));
136 let cleanup = [
137 CleanupError::DisableMouseCapture,
138 CleanupError::ShowCursor,
139 CleanupError::DisableRawMode,
140 CleanupError::LeaveAlternateScreen,
141 ]
142 .map(|wrap| MinusError::Cleanup(wrap(failure())));
143 for error in setup
144 .into_iter()
145 .chain(cleanup)
146 .chain([MinusError::HandleEvent(failure())])
147 {
148 let mut cause: &dyn Error = &error;
149 while let Some(source) = cause.source() {
150 cause = source;
151 }
152 assert_eq!(cause.to_string(), "access denied", "{error:?}");
153 }
154 }
155}