cadmus_core/dictionary/
errors.rs

1//! Errors for the Dict dictionary crate.
2use std::error;
3
4/// Error type, representing the errors which can be returned by the libdict library.
5///
6/// This enum represents a handful of custom errors and wraps `io:::Error` and
7/// `string::FromUtf8Error`.
8#[derive(Debug)]
9pub enum DictError {
10    /// Invalid character, e.g. within the index file; the error contains the erroneous character,
11    /// and optionally line and position.
12    InvalidCharacter(char, Option<usize>, Option<usize>),
13    /// Occurs whenever a line in an index file misses a column.
14    MissingColumnInIndex(usize),
15    /// Invalid file format, contains an explanation an optional path to the
16    /// file with the invalid file format.
17    InvalidFileFormat(String, Option<String>),
18    /// This reports a malicious / malformed index file, which requests a buffer which is too large.
19    MemoryError,
20    /// This reports words which are not present in the dictionary.
21    WordNotFound(String),
22    /// A wrapped io::Error.
23    IoError(::std::io::Error),
24    /// A wrapped Utf8Error.
25    Utf8Error(::std::string::FromUtf8Error),
26    /// Errors thrown by the flate2 crate - not really descriptive errors, though.
27    DeflateError(flate2::DecompressError),
28}
29
30impl ::std::fmt::Display for DictError {
31    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
32        match *self {
33            DictError::IoError(ref e) => e.fmt(f),
34            DictError::Utf8Error(ref e) => e.fmt(f),
35            DictError::DeflateError(ref err) => write!(
36                f,
37                "Error while using \
38                        the flate2 crate: {:?}",
39                err
40            ),
41            DictError::MemoryError => write!(f, "not enough memory available"),
42            DictError::WordNotFound(ref word) => write!(f, "Word not found: {}", word),
43            DictError::InvalidCharacter(ref ch, ref line, ref pos) => {
44                let mut ret = write!(f, "Invalid character {}", ch);
45                if let Some(ln) = *line {
46                    ret = write!(f, " on line {}", ln);
47                }
48                if let Some(pos) = *pos {
49                    ret = write!(f, " at position {}", pos);
50                }
51                ret
52            }
53            DictError::MissingColumnInIndex(ref lnum) => write!(
54                f,
55                "line {}: not \
56                    enough <tab>-separated columns found, expected at least 3",
57                lnum
58            ),
59            DictError::InvalidFileFormat(ref explanation, ref path) => write!(
60                f,
61                "{}{}",
62                path.clone().unwrap_or_else(String::new),
63                explanation
64            ),
65        }
66    }
67}
68
69impl error::Error for DictError {
70    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
71        match *self {
72            DictError::IoError(ref err) => err.source(),
73            DictError::Utf8Error(ref err) => err.source(),
74            _ => None,
75        }
76    }
77}
78
79// Allow seamless coercion from::Error.
80impl From<::std::io::Error> for DictError {
81    fn from(err: ::std::io::Error) -> DictError {
82        DictError::IoError(err)
83    }
84}
85
86impl From<::std::string::FromUtf8Error> for DictError {
87    fn from(err: ::std::string::FromUtf8Error) -> DictError {
88        DictError::Utf8Error(err)
89    }
90}
91
92impl From<flate2::DecompressError> for DictError {
93    fn from(err: flate2::DecompressError) -> DictError {
94        DictError::DeflateError(err)
95    }
96}