Skip to main content

flydra2/
error.rs

1// Copyright (C) The Strand-Braid Authors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4#[derive(thiserror::Error, Debug)]
5pub enum Error {
6    #[error("{source}")]
7    FlydraTypes {
8        #[from]
9        source: braid_types::FlydraTypesError,
10    },
11    #[error("{source}")]
12    Mvg {
13        #[from]
14        source: braid_mvg::MvgError,
15    },
16    #[error("{0}")]
17    FlydraMvg(#[from] flydra_mvg::FlydraMvgError),
18    #[error("{source}")]
19    Io {
20        #[from]
21        source: std::io::Error,
22    },
23    #[error("{source}")]
24    Csv {
25        #[from]
26        source: csv::Error,
27    },
28    #[error("{source}")]
29    GetTimezone {
30        #[from]
31        source: iana_time_zone::GetTimezoneError,
32    },
33    #[error("{source}")]
34    SerdeJson {
35        #[from]
36        source: serde_json::Error,
37    },
38    #[error("{source}")]
39    SerdeYaml {
40        #[from]
41        source: serde_yaml::Error,
42    },
43    #[error("{source}")]
44    TomlSerError {
45        #[from]
46        source: toml::ser::Error,
47    },
48    #[error("{source}")]
49    TomlDeError {
50        #[from]
51        source: toml::de::Error,
52    },
53    #[error("invalid hypothesis testing parameters")]
54    InvalidHypothesisTestingParameters,
55    #[error("insufficient data to calculate FPS")]
56    InsufficientDataToCalculateFps,
57    #[error(transparent)]
58    FileError(#[from] FileErrorInner),
59    #[error(transparent)]
60    WrappedError(#[from] WrappedErrorInner),
61}
62
63#[derive(Debug)]
64pub struct FileErrorInner {
65    what: &'static str,
66    filename: String,
67    source: Box<dyn std::error::Error + Sync + Send>,
68}
69
70impl std::fmt::Display for FileErrorInner {
71    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
72        write!(
73            f,
74            "Error \"{}\" opening {}: {}",
75            self.what, self.filename, self.source
76        )
77    }
78}
79
80impl std::error::Error for FileErrorInner {}
81
82#[derive(Debug)]
83pub struct WrappedErrorInner {
84    source: Box<dyn std::error::Error + Sync + Send>, // Box::new(source),
85}
86
87impl std::fmt::Display for WrappedErrorInner {
88    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
89        write!(f, "{}", self.source)
90    }
91}
92
93impl std::error::Error for WrappedErrorInner {}
94
95pub fn file_error<E>(what: &'static str, filename: String, source: E) -> Error
96where
97    E: 'static + std::error::Error + Sync + Send,
98{
99    FileErrorInner {
100        what,
101        filename,
102        source: Box::new(source),
103    }
104    .into()
105}
106
107pub fn wrap_error<E>(source: E) -> Error
108where
109    E: 'static + std::error::Error + Sync + Send,
110{
111    WrappedErrorInner {
112        source: Box::new(source),
113    }
114    .into()
115}