use object::build;
use std::{error, fmt, io};
#[derive(Debug)]
pub struct Error {
inner: ErrorInner,
}
#[derive(Debug)]
enum ErrorInner {
Io(io::Error),
Parse(build::Error),
Write(build::Error),
Modify(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ErrorKind {
Parse,
Write,
Io(io::ErrorKind),
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 {
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()),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;