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 object::build;
use std::{error, fmt, io};

/// An error that occurred while rewriting a file.
#[derive(Debug)]
pub struct Error {
    inner: ErrorInner,
}

#[derive(Debug)]
enum ErrorInner {
    Io(io::Error),
    Parse(build::Error),
    Write(build::Error),
    Modify(String),
}

/// The kind of error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ErrorKind {
    /// A parse error occurred while reading the file.
    Parse,
    /// A validation error occurred while writing the file.
    Write,
    /// An I/O error occurred while writing the file.
    Io(io::ErrorKind),
    /// A validation error occurred while modifying the file.
    Modify,
}

impl fmt::Display for Error {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.inner {
            ErrorInner::Io(e) => e.fmt(f),
            ErrorInner::Parse(e) => e.fmt(f),
            ErrorInner::Write(e) => e.fmt(f),
            ErrorInner::Modify(e) => e.fmt(f),
        }
    }
}

impl error::Error for Error {}

impl Error {
    /// Get the kind of error.
    pub fn kind(&self) -> ErrorKind {
        match &self.inner {
            ErrorInner::Io(e) => ErrorKind::Io(e.kind()),
            ErrorInner::Parse(_) => ErrorKind::Parse,
            ErrorInner::Write(_) => ErrorKind::Write,
            ErrorInner::Modify(_) => ErrorKind::Modify,
        }
    }

    pub(crate) fn io(error: io::Error) -> Self {
        Self {
            inner: ErrorInner::Io(error),
        }
    }

    pub(crate) fn parse(error: build::Error) -> Self {
        Self {
            inner: ErrorInner::Parse(error),
        }
    }

    pub(crate) fn write(error: build::Error) -> Self {
        Self {
            inner: ErrorInner::Write(error),
        }
    }

    pub(crate) fn modify(message: impl Into<String>) -> Self {
        Self {
            inner: ErrorInner::Modify(message.into()),
        }
    }
}

/// The  `Result` type for this library.
pub type Result<T> = std::result::Result<T, Error>;