-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy patherror.rs
More file actions
57 lines (49 loc) · 1.48 KB
/
error.rs
File metadata and controls
57 lines (49 loc) · 1.48 KB
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
use std::fmt;
use std::io::Error as StdIoError;
use std::result::Result as StdResult;
use crate::parser;
use crate::encoder;
/// Error type for any error within this library.
///
/// This type is not intended to be matched exhaustively as new variants
/// may be added in future without a version bump.
#[derive(Debug)]
pub enum Error {
InvalidStr,
NoFilename,
IncompleteSection,
Io(StdIoError),
Parser(parser::Error),
Encoder(encoder::Error),
#[doc(hidden)]
__Nonexhaustive,
}
pub type Result<T> = StdResult<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", match *self {
Error::InvalidStr => "invalid str".to_string(),
Error::NoFilename => "a filename must be provided".to_string(),
Error::IncompleteSection => "a section wasn't completed".to_string(), // Is there a better way to put this?
Error::Io(ref err) => err.to_string(),
Error::Parser(ref err) => err.to_string(),
Error::Encoder(ref err) => err.to_string(),
Error::__Nonexhaustive => unreachable!(),
})
}
}
impl From<StdIoError> for Error {
fn from(err: StdIoError) -> Error {
Error::Io(err)
}
}
impl From<parser::Error> for Error {
fn from(err: parser::Error) -> Error {
Error::Parser(err)
}
}
impl From<encoder::Error> for Error {
fn from(err: encoder::Error) -> Error {
Error::Encoder(err)
}
}