summaryrefslogtreecommitdiffstats
path: root/third_party/rust/embed-manifest/src/embed/error.rs
blob: 365d0525edf190a5c3e6797cb3766b4fd5791405 (plain)
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
//! Error handling for application manifest embedding.

use std::fmt::{self, Display, Formatter};
use std::io::{self, ErrorKind};

/// The error type which is returned when application manifest embedding fails.
#[derive(Debug)]
pub struct Error {
    repr: Repr,
}

#[derive(Debug)]
enum Repr {
    IoError(io::Error),
    UnknownTarget,
}

impl Error {
    pub(crate) fn unknown_target() -> Error {
        Error {
            repr: Repr::UnknownTarget,
        }
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self.repr {
            Repr::IoError(ref e) => write!(f, "I/O error: {}", e),
            Repr::UnknownTarget => f.write_str("unknown target"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self.repr {
            Repr::IoError(ref e) => Some(e),
            _ => None,
        }
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error { repr: Repr::IoError(e) }
    }
}

impl From<Error> for io::Error {
    fn from(e: Error) -> Self {
        match e.repr {
            Repr::IoError(ioe) => ioe,
            _ => io::Error::new(ErrorKind::Other, e),
        }
    }
}