casc_rs/
error.rs

1/// Represents all possible errors that can occur in the CASC library.
2///
3/// This enum is used throughout the crate to provide detailed error information for
4/// operations that may fail, such as file access, data validation, and I/O operations.
5#[derive(Debug)]
6pub enum CascError {
7    /// Represents an error that occurs when a file is not found in the CASC storage.
8    FileNotFound(String),
9    /// Represents an error that occurs when a file is corrupted or invalid.
10    FileCorrupted(String),
11    /// Represents an error that occurs when the data in a file is invalid.
12    InvalidData(String),
13    /// Represents an error that occurs when a file is not supported by the CASC storage.
14    UnsupportedFileType(String),
15    /// Represents an error that occurs during I/O operations.
16    Io(std::io::Error),
17    /// Represents an error that occurs for any other reason not covered by the above variants.
18    Other(String),
19}
20
21/// Provides a user-friendly string representation for each error variant in `CascError`.
22impl std::fmt::Display for CascError {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            CascError::InvalidData(err) => write!(f, "Invalid data: {err}"),
26            CascError::FileNotFound(name) => write!(f, "File not found: {name}"),
27            CascError::FileCorrupted(name) => write!(f, "File is corrupted: {name}"),
28            CascError::UnsupportedFileType(name) => write!(f, "Unsupported file type: {name}"),
29            CascError::Io(err) => write!(f, "I/O error: {err}"),
30            CascError::Other(err) => write!(f, "CASC error: {err}"),
31        }
32    }
33}
34
35/// Implements the standard error trait for `CascError`, allowing it to be used with
36/// error chaining and other error handling utilities.
37impl std::error::Error for CascError {
38    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
39        match self {
40            CascError::Io(err) => Some(err),
41            _ => None,
42        }
43    }
44}
45
46/// Allows automatic conversion from `std::io::Error` to `CascError`.
47impl From<std::io::Error> for CascError {
48    fn from(error: std::io::Error) -> Self {
49        CascError::Io(error)
50    }
51}