use core::{convert::Infallible, mem};
use crate::field::metadata::map::record;
mod fixed;
pub mod plain;
pub(in crate::field::metadata) mod scratch;
pub(in crate::field::metadata) mod tagged;
use o3::buffer::{
pool,
write::{self, consume},
};
use write::edit;
use crate::field::{
Indexing, head,
metadata::{
block,
huffman::decoder,
section::{rules, state::Decoder, storage},
},
};
#[derive(Clone, PartialEq, Eq)]
#[doc(hidden)]
#[derive(Debug)]
pub enum Error {
Plain(plain::Error),
Layout([usize; 3]),
Prefix(storage::Tag, usize),
Capacity {
cause: consume::Error,
},
Scratch(storage::Error),
Edit(edit::Error),
Write(consume::Error),
SinkProgress {
cause: consume::Error,
stored: [usize; 2],
remaining: usize,
input: [usize; 3],
},
}
impl From<Infallible> for Error {
fn from(never: Infallible) -> Self {
match never {}
}
}
impl From<plain::Error> for Error {
fn from(error: plain::Error) -> Self {
Self::Plain(error)
}
}
#[doc(hidden)]
pub enum StringError {
Bound(decoder::Error),
Decode {
cause: decoder::DecodeError<Infallible>,
progress: decoder::Progress,
},
Store(Error),
}
const _: () = assert!(mem::size_of::<Result<(), Error>>() == mem::size_of::<Error>());
const _: () = assert!(mem::size_of::<Result<(), StringError>>() == mem::size_of::<Error>());
pub(super) fn push<P: rules::Plan, A: rules::Push>(
state: &mut Decoder<P, impl block::Initial>,
lease: &mut pool::Span,
scratch: &mut storage::Scratch,
decision: rules::Decision<P::Inspect>,
name: record::Span<'_>,
value: record::Span<'_>,
indexing: Indexing,
) -> Result<(), Error> {
let field = record::View::new(name, value).with_index(indexing);
match decision {
rules::Decision::Discard(known) => {
state.retain::<A>(known, field, lease.as_slice(), scratch.authority());
}
rules::Decision::Inspect(token, known) => {
state.inspect::<A>(token, known, field, lease, scratch.authority());
}
rules::Decision::Skip => {}
rules::Decision::Scratch => {
let (first, second) = value.parts();
scratch
.try_replace([first, second])
.map_err(Error::Scratch)?;
scratch.indexing = indexing;
let keep = state.retain::<A>(
head::Name::Authority,
record::View::new(name, record::Span::from_slice(scratch.authority()))
.with_index(indexing),
lease.as_slice(),
scratch.authority(),
);
match keep {
rules::Retention::Retain => state.store(
head::Name::Authority,
rules::Store::Scratch,
0..scratch.authority().len(),
lease,
),
rules::Retention::Discard => scratch.clear(),
}
}
rules::Decision::Tagged(known, tag) => {
let len = value.len();
let prefix = tag
.prefix_with(len, indexing)
.ok_or(Error::Prefix(tag, len))?;
let start = lease.len();
let end = start
.checked_add(storage::Tag::PREFIX_LEN)
.and_then(|offset| offset.checked_add(len))
.ok_or(Error::Layout([start, name.len(), len]))?;
let value_at = start + storage::Tag::PREFIX_LEN;
let (first, second) = value.parts();
let append =
pool::Append::try_prefixed(lease, prefix, [first, second]).map_err(Error::Write)?;
let keep = if known == head::Name::Path && tag == storage::Tag::PATH {
state.path(value, value_at)
} else {
state.retain::<A>(known, field, append.as_slice(), scratch.authority())
};
if keep == rules::Retention::Retain {
append.commit();
state.store(known, rules::Store::Tagged(tag), value_at..end, lease);
}
}
}
Ok(())
}