Skip to main content

tau_cli_picker/
error.rs

1use std::{fmt, io};
2
3/// Errors returned while rendering or driving an interactive picker.
4#[derive(Debug)]
5pub enum PickerError {
6    /// Underlying terminal or stream I/O failed.
7    Io(io::Error),
8    /// The picker was invoked with no items.
9    Empty,
10    /// The picker had items, but none were selectable.
11    NoEnabledItems,
12    /// The user or input stream cancelled with Escape, Ctrl-C, Ctrl-D/EOF, or
13    /// `q`.
14    Cancelled,
15}
16
17impl fmt::Display for PickerError {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            Self::Io(source) => write!(f, "I/O error: {source}"),
21            Self::Empty => f.write_str("picker has no items"),
22            Self::NoEnabledItems => f.write_str("picker has no enabled items"),
23            Self::Cancelled => f.write_str("picker cancelled"),
24        }
25    }
26}
27impl std::error::Error for PickerError {
28    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
29        match self {
30            Self::Io(source) => Some(source),
31            Self::Empty | Self::NoEnabledItems | Self::Cancelled => None,
32        }
33    }
34}
35
36impl From<io::Error> for PickerError {
37    fn from(source: io::Error) -> Self {
38        Self::Io(source)
39    }
40}