bdat 0.6.0

(De)serialization library for Monolithsoft's BDAT file format
Documentation
use crate::table::convert::FormatConvertError;
use crate::{BdatVersion, DetectError, ValueType};
use std::num::TryFromIntError;
use std::str::Utf8Error;
use thiserror::Error;

/// Alias for `Result<T, BdatError>`
pub type Result<T> = std::result::Result<T, BdatError>;

/// Errors that may occur while reading and writing BDAT tables
#[derive(Error, Debug)]
pub enum BdatError {
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    Read(#[from] Box<PosError>),
    #[error(transparent)]
    InvalidLength(#[from] TryFromIntError),
    #[error("Unsupported type: BDAT version {1:?} does not support value type {0:?}")]
    UnsupportedType(ValueType, BdatVersion),
    #[error("Invalid flag type: value type {0:?} does not support flags")]
    InvalidFlagType(ValueType),
    #[error("Could not detect version: {0}")]
    VersionDetect(#[from] DetectError),
    #[error("Could not convert table: {0}")]
    FormatConvert(#[from] FormatConvertError),
    #[error("Unsupported cast type for {0:?}")]
    ValueCast(ValueType),
}

#[derive(Error, Debug)]
pub enum ReadError {
    #[error("invalid magic bytes {0:02X?}")]
    InvalidMagic([u8; 4]),
    #[error("unsupported version {0}")]
    UnsupportedVersion(u32),
    #[error("unknown cell type: {0}")]
    UnknownCellType(u8),
    #[error("unknown value type: {0}")]
    UnknownValueType(u8),
    #[error("name table contains duplicate ID <{0:08X}>")]
    NameTableDuplicate(u32),
    #[error("name table contains ID pair (<{0:08X}>, <{1:08X}>) in incorrect order")]
    NameTableOrder(u32, u32),
    #[error("unexpected unknown value {0}")]
    UnexpectedUnknown(u32),
    #[error(transparent)]
    Utf8(#[from] Utf8Error),
}

#[derive(Debug, Error)]
#[error("BDAT read error at {pos}: {error}")]
pub struct PosError {
    pub pos: u64,
    pub error: ReadError,
}

impl BdatError {
    pub(crate) fn new_read(pos: u64, error: ReadError) -> Self {
        Self::Read(Box::new(PosError { pos, error }))
    }
}