srcdmp 0.1.0-alpha.1

Amazing code snapshot tool and library
Documentation
use std::fmt;

/// Every way srcdmp can fail.
///
/// This is the unified error type returned by all srcdmp library
/// functions. It covers invalid file headers, codec issues, UTF-8
/// validation failures, I/O errors, and truncated data.
///
/// # Examples
///
/// ```rust
/// use srcdmp::raw::error::SdmpError;
///
/// let err = SdmpError::NotAnSdmpFile;
/// assert_eq!(err.to_string(), "not a valid .srcdmp file");
/// ```
#[derive(Debug)]
pub enum SdmpError {
    /// The file does not start with the expected magic bytes (`SDMP`).
    NotAnSdmpFile,

    /// The codec ID byte is not one of the recognized values.
    ///
    /// Stores the unrecognized byte so error messages can display it.
    UnknownCodec(u8),

    /// The input ended before all expected data was consumed.
    UnexpectedEof,

    /// Decoded bytes are not valid UTF-8.
    InvalidUtf8,

    /// An underlying I/O operation failed (read, write, create directory, etc.).
    IoError(std::io::Error),

    /// The manifest section of the archive is corrupt or malformed.
    CorruptManifest,
}

impl fmt::Display for SdmpError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotAnSdmpFile => write!(f, "not a valid .srcdmp file"),
            Self::UnknownCodec(byte) => write!(f, "unknown codec: 0x{byte:02X}"),
            Self::UnexpectedEof => write!(f, "unexpected end of file"),
            Self::InvalidUtf8 => write!(f, "decoded bytes are not valid UTF-8"),
            Self::IoError(e) => write!(f, "io error: {e}"),
            Self::CorruptManifest => write!(f, "manifest is corrupt or malformed"),
        }
    }
}

impl From<std::io::Error> for SdmpError {
    fn from(e: std::io::Error) -> Self {
        Self::IoError(e)
    }
}