nixterm/terminfo/
errors.rs

1use failure;
2use std::{fmt, result};
3
4pub type Result<T> = result::Result<T, Error>;
5
6/// TermInfo's failure type.
7///
8/// This is a thin wrapper around a `failure::Context`.
9/// To get what actually happened call `Error::kind` to get an `ErrorKind`,
10/// or `<Error as failure::Fail>::cause` to get the underlying error, if there is one (in the case of this library, there usually is).
11#[derive(Debug)]
12pub struct Error {
13    inner: failure::Context<ErrorKind>,
14}
15
16/// Outlines the various points where TermInfo routines may fail.
17///
18/// ErrorKind will almost always be wrapped in an `Error`, and
19/// generally it will be won't make much sense without that error's cause.
20#[derive(Eq, PartialEq, Debug, Fail)]
21pub enum ErrorKind {
22    #[fail(display = "could not find a valid path to the terminfo file.")]
23    FailedToFindTermInfo,
24
25    #[fail(display = "failed to parse the terminfo file.")]
26    FailedToParseFile,
27
28    #[fail(display = "this file is not a terminfo file (bad magic number)")]
29    InvalidMagicNumber,
30
31    #[fail(display = "this file is not a terminfo file (too short)")]
32    IncompleteTermInfo,
33
34    #[fail(display = "the string table has exceeded its maximum capacity for a 16bit file")]
35    MaxStrTabSizeReached,
36
37    #[fail(display = "failed to read from index {}, there are only {} elements!", _0, _1)]
38    OutOfRange(usize, usize),
39
40    #[fail(display = "the file is too short to fit a terminfo header")]
41    IncompleteTermInfoHeader,
42
43    #[fail(display = "failed to read a string from a string table")]
44    FailedToReadStringFromTable,
45
46    #[fail(
47        display = "The file is too short to fit any terminfo extended data, but too long to be only a standard terminfo file."
48    )]
49    IncompleteExtendedTermInfo,
50
51    #[fail(
52        display = "The file is too short to fit any terminfo extended header, but too long to be only a standard terminfo file."
53    )]
54    IncompleteExtendedHeader,
55
56    #[fail(
57        display = "maximum capability count exceeded, there can only be a maximum of 65535 capabilities in each array for 16bit files"
58    )]
59    MaximumCapabilityCountExceeded,
60
61    #[fail(display = "invalid printf format specifier")]
62    BadPrintfSpecifier,
63
64    #[fail(display = "invalid precision number in printf specifier")]
65    BadPrecisionSpecified,
66
67    #[fail(display = "invalid digit in number")]
68    InvalidDigit(u8),
69
70    #[fail(display = "failed to write a string argument")]
71    FailedToWriteArgument,
72
73    #[fail(display = "invalid argument identifier")]
74    InvalidArgumentIdentifier,
75
76    #[fail(display = "unexpected argument type, expected a {}, got a(n) {}", _0, _1)]
77    UnexpectedArgumentType(&'static str, &'static str),
78
79    #[fail(display = "failed to write string literal")]
80    FailedToWriteStringLiteral,
81
82    #[fail(display = "unexpected EOF")]
83    UnexpectedEof,
84
85    #[fail(display = "invalid numeric literal")]
86    InvalidNumber,
87
88    #[fail(display = "invalid character literal")]
89    InvalidChar,
90}
91
92impl Error {
93    /// Get this error's specific kind.
94    pub fn kind(&self) -> &ErrorKind {
95        self.inner.get_context()
96    }
97}
98
99impl failure::Fail for Error {
100    fn cause(&self) -> Option<&failure::Fail> {
101        self.inner.cause()
102    }
103
104    fn backtrace(&self) -> Option<&failure::Backtrace> {
105        self.inner.backtrace()
106    }
107}
108
109impl fmt::Display for Error {
110    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
111        fmt::Display::fmt(&self.inner, f)
112    }
113}
114
115impl From<ErrorKind> for Error {
116    fn from(kind: ErrorKind) -> Error {
117        Error {
118            inner: failure::Context::new(kind),
119        }
120    }
121}
122
123impl From<failure::Context<ErrorKind>> for Error {
124    fn from(inner: failure::Context<ErrorKind>) -> Error {
125        Error { inner: inner }
126    }
127}