pub mod blast;
mod container;
mod dbf;
use std::fmt;
use std::path::Path;
pub use container::{decode, decode_with_limit, Container, DbfData, DEFAULT_MAX_DECOMPRESSED};
pub use dbf::{
repair_names, ColType, ColumnData, Cursor, Encoding, Field, ReadOptions, Sink, Table,
};
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Blast { code: i32, record_area: bool },
TooLarge { max_bytes: u64 },
NotDbf,
Header(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "{e}"),
Error::Blast { code, record_area: true } => {
write!(f, "Failed to decompress DBC record area with blast(); code={code}")
}
Error::Blast { code, record_area: false } => {
write!(f, "Failed to decompress DBC with blast(); code={code}")
}
Error::TooLarge { max_bytes } => {
write!(f, "Decompressed data exceeds the limit of {max_bytes} bytes.")
}
Error::NotDbf => f.write_str(
"Decompression succeeded, but output does not look like a valid DBF file.",
),
Error::Header(msg) => f.write_str(msg),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
pub fn open(path: impl AsRef<Path>, opts: &ReadOptions) -> Result<Table, Error> {
from_bytes(std::fs::read(path)?, opts)
}
pub fn from_bytes(bytes: Vec<u8>, opts: &ReadOptions) -> Result<Table, Error> {
Table::new(decode(bytes)?, opts)
}