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
130
131
132
133
//! Contains error type for handling I/O and Errno errors
#[cfg(windows)]
use std::char;
use std::error::Error;
use std::fmt;
use std::io;

/// The error type for Rustyline errors that can arise from
/// I/O related errors or Errno when using the nix-rust library
// #[non_exhaustive]
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[non_exhaustive]
pub enum ReadlineError {
    /// I/O Error
    Io(io::Error),
    /// EOF (VEOF / Ctrl-D)
    Eof,
    /// Interrupt signal (VINTR / VQUIT / Ctrl-C)
    Interrupted,
    /// Unix Error from syscall
    #[cfg(unix)]
    Errno(nix::Error),
    /// Error generated on WINDOW_BUFFER_SIZE_EVENT / SIGWINCH signal
    WindowResized,
    /// Like Utf8Error on unix
    #[cfg(windows)]
    Decode(char::DecodeUtf16Error),
    /// Something went wrong calling a Windows API
    #[cfg(windows)]
    SystemError(clipboard_win::SystemError),
}

impl fmt::Display for ReadlineError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            ReadlineError::Io(ref err) => err.fmt(f),
            ReadlineError::Eof => write!(f, "EOF"),
            ReadlineError::Interrupted => write!(f, "Interrupted"),
            #[cfg(unix)]
            ReadlineError::Errno(ref err) => err.fmt(f),
            ReadlineError::WindowResized => write!(f, "WindowResized"),
            #[cfg(windows)]
            ReadlineError::Decode(ref err) => err.fmt(f),
            #[cfg(windows)]
            ReadlineError::SystemError(ref err) => err.fmt(f),
        }
    }
}

impl Error for ReadlineError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match *self {
            ReadlineError::Io(ref err) => Some(err),
            ReadlineError::Eof => None,
            ReadlineError::Interrupted => None,
            #[cfg(unix)]
            ReadlineError::Errno(ref err) => Some(err),
            ReadlineError::WindowResized => None,
            #[cfg(windows)]
            ReadlineError::Decode(ref err) => Some(err),
            #[cfg(windows)]
            ReadlineError::SystemError(_) => None,
        }
    }
}

#[cfg(unix)]
#[derive(Debug)]
pub(crate) struct WindowResizedError;
#[cfg(unix)]
impl fmt::Display for WindowResizedError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "WindowResized")
    }
}
#[cfg(unix)]
impl Error for WindowResizedError {}

impl From<io::Error> for ReadlineError {
    fn from(err: io::Error) -> Self {
        #[cfg(unix)]
        if err.kind() == io::ErrorKind::Interrupted {
            if let Some(e) = err.get_ref() {
                if e.downcast_ref::<WindowResizedError>().is_some() {
                    return ReadlineError::WindowResized;
                }
            }
        }
        ReadlineError::Io(err)
    }
}

impl From<io::ErrorKind> for ReadlineError {
    fn from(kind: io::ErrorKind) -> Self {
        ReadlineError::Io(io::Error::from(kind))
    }
}

#[cfg(unix)]
impl From<nix::Error> for ReadlineError {
    fn from(err: nix::Error) -> Self {
        ReadlineError::Errno(err)
    }
}

#[cfg(windows)]
impl From<char::DecodeUtf16Error> for ReadlineError {
    fn from(err: char::DecodeUtf16Error) -> Self {
        ReadlineError::Io(io::Error::new(io::ErrorKind::InvalidData, err))
    }
}

#[cfg(windows)]
impl From<std::string::FromUtf8Error> for ReadlineError {
    fn from(err: std::string::FromUtf8Error) -> Self {
        ReadlineError::Io(io::Error::new(io::ErrorKind::InvalidData, err))
    }
}

#[cfg(unix)]
impl From<fmt::Error> for ReadlineError {
    fn from(err: fmt::Error) -> Self {
        ReadlineError::Io(io::Error::new(io::ErrorKind::Other, err))
    }
}

#[cfg(windows)]
impl From<clipboard_win::SystemError> for ReadlineError {
    fn from(err: clipboard_win::SystemError) -> Self {
        ReadlineError::SystemError(err)
    }
}