dmap/error.rs
1//! Error type for `dmap`.
2use pyo3::exceptions::{PyIOError, PyValueError};
3use pyo3::PyErr;
4use thiserror::Error;
5
6/// Enum of the possible error variants that may be encountered.
7#[derive(Error, Debug)]
8pub enum DmapError {
9 /// Represents invalid conditions when reading from input.
10 #[error("{0}")]
11 CorruptStream(&'static str),
12
13 /// Unable to read from a buffer.
14 #[error(transparent)]
15 Io(#[from] std::io::Error),
16
17 /// Error casting between Dmap types.
18 #[error(transparent)]
19 BadCast(#[from] std::num::TryFromIntError),
20
21 /// Invalid key for a DMAP type. Valid keys are defined [here](https://github.com/SuperDARN/rst/blob/main/codebase/general/src.lib/dmap.1.25/include/dmap.h)
22 #[error("{0}")]
23 InvalidKey(i8),
24
25 /// An issue with parsing a record. This is a broad error that is returned by higher-level
26 /// functions (ones that are reading/writing files, as opposed to single-record operations).
27 #[error("{0}")]
28 InvalidRecord(String),
29
30 /// Error interpreting data as a valid DMAP scalar.
31 #[error("{0}")]
32 InvalidScalar(String),
33
34 /// Error interpreting data as a valid DMAP vector.
35 #[error("{0}")]
36 InvalidVector(String),
37
38 /// Bytes cannot be interpreted as a DMAP field.
39 #[error("{0}")]
40 InvalidField(String),
41
42 /// Errors when reading in multiple records
43 #[error("First error: {1}\nRecords with errors: {0:?}")]
44 BadRecords(Vec<usize>, String),
45}
46
47impl From<DmapError> for PyErr {
48 fn from(value: DmapError) -> Self {
49 let msg = value.to_string();
50 match value {
51 DmapError::CorruptStream(..) => PyIOError::new_err(msg),
52 DmapError::Io(..) => PyIOError::new_err(msg),
53 _ => PyValueError::new_err(msg),
54 }
55 }
56}