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

#[cfg(unix)]
use char_iter;

/// The error type for Rustyline errors that can arise from
/// I/O related errors or Errno when using the nix-rust library
#[derive(Debug)]
pub enum ReadlineError {
    /// I/O Error
    Io(io::Error),
    /// EOF (Ctrl-D)
    Eof,
    /// Ctrl-C
    Cancelled,
    /// Ctrl-C
    Interrupted,
    /// Chars Error
    #[cfg(unix)]
    Char(char_iter::CharsError),
    /// Unix Error from syscall
    #[cfg(unix)]
    Errno(nix::Error),
    #[cfg(windows)]
    WindowResize,
    #[cfg(windows)]
    Decode(char::DecodeUtf16Error),
}

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::Cancelled => write!(f, "Cancelled"),
            ReadlineError::Interrupted => write!(f, "Interrupted"),
            #[cfg(unix)]
            ReadlineError::Char(ref err) => err.fmt(f),
            #[cfg(unix)]
            ReadlineError::Errno(ref err) => write!(f, "Errno: {}", err.errno().desc()),
            #[cfg(windows)]
            ReadlineError::WindowResize => write!(f, "WindowResize"),
            #[cfg(windows)]
            ReadlineError::Decode(ref err) => err.fmt(f),
        }
    }
}

impl error::Error for ReadlineError {
    fn description(&self) -> &str {
        match *self {
            ReadlineError::Io(ref err) => err.description(),
            ReadlineError::Eof => "EOF",
            ReadlineError::Cancelled => "Cancelled",
            ReadlineError::Interrupted => "Interrupted",
            #[cfg(unix)]
            ReadlineError::Char(ref err) => err.description(),
            #[cfg(unix)]
            ReadlineError::Errno(ref err) => err.errno().desc(),
            #[cfg(windows)]
            ReadlineError::WindowResize => "WindowResize",
            #[cfg(windows)]
            ReadlineError::Decode(ref err) => err.description(),
        }
    }
}

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

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

#[cfg(unix)]
impl From<char_iter::CharsError> for ReadlineError {
    fn from(err: char_iter::CharsError) -> ReadlineError {
        ReadlineError::Char(err)
    }
}

#[cfg(windows)]
impl From<char::DecodeUtf16Error> for ReadlineError {
    fn from(err: char::DecodeUtf16Error) -> ReadlineError {
        ReadlineError::Decode(err)
    }
}