use core::num::NonZeroUsize;
use super::EncodePlan;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PrefixExtent {
encoded_len: NonZeroUsize,
}
impl PrefixExtent {
#[must_use]
pub const fn new(encoded_len: NonZeroUsize) -> Self {
Self { encoded_len }
}
#[must_use]
pub const fn encoded_len(self) -> NonZeroUsize {
self.encoded_len
}
#[inline]
#[must_use]
pub fn split_input<'input>(&self, input: &'input [u8]) -> Option<(&'input [u8], &'input [u8])> {
let encoded_len = self.encoded_len.get();
if encoded_len > input.len() {
None
} else {
Some(input.split_at(encoded_len))
}
}
}
pub trait PrefixCodec {
type Value<'wire>
where
Self: 'wire;
type DecodeError: core::fmt::Debug;
type EncodeError: core::fmt::Debug;
type Plan<'value>: EncodePlan
where
Self: 'value;
fn validate_prefix(bytes: &[u8]) -> Result<PrefixExtent, Self::DecodeError>;
fn decode<'wire>(bytes: &'wire [u8]) -> Self::Value<'wire>;
fn plan<'value>(value: Self::Value<'value>) -> Result<Self::Plan<'value>, Self::EncodeError>;
}