rustyline/
error.rs

1//! Contains error type for handling I/O and Errno errors
2#[cfg(all(unix, not(any(target_os = "fuchsia"))))]
3use nix;
4#[cfg(windows)]
5use std::char;
6use std::error;
7use std::fmt;
8use std::io;
9use std::str;
10
11/// The error type for Rustyline errors that can arise from
12/// I/O related errors or Errno when using the nix-rust library
13/// #[non_exhaustive]
14#[derive(Debug)]
15pub enum ReadlineError {
16    /// I/O Error
17    Io(io::Error),
18    /// EOF (Ctrl-D)
19    Eof,
20    /// Ctrl-C
21    Interrupted,
22    /// Chars Error
23    #[cfg(unix)]
24    Utf8Error,
25    /// Unix Error from syscall
26    #[cfg(all(unix, not(any(target_os = "fuchsia"))))]
27    Errno(nix::Error),
28    #[cfg(windows)]
29    WindowResize,
30    #[cfg(windows)]
31    Decode(char::DecodeUtf16Error),
32}
33
34impl fmt::Display for ReadlineError {
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36        match *self {
37            ReadlineError::Io(ref err) => err.fmt(f),
38            ReadlineError::Eof => write!(f, "EOF"),
39            ReadlineError::Interrupted => write!(f, "Interrupted"),
40            #[cfg(unix)]
41            ReadlineError::Utf8Error => write!(f, "invalid utf-8: corrupt contents"),
42            #[cfg(all(unix, not(any(target_os = "fuchsia"))))]
43            ReadlineError::Errno(ref err) => err.fmt(f),
44            #[cfg(windows)]
45            ReadlineError::WindowResize => write!(f, "WindowResize"),
46            #[cfg(windows)]
47            ReadlineError::Decode(ref err) => err.fmt(f),
48        }
49    }
50}
51
52impl error::Error for ReadlineError {
53    fn description(&self) -> &str {
54        match *self {
55            ReadlineError::Io(ref err) => err.description(),
56            ReadlineError::Eof => "EOF",
57            ReadlineError::Interrupted => "Interrupted",
58            #[cfg(unix)]
59            ReadlineError::Utf8Error => "invalid utf-8: corrupt contents",
60            #[cfg(all(unix, not(any(target_os = "fuchsia"))))]
61            ReadlineError::Errno(ref err) => err.description(),
62            #[cfg(windows)]
63            ReadlineError::WindowResize => "WindowResize",
64            #[cfg(windows)]
65            ReadlineError::Decode(ref err) => err.description(),
66        }
67    }
68}
69
70impl From<io::Error> for ReadlineError {
71    fn from(err: io::Error) -> ReadlineError {
72        ReadlineError::Io(err)
73    }
74}
75
76#[cfg(all(unix, not(any(target_os = "fuchsia"))))]
77impl From<nix::Error> for ReadlineError {
78    fn from(err: nix::Error) -> ReadlineError {
79        ReadlineError::Errno(err)
80    }
81}
82
83#[cfg(windows)]
84impl From<char::DecodeUtf16Error> for ReadlineError {
85    fn from(err: char::DecodeUtf16Error) -> ReadlineError {
86        ReadlineError::Decode(err)
87    }
88}