cadmus_core/dictionary/
errors.rs1use std::error;
3
4#[derive(Debug)]
9pub enum DictError {
10 InvalidCharacter(char, Option<usize>, Option<usize>),
13 MissingColumnInIndex(usize),
15 InvalidFileFormat(String, Option<String>),
18 MemoryError,
20 WordNotFound(String),
22 IoError(::std::io::Error),
24 Utf8Error(::std::string::FromUtf8Error),
26 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
79impl 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}