streampager/
error.rs

1//! Error types.
2
3use std::ffi::OsStr;
4use std::result::Result as StdResult;
5use std::sync::mpsc::RecvError;
6use std::sync::mpsc::RecvTimeoutError;
7use std::sync::mpsc::SendError;
8use std::sync::mpsc::TryRecvError;
9
10use thiserror::Error;
11
12/// Convenient return type for functions.
13pub type Result<T> = StdResult<T, Error>;
14
15/// Main error type.
16#[derive(Debug, Error)]
17pub enum Error {
18    /// Comes from [Termwiz](https://crates.io/crates/termwiz).
19    #[error("terminal error")]
20    Termwiz(#[source] termwiz::Error),
21
22    /// Comes from [Regex](https://github.com/rust-lang/regex).
23    #[error("regex error")]
24    Regex(#[from] regex::Error),
25
26    /// Generic I/O error.
27    #[error("i/o error")]
28    Io(#[from] std::io::Error),
29
30    /// Keymap-related error.
31    #[error("keymap error")]
32    Keymap(#[from] crate::keymap_error::KeymapError),
33
34    /// Binding-related error.
35    #[error("keybinding error")]
36    Binding(#[from] crate::bindings::BindingError),
37
38    /// Generic formatting error.
39    #[error(transparent)]
40    Fmt(#[from] std::fmt::Error),
41
42    /// Receive error on a channel.
43    #[error("channel error")]
44    ChannelRecv(#[from] RecvError),
45
46    /// Try-receive error on a channel.
47    #[error("channel error")]
48    ChannelTryRecv(#[from] TryRecvError),
49
50    /// Receive-timeout error on a channel.
51    #[error("channel error")]
52    ChannelRecvTimeout(#[from] RecvTimeoutError),
53
54    /// Send error on a channel.
55    #[error("channel error")]
56    ChannelSend,
57
58    /// Error returned if the terminfo database is missing.
59    #[error("terminfo database not found (is $TERM correct?)")]
60    TerminfoDatabaseMissing,
61
62    /// Wrapped error within the context of a command.
63    #[error("error running command '{command}'")]
64    WithCommand {
65        /// Wrapped error.
66        #[source]
67        error: Box<Self>,
68
69        /// Command the error is about.
70        command: String,
71    },
72
73    /// Wrapped error within the context of a file.
74    #[error("error loading file '{file}'")]
75    WithFile {
76        /// Wrapped error.
77        #[source]
78        error: Box<Self>,
79
80        /// File the error is about.
81        file: String,
82    },
83}
84
85impl Error {
86    #[cfg(feature = "load_file")]
87    pub(crate) fn with_file(self, file: impl AsRef<str>) -> Self {
88        Self::WithFile {
89            error: Box::new(self),
90            file: file.as_ref().to_owned(),
91        }
92    }
93
94    pub(crate) fn with_command(self, command: impl AsRef<OsStr>) -> Self {
95        Self::WithCommand {
96            error: Box::new(self),
97            command: command.as_ref().to_string_lossy().to_string(),
98        }
99    }
100}
101
102impl<T> From<SendError<T>> for Error {
103    fn from(_send_error: SendError<T>) -> Error {
104        Error::ChannelSend
105    }
106}