bufkit_data/
errors.rs

1//! Module for errors.
2use std::{error::Error, fmt::Display};
3
4/// Error from the archive interface.
5#[derive(Debug)]
6pub enum BufkitDataErr {
7    // Inherited errors from sounding stack
8    /// Error forwarded from sounding-analysis
9    SoundingAnalysis(sounding_analysis::AnalysisError),
10    /// Error forwarded from sounding-bufkit
11    SoundingBufkit(sounding_bufkit::BufkitFileError),
12
13    // Inherited errors from std
14    /// Error forwarded from std
15    IO(std::io::Error),
16
17    // Other forwarded errors
18    /// Database error
19    Database(rusqlite::Error),
20    /// Error forwarded from the strum crate
21    StrumError(strum::ParseError),
22    /// General error with any cause information erased and replaced by a string
23    GeneralError(String),
24
25    // My own errors from this crate
26    /// File not found in the index.
27    NotInIndex,
28    /// Not enough data to complete the task.
29    NotEnoughData,
30    /// Sounding was missing a valid time
31    MissingValidTime,
32    /// Missing station information.
33    MissingStationData,
34    /// An error that is known and hard coded into the library.
35    KnownArchiveError(&'static str),
36    /// There was an internal logic error.
37    LogicError(&'static str),
38    /// The site id didn't match the hint when adding.
39    MismatchedIDs {
40        /// The ID that was provided as a hint.
41        hint: String,
42        /// The ID that was parsed from the file.
43        parsed: String,
44    },
45    /// The station numbers didn't match.
46    MismatchedStationNumbers {
47        /// The StationNumber number with the original request.
48        hint: crate::StationNumber,
49        /// The StationNumber parsed from the file.
50        parsed: crate::StationNumber,
51    },
52    /// Parsed and expected initialization times didn't match.
53    MismatchedInitializationTimes {
54        /// The initialization time that was expected.
55        hint: chrono::NaiveDateTime,
56        /// The inizialization time that was parsed from the file.
57        parsed: chrono::NaiveDateTime,
58    },
59}
60
61impl Display for BufkitDataErr {
62    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
63        use crate::errors::BufkitDataErr::*;
64
65        match self {
66            SoundingAnalysis(err) => write!(f, "error from sounding-analysis: {}", err),
67            SoundingBufkit(err) => write!(f, "error from sounding-bufkit: {}", err),
68
69            IO(err) => write!(f, "std lib io error: {}", err),
70
71            Database(err) => write!(f, "database error: {}", err),
72            StrumError(err) => write!(f, "error forwarded from strum crate: {}", err),
73            GeneralError(msg) => write!(f, "general error forwarded: {}", msg),
74
75            NotInIndex => write!(f, "no match in the index"),
76            NotEnoughData => write!(f, "not enough data to complete task"),
77            MissingValidTime => write!(f, "sounding missing a valid time"),
78            MissingStationData => write!(f, "not enough information about the station"),
79            KnownArchiveError(msg) => write!(f, "Known error: {}", msg),
80            LogicError(msg) => write!(f, "internal logic error: {}", msg),
81            MismatchedIDs { hint, parsed } => {
82                write!(f, "mismatched ids parsed: {} != hint:{}", parsed, hint)
83            }
84            MismatchedStationNumbers { .. } => write!(f, "mismatched station numbers"),
85            MismatchedInitializationTimes { .. } => write!(f, "mismatched initialization times"),
86        }
87    }
88}
89
90impl Error for BufkitDataErr {}
91
92impl From<sounding_bufkit::BufkitFileError> for BufkitDataErr {
93    fn from(err: sounding_bufkit::BufkitFileError) -> BufkitDataErr {
94        BufkitDataErr::SoundingBufkit(err)
95    }
96}
97
98impl From<sounding_analysis::AnalysisError> for BufkitDataErr {
99    fn from(err: sounding_analysis::AnalysisError) -> BufkitDataErr {
100        BufkitDataErr::SoundingAnalysis(err)
101    }
102}
103
104impl From<std::io::Error> for BufkitDataErr {
105    fn from(err: std::io::Error) -> BufkitDataErr {
106        BufkitDataErr::IO(err)
107    }
108}
109
110impl From<rusqlite::Error> for BufkitDataErr {
111    fn from(err: rusqlite::Error) -> BufkitDataErr {
112        BufkitDataErr::Database(err)
113    }
114}
115
116impl From<strum::ParseError> for BufkitDataErr {
117    fn from(err: strum::ParseError) -> BufkitDataErr {
118        BufkitDataErr::StrumError(err)
119    }
120}
121
122impl From<Box<dyn Error>> for BufkitDataErr {
123    fn from(err: Box<dyn Error>) -> BufkitDataErr {
124        BufkitDataErr::GeneralError(err.to_string())
125    }
126}