use std::fmt;
use std::io;
use thiserror::Error;
pub type Version = (u8, u8, u8);
#[derive(Error, Debug)]
pub enum ArchiveError {
#[error("IO error reading data")]
IOError(#[from] io::Error),
#[error("format error: {0}")]
InvalidData(String),
#[error("format error for id {0}: {1}")]
InvalidEntryData(crate::toc::ID, String),
#[error("TOC entry has no data")]
NoDataPresent,
#[error("reading BLOB data is not supported")]
BlobNotSupported,
#[error("archive format {}.{}.{} is not supported", (.0).0, (.0).1, (.0).2)]
UnsupportedVersionError(Version),
#[error("compression method {0} is not supported")]
CompressionMethodNotSupported(CompressionMethod),
}
pub type Oid = u64;
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Offset {
Unknown,
PosNotSet,
PosSet(u64),
NoData,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum BlockType {
Data = 1,
Blob = 3,
}
impl TryFrom<u8> for BlockType {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
x if x == BlockType::Data as u8 => Ok(BlockType::Data),
x if x == BlockType::Blob as u8 => Ok(BlockType::Blob),
_ => Err(()),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CompressionMethod {
None,
Gzip(i64),
LZ4,
ZSTD,
}
impl TryFrom<u8> for CompressionMethod {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(CompressionMethod::None),
1 => Ok(CompressionMethod::Gzip(0)),
2 => Ok(CompressionMethod::LZ4),
3 => Ok(CompressionMethod::ZSTD),
_ => Err(()),
}
}
}
impl fmt::Display for CompressionMethod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum Section {
None = 1,
PreData,
Data,
PostData,
}
impl TryFrom<i64> for Section {
type Error = ();
fn try_from(value: i64) -> Result<Self, Self::Error> {
match value {
x if x == Section::None as i64 => Ok(Section::None),
x if x == Section::PreData as i64 => Ok(Section::PreData),
x if x == Section::Data as i64 => Ok(Section::Data),
x if x == Section::PostData as i64 => Ok(Section::PostData),
_ => Err(()),
}
}
}
impl fmt::Display for Section {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}