Skip to main content

ree/
error.rs

1use rustix::io::Errno;
2use thiserror::Error as ThisError;
3
4/// An error that stopped `ree` from resetting the terminal.
5#[derive(Debug, ThisError)]
6#[non_exhaustive]
7pub enum Error {
8    /// No usable terminal was attached to standard input, output, error, or `/dev/tty`.
9    #[error("no terminal found")]
10    NoTerminal,
11    /// The process is in a background job and must not change another job's terminal.
12    #[error("the process is not in the terminal's foreground process group")]
13    NotForegroundProcess,
14    /// The process could not prove that it owns the terminal foreground.
15    #[error("failed to read the terminal's foreground process group: {0}")]
16    GetForegroundProcess(#[source] Errno),
17    /// The terminal output queue could not be resumed.
18    #[error("failed to resume terminal output: {0}")]
19    ResumeOutput(#[source] Errno),
20    /// The current terminal driver state could not be read.
21    #[error("failed to read terminal state: {0}")]
22    GetTerminalState(#[source] Errno),
23    /// The repaired terminal driver state could not be applied.
24    #[error("failed to set terminal state: {0}")]
25    SetTerminalState(#[source] Errno),
26    /// `TERM` is absent from the process environment.
27    #[error("TERM is not set")]
28    TermNotSet,
29    /// `TERM` contains characters that cannot name a terminfo entry safely.
30    #[error("TERM is not a valid terminal name")]
31    InvalidTermName,
32    /// No compiled terminfo entry was found for `TERM`.
33    #[error("no terminfo entry was found for TERM")]
34    TerminfoNotFound,
35    /// A compiled terminfo entry could not be read.
36    #[error("the terminfo entry for TERM could not be read")]
37    TerminfoUnreadable,
38    /// A compiled terminfo entry was structurally invalid.
39    #[error("the terminfo entry for TERM is invalid")]
40    InvalidTerminfo,
41    /// A write to the terminal failed.
42    #[error("failed to write terminal reset data: {0}")]
43    TerminalWrite(#[source] Errno),
44    /// A terminal write returned zero bytes before all reset data was sent.
45    #[error("a terminal write made no progress")]
46    TerminalWriteMadeNoProgress,
47}
48
49impl Error {
50    /// Return whether writing a diagnostic could violate terminal job control.
51    #[must_use]
52    pub const fn requires_silent_exit(&self) -> bool {
53        matches!(
54            self,
55            Self::NotForegroundProcess | Self::GetForegroundProcess(_)
56        )
57    }
58}