1use std::fmt;
2
3pub type Result<T, E = Error> = std::result::Result<T, E>;
5
6pub struct Error(Box<ErrorInner>);
8
9#[derive(Debug)]
10#[cfg_attr(not(feature = "default"), allow(dead_code))]
11pub(crate) enum ErrorInner {
12 Limit(&'static str),
13 SerializeMetadata(dwarfs::metadata::Error),
14 DuplicatedEntry,
15 Compress(std::io::Error),
16
17 Io(std::io::Error),
18}
19
20impl fmt::Debug for Error {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 self.0.fmt(f)
23 }
24}
25
26impl fmt::Display for Error {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 match &*self.0 {
29 ErrorInner::DuplicatedEntry => f.pad("duplicated entry names in a directory"),
30 ErrorInner::Limit(msg) => write!(f, "{msg}"),
31 ErrorInner::SerializeMetadata(err) => err.fmt(f),
32 ErrorInner::Compress(err) | ErrorInner::Io(err) => err.fmt(f),
33 }
34 }
35}
36
37impl std::error::Error for Error {
38 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
39 match &*self.0 {
40 ErrorInner::Compress(err) | ErrorInner::Io(err) => Some(err),
41 ErrorInner::SerializeMetadata(err) => Some(err),
42 _ => None,
43 }
44 }
45}
46
47impl From<ErrorInner> for Error {
48 #[cold]
49 fn from(err: ErrorInner) -> Self {
50 Self(Box::new(err))
51 }
52}
53
54impl From<std::io::Error> for Error {
55 #[cold]
56 fn from(err: std::io::Error) -> Self {
57 Self(Box::new(ErrorInner::Io(err)))
58 }
59}
60
61impl From<dwarfs::metadata::Error> for Error {
62 #[cold]
63 fn from(err: dwarfs::metadata::Error) -> Self {
64 Self(Box::new(ErrorInner::SerializeMetadata(err)))
65 }
66}