pub mod dbn;
#[cfg_attr(
not(test),
deprecated(
since = "0.3.0",
note = "DBZ was renamed to DBN and the format was changed to no longer rely on Zstd."
)
)]
pub mod dbz;
mod dyn_decoder;
mod dyn_reader;
mod merge;
mod stream;
#[doc(hidden)]
pub mod zstd;
pub use self::dbn::{
Decoder as DbnDecoder, MetadataDecoder as DbnMetadataDecoder, RecordDecoder as DbnRecordDecoder,
};
#[doc(inline)]
pub use dyn_decoder::DynDecoder;
#[doc(inline)]
pub use dyn_reader::*;
#[doc(inline)]
pub use merge::{Decoder as MergeDecoder, RecordDecoder as MergeRecordDecoder};
#[doc(inline)]
pub use stream::StreamIterDecoder;
use std::{io::Seek, mem};
use crate::{HasRType, Metadata, RecordRef, VersionUpgradePolicy};
pub trait DecodeRecordRef {
fn decode_record_ref(&mut self) -> crate::Result<Option<RecordRef<'_>>>;
}
pub trait DbnMetadata {
fn metadata(&self) -> &Metadata;
fn metadata_mut(&mut self) -> &mut Metadata;
}
pub trait DecodeRecord {
fn decode_record<T: HasRType>(&mut self) -> crate::Result<Option<&T>>;
fn decode_records<T: HasRType + Clone>(mut self) -> crate::Result<Vec<T>>
where
Self: Sized,
{
let mut res = Vec::new();
while let Some(rec) = self.decode_record::<T>()? {
res.push(rec.clone());
}
Ok(res)
}
}
pub trait DecodeDbn: DecodeRecord + DecodeRecordRef + DbnMetadata {}
pub trait DecodeStream: DecodeRecord + private::LastRecord {
fn decode_stream<T: HasRType>(self) -> StreamIterDecoder<Self, T>
where
Self: Sized;
}
pub trait SkipBytes {
fn skip_bytes(&mut self, n_bytes: usize) -> crate::Result<()>;
}
impl<T> SkipBytes for T
where
T: Seek,
{
fn skip_bytes(&mut self, n_bytes: usize) -> crate::Result<()> {
self.seek(std::io::SeekFrom::Current(n_bytes as i64))
.map(drop)
.map_err(|err| crate::Error::io(err, format!("seeking ahead {n_bytes} bytes")))
}
}
#[cfg(feature = "async")]
#[allow(async_fn_in_trait)] pub trait AsyncDecodeRecordRef {
async fn decode_record_ref(&mut self) -> crate::Result<Option<RecordRef<'_>>>;
}
#[cfg(feature = "async")]
#[allow(async_fn_in_trait)] pub trait AsyncDecodeRecord {
async fn decode_record<'a, T: HasRType + 'a>(&'a mut self) -> crate::Result<Option<&'a T>>;
async fn decode_records<T: HasRType + Clone>(&mut self) -> crate::Result<Vec<T>>
where
Self: Sized,
{
let mut res = Vec::new();
while let Some(rec) = self.decode_record::<T>().await? {
res.push(rec.clone());
}
Ok(res)
}
}
#[cfg(feature = "async")]
#[allow(async_fn_in_trait)] pub trait AsyncSkipBytes {
async fn skip_bytes(&mut self, n_bytes: usize) -> crate::Result<()>;
}
#[cfg(feature = "async")]
const ZSTD_FILE_BUFFER_CAPACITY: usize = 1 << 20;
#[doc(hidden)]
pub mod private {
use crate::RecordRef;
#[doc(hidden)]
pub trait LastRecord {
fn last_record(&self) -> Option<RecordRef<'_>>;
}
}
pub(crate) trait FromLittleEndianSlice {
fn from_le_slice(slice: &[u8]) -> Self;
}
impl FromLittleEndianSlice for u64 {
fn from_le_slice(slice: &[u8]) -> Self {
let (bytes, _) = slice.split_at(mem::size_of::<Self>());
Self::from_le_bytes(bytes.try_into().unwrap())
}
}
impl FromLittleEndianSlice for i32 {
fn from_le_slice(slice: &[u8]) -> Self {
let (bytes, _) = slice.split_at(mem::size_of::<Self>());
Self::from_le_bytes(bytes.try_into().unwrap())
}
}
impl FromLittleEndianSlice for u32 {
fn from_le_slice(slice: &[u8]) -> Self {
let (bytes, _) = slice.split_at(mem::size_of::<Self>());
Self::from_le_bytes(bytes.try_into().unwrap())
}
}
impl FromLittleEndianSlice for u16 {
fn from_le_slice(slice: &[u8]) -> Self {
let (bytes, _) = slice.split_at(mem::size_of::<Self>());
Self::from_le_bytes(bytes.try_into().unwrap())
}
}
#[cfg(feature = "async")]
pub use self::dbn::{
AsyncDecoder as AsyncDbnDecoder, AsyncMetadataDecoder as AsyncDbnMetadataDecoder,
AsyncRecordDecoder as AsyncDbnRecordDecoder,
};
#[cfg(test)]
mod tests {
pub const TEST_DATA_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../tests/data");
}