use core::mem;
use o3::buffer::{
pool,
write::{self, consume},
};
use crate::field::{
self, head,
metadata::{
block,
huffman::decoder,
lower,
map::record,
section::{rules, state, storage},
strings,
},
};
pub struct Scratch {
bytes: Vec<u8>,
scan: block::Value,
indexing: field::Indexing,
}
impl Scratch {
pub(in crate::field::metadata) fn begin(
scratch: &mut storage::Scratch,
indexing: field::Indexing,
) -> Self {
let mut bytes = mem::take(&mut scratch.authority);
bytes.clear();
Self {
bytes,
scan: block::Value::new(),
indexing,
}
}
pub(in crate::field::metadata) fn finish<P: rules::Plan, A: rules::Push, C: block::Initial>(
self,
lease: &mut pool::Span,
scratch: &mut storage::Scratch,
state: &mut state::Decoder<P, C>,
) {
let Self {
mut bytes,
scan,
indexing,
} = self;
rules::Validate::value(state, scan.finish());
let value = record::Span::from_slice(&bytes);
match state.retain::<A>(
head::Name::Authority,
record::View::new(record::Span::from_slice(&[]), value).with_index(indexing),
lease.as_slice(),
&bytes,
) {
rules::Retention::Discard => bytes.clear(),
rules::Retention::Retain => state.store(
head::Name::Authority,
rules::Store::Scratch,
0..bytes.len(),
lease,
),
}
mem::swap(&mut bytes, &mut scratch.authority);
scratch.indexing = indexing;
}
pub(in crate::field::metadata) fn rollback(self, scratch: &mut storage::Scratch) {
let Self { mut bytes, .. } = self;
bytes.clear();
mem::swap(&mut bytes, &mut scratch.authority);
}
pub(in crate::field::metadata) fn try_string(
&mut self,
cursor: &mut strings::Cursor,
input: &[u8],
) -> Result<(), lower::StringError> {
let bound = cursor
.output_bound(input.len())
.map_err(lower::StringError::Bound)?;
storage::try_reserve(&mut self.bytes, bound)
.map_err(|cause| lower::StringError::Store(lower::Error::Scratch(cause)))?;
let current = self.bytes.len();
let rest = cursor.remaining();
let sink = Writer {
bytes: write::Reserved::from(&mut self.bytes).into_staged(),
scan: &mut self.scan,
};
match cursor.feed_into(input, sink) {
Ok((_, sink)) => {
sink.bytes.commit();
Ok(())
}
Err(failure) => {
let decoder::Failure {
sink,
cause,
progress,
} = failure;
let stored = [current, current + sink.bytes.len()];
sink.bytes.commit();
match cause {
decoder::DecodeError::InvalidEncoding(cause) => {
Err(lower::StringError::Decode {
cause: decoder::DecodeError::InvalidEncoding(cause),
progress,
})
}
decoder::DecodeError::Sink(cause) => {
Err(lower::StringError::Store(lower::Error::SinkProgress {
cause,
stored,
remaining: rest,
input: [input.len(), progress.consumed(), progress.decoded()],
}))
}
}
}
}
}
}
struct Writer<'a> {
bytes: write::Staged<&'a mut Vec<u8>, usize>,
scan: &'a mut block::Value,
}
impl write::Sink for Writer<'_> {
type Error = consume::Error;
fn write_slice(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
self.bytes.write_slice(bytes)?;
self.scan.extend(bytes);
Ok(())
}
fn write_slices<const N: usize>(&mut self, slices: [&[u8]; N]) -> Result<(), Self::Error> {
self.bytes.write_slices(slices)?;
for bytes in slices {
self.scan.extend(bytes);
}
Ok(())
}
}