use crate::{
error::Result,
segment::Role,
structure::{SegmentId, Structure},
};
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct Chunk<'a> {
pub segment: SegmentId,
pub role: Role,
pub offset: u64,
pub data: &'a [u8],
}
impl<'a> Chunk<'a> {
pub const fn new(segment: SegmentId, role: Role, offset: u64, data: &'a [u8]) -> Self {
Self {
segment,
role,
offset,
data,
}
}
}
pub trait Sink {
fn begin(&mut self, _structure: &Structure) -> Result<()> {
Ok(())
}
fn wants(&self, _segment: &crate::segment::Segment, _role: Role) -> bool {
true
}
fn chunk(&mut self, chunk: Chunk<'_>) -> Result<()>;
}
#[derive(Debug, Default, Clone, Copy)]
pub struct NullSink;
impl Sink for NullSink {
fn wants(&self, _: &crate::segment::Segment, _: Role) -> bool {
false
}
fn chunk(&mut self, _: Chunk<'_>) -> Result<()> {
Ok(())
}
}
impl<F> Sink for F
where
F: for<'a> FnMut(Chunk<'a>) -> Result<()>,
{
fn chunk(&mut self, chunk: Chunk<'_>) -> Result<()> {
self(chunk)
}
}