sett 0.4.0

Rust port of sett (data compression, encryption and transfer tool).
Documentation
//! Error namespace

/// Error opening a zip data stream
#[derive(Debug)]
pub enum OpenStreamError<E> {
    /// The underlying stream returned an error
    Source(E),
    /// The zip archive is invalid
    Zip(ZipError),
}

impl<E> std::fmt::Display for OpenStreamError<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Error opening zip data stream")
    }
}

impl<E: core::error::Error + 'static> core::error::Error for OpenStreamError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Source(source) => Some(source),
            Self::Zip(source) => Some(source),
        }
    }
}

impl<E> From<E> for OpenStreamError<E> {
    fn from(value: E) -> Self {
        Self::Source(value)
    }
}

/// Error reading a zip data stream
#[derive(Debug)]
pub enum ReadStreamError<E> {
    /// The underlying stream returned an error
    Source(E),
    /// The zip archive header is invalid
    Header(HeaderParseError),
    /// The requested file was not found in the archive
    NotFound(String),
    /// An I/O error occurred
    Io(std::io::Error),
    /// The compression method is not supported
    InvalidCompressionMethod,
}

impl<E> std::fmt::Display for ReadStreamError<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotFound(path) => {
                write!(f, "File reader error: archive member not found: {path}")
            }
            Self::InvalidCompressionMethod => {
                write!(f, "only uncompressed files are supported")
            }
            _ => write!(f, "Error while reading zip data stream"),
        }
    }
}

impl<E: core::error::Error + 'static> core::error::Error for ReadStreamError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Source(source) => Some(source),
            Self::Header(source) => Some(source),
            Self::Io(source) => Some(source),
            _ => None,
        }
    }
}

impl<E> From<HeaderParseError> for ReadStreamError<E> {
    fn from(value: HeaderParseError) -> Self {
        Self::Header(value)
    }
}
impl<E> From<std::io::Error> for ReadStreamError<E> {
    fn from(value: std::io::Error) -> Self {
        Self::Io(value)
    }
}

/// Error parsing a zip archive
#[derive(Debug)]
pub enum ZipError {
    /// The offset of a file is invalid
    InvalidOffset,
    /// Error seeking to the central directory
    SeekCentralDirectory(SeekHeaderError),
    /// Error parsing the central directory
    ParseCentralDirectory(HeaderParseError),
    /// An I/O error occurred
    Io(std::io::Error),
    /// Multi-disk archives are not supported
    UnimplementedMultiDisk,
}

impl std::fmt::Display for ZipError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidOffset => write!(
                f,
                "Error parsing zip file: zip offset is before the buffer start"
            ),
            Self::UnimplementedMultiDisk => {
                write!(f, "multi-disk zip archives are not supported")
            }
            _ => write!(f, "Error parsing zip file: zip"),
        }
    }
}

impl std::error::Error for ZipError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(source) => Some(source),
            Self::SeekCentralDirectory(source) => Some(source),
            Self::ParseCentralDirectory(source) => Some(source),
            _ => None,
        }
    }
}

impl From<std::io::Error> for ZipError {
    fn from(value: std::io::Error) -> Self {
        Self::Io(value)
    }
}
impl From<SeekHeaderError> for ZipError {
    fn from(value: SeekHeaderError) -> Self {
        Self::SeekCentralDirectory(value)
    }
}
impl From<HeaderParseError> for ZipError {
    fn from(value: HeaderParseError) -> Self {
        Self::ParseCentralDirectory(value)
    }
}

/// Error seeking to a zip header
#[derive(Debug)]
pub enum SeekHeaderError {
    /// The header was not found
    NotFound(&'static str),
    /// An I/O error occurred while seeking
    Seek(std::io::Error),
}

impl std::fmt::Display for SeekHeaderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotFound(message) => message.fmt(f),
            _ => write!(f, "Zip/Zip64 central directory header error"),
        }
    }
}

impl std::error::Error for SeekHeaderError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Seek(source) => Some(source),
            _ => None,
        }
    }
}

impl From<std::io::Error> for SeekHeaderError {
    fn from(value: std::io::Error) -> Self {
        Self::Seek(value)
    }
}

/// Error parsing a zip header
#[derive(Debug)]
pub enum HeaderParseError {
    /// The header signature is invalid
    InvalidSignature,
    /// The extra field is invalid
    InvalidExtraField,
    /// The input is invalid
    InvalidInput,
    /// An I/O error occurred
    Io(std::io::Error),
    /// A required zip info field is missing
    MissingZipInfo(&'static str),
}

impl std::fmt::Display for HeaderParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidSignature => write!(f, "invalid zip central directory header"),
            Self::InvalidExtraField => write!(
                f,
                "invalid zip64 central directory header: missing or \
                incomplete 'extra field' section"
            ),
            Self::InvalidInput => write!(f, "only uncompressed files are supported"),
            Self::MissingZipInfo(msg) => msg.fmt(f),
            _ => write!(f, "Zip/Zip64 central directory header parse error"),
        }
    }
}

impl std::error::Error for HeaderParseError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(source) => Some(source),
            _ => None,
        }
    }
}

impl From<std::io::Error> for HeaderParseError {
    fn from(value: std::io::Error) -> Self {
        Self::Io(value)
    }
}