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
159
160
#[cfg(feature = "backtrace")]
use std::backtrace::Backtrace;

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("{source}")]
    FlydraTypes {
        #[from]
        source: flydra_types::FlydraTypesError,
    },
    #[error("{source}")]
    Mvg {
        #[from]
        #[cfg_attr(feature = "backtrace", backtrace)]
        source: mvg::MvgError,
    },
    #[error("{source}")]
    Io {
        #[from]
        source: std::io::Error,
        #[cfg(feature = "backtrace")]
        backtrace: Backtrace,
    },
    #[error("{source}")]
    Csv {
        #[from]
        source: csv::Error,
        #[cfg(feature = "backtrace")]
        backtrace: Backtrace,
    },
    #[error("{source}")]
    GetTimezone {
        #[from]
        source: iana_time_zone::GetTimezoneError,
        #[cfg(feature = "backtrace")]
        backtrace: Backtrace,
    },
    #[error("{source}")]
    SerdeJson {
        #[from]
        source: serde_json::Error,
        #[cfg(feature = "backtrace")]
        backtrace: Backtrace,
    },
    #[error("{source}")]
    SerdeYaml {
        #[from]
        source: serde_yaml::Error,
        #[cfg(feature = "backtrace")]
        backtrace: Backtrace,
    },
    // #[error("{source}")]
    // HyperError {
    //     #[from]
    //     source: hyper::Error,
    //     #[cfg(feature = "backtrace")]
    //     backtrace: Backtrace,
    // },
    #[error("{source}")]
    TomlSerError {
        #[from]
        source: toml::ser::Error,
        #[cfg(feature = "backtrace")]
        backtrace: Backtrace,
    },
    #[error("{source}")]
    TomlDeError {
        #[from]
        source: toml::de::Error,
        #[cfg(feature = "backtrace")]
        backtrace: Backtrace,
    },
    #[error("{source}")]
    SendToDiskError {
        #[from]
        source: tokio::sync::mpsc::error::SendError<crate::SaveToDiskMsg>,
        #[cfg(feature = "backtrace")]
        backtrace: Backtrace,
    },
    #[error("invalid hypothesis testing parameters")]
    InvalidHypothesisTestingParameters,
    #[error("insufficient data to calculate FPS")]
    InsufficientDataToCalculateFps,
    #[error(transparent)]
    FileError(
        #[from]
        #[cfg_attr(feature = "backtrace", backtrace)]
        FileErrorInner,
    ),
    #[error(transparent)]
    WrappedError(
        #[from]
        #[cfg_attr(feature = "backtrace", backtrace)]
        WrappedErrorInner,
    ),
}

#[derive(Debug)]
pub struct FileErrorInner {
    what: &'static str,
    filename: String,
    source: Box<dyn std::error::Error + Sync + Send>,
}

impl std::fmt::Display for FileErrorInner {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "Error \"{}\" opening {}: {}",
            self.what, self.filename, self.source
        )
    }
}

impl std::error::Error for FileErrorInner {
    #[cfg(feature = "backtrace")]
    fn provide<'a>(&'a self, req: &mut std::error::Request<'a>) {
        self.source.provide(req)
    }
}

#[derive(Debug)]
pub struct WrappedErrorInner {
    source: Box<dyn std::error::Error + Sync + Send>, // Box::new(source),
}

impl std::fmt::Display for WrappedErrorInner {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.source)
    }
}

impl std::error::Error for WrappedErrorInner {
    #[cfg(feature = "backtrace")]
    fn provide<'a>(&'a self, req: &mut std::error::Request<'a>) {
        self.source.provide(req)
    }
}

pub fn file_error<E>(what: &'static str, filename: String, source: E) -> Error
where
    E: 'static + std::error::Error + Sync + Send,
{
    FileErrorInner {
        what,
        filename,
        source: Box::new(source),
    }
    .into()
}

pub fn wrap_error<E>(source: E) -> Error
where
    E: 'static + std::error::Error + Sync + Send,
{
    WrappedErrorInner {
        source: Box::new(source),
    }
    .into()
}