1use std::fmt;
5use std::io;
6
7#[derive(Debug)]
8pub enum Error {
9 Io(io::Error),
11 ShortRead {
13 offset: u64,
14 want: usize,
15 got: usize,
16 },
17 ReadOnly,
19 OutOfBounds { offset: u64, len: u64, size: u64 },
21 Custom(String),
24}
25
26impl fmt::Display for Error {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 match self {
29 Error::Io(e) => write!(f, "io: {e}"),
30 Error::ShortRead { offset, want, got } => {
31 write!(f, "short read at {offset}: wanted {want} got {got}")
32 }
33 Error::ReadOnly => write!(f, "device is read-only"),
34 Error::OutOfBounds { offset, len, size } => {
35 write!(f, "{offset}+{len} past device size {size}")
36 }
37 Error::Custom(s) => f.write_str(s),
38 }
39 }
40}
41
42impl std::error::Error for Error {
43 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
44 match self {
45 Error::Io(e) => Some(e),
46 _ => None,
47 }
48 }
49}
50
51impl From<io::Error> for Error {
52 fn from(e: io::Error) -> Self {
53 Error::Io(e)
54 }
55}
56
57pub type Result<T> = std::result::Result<T, Error>;