tcalc_rustyline/
error.rs

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