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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use hydrate_data::DataSetError;
use hydrate_schema::DataSetErrorWithBacktrace;
use std::sync::Arc;

#[derive(Debug, Clone)]
pub enum PipelineError {
    StringError(String),
    DataSetError(DataSetError),
    DataSetErrorWithBacktrace(DataSetErrorWithBacktrace),
    IoError(Arc<std::io::Error>),
    BincodeError(Arc<bincode::Error>),
    JsonError(Arc<serde_json::Error>),
    UuidError(uuid::Error),
    ThumbnailUnavailable,
}

impl std::error::Error for PipelineError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match *self {
            PipelineError::StringError(_) => None,
            PipelineError::DataSetError(_) => None,
            PipelineError::DataSetErrorWithBacktrace(_) => None,
            PipelineError::IoError(ref e) => Some(&**e),
            PipelineError::BincodeError(ref e) => Some(&**e),
            PipelineError::JsonError(ref e) => Some(&**e),
            PipelineError::UuidError(ref e) => Some(e),
            PipelineError::ThumbnailUnavailable => None,
        }
    }
}

impl core::fmt::Display for PipelineError {
    fn fmt(
        &self,
        fmt: &mut core::fmt::Formatter,
    ) -> core::fmt::Result {
        match *self {
            PipelineError::StringError(ref e) => e.fmt(fmt),
            PipelineError::DataSetError(ref e) => {
                use std::fmt::Debug;
                e.fmt(fmt)
            }
            PipelineError::DataSetErrorWithBacktrace(ref e) => {
                use std::fmt::Debug;
                e.fmt(fmt)
            }
            PipelineError::IoError(ref e) => e.fmt(fmt),
            PipelineError::BincodeError(ref e) => e.fmt(fmt),
            PipelineError::JsonError(ref e) => e.fmt(fmt),
            PipelineError::UuidError(ref e) => e.fmt(fmt),
            PipelineError::ThumbnailUnavailable => "ThumbnailUnavailable".fmt(fmt),
        }
    }
}

impl From<&str> for PipelineError {
    fn from(str: &str) -> Self {
        PipelineError::StringError(str.to_string())
    }
}

impl From<String> for PipelineError {
    fn from(string: String) -> Self {
        PipelineError::StringError(string)
    }
}

impl From<DataSetError> for PipelineError {
    fn from(error: DataSetError) -> Self {
        PipelineError::DataSetError(error)
    }
}

impl From<DataSetErrorWithBacktrace> for PipelineError {
    fn from(error: DataSetErrorWithBacktrace) -> Self {
        PipelineError::DataSetErrorWithBacktrace(error)
    }
}

impl From<std::io::Error> for PipelineError {
    fn from(error: std::io::Error) -> Self {
        PipelineError::IoError(Arc::new(error))
    }
}

impl From<bincode::Error> for PipelineError {
    fn from(error: bincode::Error) -> Self {
        PipelineError::BincodeError(Arc::new(error))
    }
}

impl From<serde_json::Error> for PipelineError {
    fn from(error: serde_json::Error) -> Self {
        PipelineError::JsonError(Arc::new(error))
    }
}

impl From<uuid::Error> for PipelineError {
    fn from(error: uuid::Error) -> Self {
        PipelineError::UuidError(error)
    }
}

#[derive(Clone)]
pub struct PipelineErrorWithBacktrace {
    pub error: PipelineError,
    #[cfg(all(backtrace, debug_assertions))]
    backtrace: Arc<backtrace::Backtrace>,
}

impl std::error::Error for PipelineErrorWithBacktrace {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.error.source()
    }
}

impl core::fmt::Display for PipelineErrorWithBacktrace {
    fn fmt(
        &self,
        fmt: &mut core::fmt::Formatter,
    ) -> core::fmt::Result {
        self.error.fmt(fmt)
    }
}

impl<T: Into<PipelineError>> From<T> for PipelineErrorWithBacktrace {
    fn from(error: T) -> Self {
        PipelineErrorWithBacktrace {
            error: error.into(),
            #[cfg(all(backtrace, debug_assertions))]
            backtrace: Arc::new(backtrace::Backtrace::new()),
        }
    }
}

impl std::fmt::Debug for PipelineErrorWithBacktrace {
    #[cfg(not(all(backtrace, debug_assertions)))]
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        write!(f, "{:?}", self.error)
    }

    #[cfg(all(backtrace, debug_assertions))]
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        let backtrace = match &self.error {
            PipelineError::DataSetErrorWithBacktrace(e) => &e.backtrace,
            _ => &*self.backtrace,
        };
        write!(f, "{:?}:\n{:?}", self.error, backtrace)
    }
}

pub type PipelineResult<T> = Result<T, PipelineErrorWithBacktrace>;