use super::DecodeConfigError;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum MemberMode {
#[default]
Single,
Concatenated,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct WindowLimit {
bits: u8,
large: bool,
}
impl WindowLimit {
pub const fn standard(max_bits: u8) -> Result<Self, DecodeConfigError> {
if max_bits < 10 || max_bits > 24 {
return Err(DecodeConfigError::StandardWindow { max_bits });
}
Ok(Self {
bits: max_bits,
large: false,
})
}
pub const fn large(max_bits: u8) -> Result<Self, DecodeConfigError> {
if max_bits < 10 || max_bits > 62 {
return Err(DecodeConfigError::LargeWindow { max_bits });
}
Ok(Self {
bits: max_bits,
large: true,
})
}
pub const fn max_bits(self) -> u8 {
self.bits
}
pub const fn allows_large(self) -> bool {
self.large
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct DecodeLimits {
input: Option<u64>,
output: Option<u64>,
workspace: Option<usize>,
}
impl DecodeLimits {
pub const fn with_max_input_bytes(mut self, value: Option<u64>) -> Self {
self.input = value;
self
}
pub const fn with_max_output_bytes(mut self, value: Option<u64>) -> Self {
self.output = value;
self
}
pub const fn with_max_workspace_bytes(mut self, value: Option<usize>) -> Self {
self.workspace = value;
self
}
pub const fn max_input_bytes(self) -> Option<u64> {
self.input
}
pub const fn max_output_bytes(self) -> Option<u64> {
self.output
}
pub const fn max_workspace_bytes(self) -> Option<usize> {
self.workspace
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DecoderConfig {
window: WindowLimit,
members: MemberMode,
limits: DecodeLimits,
}
impl Default for DecoderConfig {
fn default() -> Self {
Self {
window: WindowLimit {
bits: 62,
large: true,
},
members: MemberMode::Single,
limits: DecodeLimits::default(),
}
}
}
impl DecoderConfig {
pub const fn with_window_limit(mut self, value: WindowLimit) -> Self {
self.window = value;
self
}
pub const fn with_member_mode(mut self, value: MemberMode) -> Self {
self.members = value;
self
}
pub const fn with_limits(mut self, value: DecodeLimits) -> Self {
self.limits = value;
self
}
pub const fn window_limit(&self) -> WindowLimit {
self.window
}
pub const fn member_mode(&self) -> MemberMode {
self.members
}
pub const fn limits(&self) -> DecodeLimits {
self.limits
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum OutputSize {
#[default]
Unknown,
Exact(u64),
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct DecodeStreamConfig {
size: OutputSize,
}
impl DecodeStreamConfig {
pub const fn with_output_size(mut self, value: OutputSize) -> Self {
self.size = value;
self
}
pub const fn output_size(&self) -> OutputSize {
self.size
}
}
impl From<OutputSize> for DecodeStreamConfig {
fn from(size: OutputSize) -> Self {
Self { size }
}
}