Skip to main content

lib_bcsv_jmap/
error.rs

1use thiserror::Error;
2
3/// Result type alias for JMap operations
4pub type Result<T> = std::result::Result<T, JMapError>;
5
6/// Errors that can occur during JMap operations
7#[derive(Error, Debug)]
8pub enum JMapError {
9    /// Invalid field type ID encountered during parsing
10    #[error("Invalid field type ID: 0x{0:02X}")]
11    InvalidFieldType(u8),
12
13    /// Field not found in the container
14    #[error("Field not found: {0}")]
15    FieldNotFound(String),
16
17    /// Field already exists in the container
18    #[error("Field already exists: {0}")]
19    FieldAlreadyExists(String),
20
21    /// Type mismatch when setting a field value
22    #[error("Type mismatch: expected {expected}, got {got}")]
23    TypeMismatch {
24        expected: &'static str,
25        got: &'static str,
26    },
27
28    /// Entry index out of bounds
29    #[error("Entry index out of bounds: {index} (len: {len})")]
30    EntryIndexOutOfBounds { index: usize, len: usize },
31
32    /// Buffer too small to contain valid BCSV data
33    #[error("Buffer too small: expected at least {expected} bytes, got {got}")]
34    BufferTooSmall { expected: usize, got: usize },
35
36    /// Invalid BCSV header
37    #[error("Invalid BCSV header")]
38    InvalidHeader,
39
40    /// String encoding error
41    #[error("String encoding error: {0}")]
42    EncodingError(String),
43
44    /// CSV parsing error
45    #[error("CSV error: {0}")]
46    CsvError(String),
47
48    /// I/O error
49    #[error("I/O error: {0}")]
50    IoError(#[from] std::io::Error),
51
52    /// Lookup file not found
53    #[error("Lookup file not found: {0}")]
54    LookupFileNotFound(String),
55
56    /// Invalid CSV field descriptor format
57    #[error("Invalid CSV field descriptor: {0}")]
58    InvalidCsvFieldDescriptor(String),
59}
60
61impl From<csv::Error> for JMapError {
62    fn from(err: csv::Error) -> Self {
63        JMapError::CsvError(err.to_string())
64    }
65}