#[derive(Debug)]
pub enum OpenStreamError<E> {
Source(E),
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)
}
}
#[derive(Debug)]
pub enum ReadStreamError<E> {
Source(E),
Header(HeaderParseError),
NotFound(String),
Io(std::io::Error),
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)
}
}
#[derive(Debug)]
pub enum ZipError {
InvalidOffset,
SeekCentralDirectory(SeekHeaderError),
ParseCentralDirectory(HeaderParseError),
Io(std::io::Error),
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)
}
}
#[derive(Debug)]
pub enum SeekHeaderError {
NotFound(&'static str),
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)
}
}
#[derive(Debug)]
pub enum HeaderParseError {
InvalidSignature,
InvalidExtraField,
InvalidInput,
Io(std::io::Error),
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)
}
}