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
use std::error::Error as StdError;
use std::fmt::{self, Display, Formatter};
use std::io::{self, ErrorKind};
use std::sync::PoisonError;
#[derive(Debug)]
pub enum Error {
Io(io::Error),
Poison(String),
FileType,
Version(u32),
UnsupportedFeature(String),
FileFormat(String),
Internal(String),
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::Io(err)
}
}
impl<T> From<PoisonError<T>> for Error {
fn from(err: PoisonError<T>) -> Error {
Error::Poison(format!("{}", err))
}
}
impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::Io(ref err) => err.description(),
Error::FileType => "Not a qcow2 file",
Error::Version(_) => "Unsupported version",
Error::UnsupportedFeature(_) => "Unsupported feature",
Error::FileFormat(_) => "Malformed qcow2 file",
Error::Internal(_) => "Internal error",
Error::Poison(ref s) => s,
}
}
fn cause(&self) -> Option<&StdError> {
match *self {
Error::Io(ref err) => Some(err),
_ => None,
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
Error::Io(ref err) => err.fmt(f),
Error::Version(found) => write!(f, "Unsupported version {}", found),
Error::UnsupportedFeature(ref feat) => write!(f, "Unsupported feature: {}", feat),
Error::FileFormat(ref err) => write!(f, "Malformed qcow2 file: {}", err),
Error::Internal(ref err) => write!(f, "Internal error: {}", err),
_ => f.write_str(self.description()),
}
}
}
impl From<Error> for io::Error {
fn from(err: Error) -> io::Error {
io::Error::new(ErrorKind::Other, err)
}
}