use super::DecodeConfigError;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum FrameMode {
#[default]
Concatenated,
Single,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DecodeLimits {
window_log: u8,
input: Option<u64>,
output: Option<u64>,
}
impl DecodeLimits {
pub const MIN_WINDOW_LOG: u8 = 10;
pub const MAX_WINDOW_LOG: u8 = 31;
pub const DEFAULT_WINDOW_LOG: u8 = 27;
pub const DEFAULT: Self = Self {
window_log: Self::DEFAULT_WINDOW_LOG,
input: None,
output: None,
};
pub const fn with_max_window_log(mut self, log: u8) -> Result<Self, DecodeConfigError> {
if log < Self::MIN_WINDOW_LOG || log > Self::MAX_WINDOW_LOG {
return Err(DecodeConfigError::WindowLog { log });
}
self.window_log = log;
Ok(self)
}
#[must_use]
pub const fn with_max_input_bytes(mut self, limit: Option<u64>) -> Self {
self.input = limit;
self
}
#[must_use]
pub const fn with_max_output_bytes(mut self, limit: Option<u64>) -> Self {
self.output = limit;
self
}
#[must_use]
pub const fn max_window_log(&self) -> u8 {
self.window_log
}
#[must_use]
pub const fn max_window_size(&self) -> u64 {
1 << self.window_log
}
#[must_use]
pub const fn max_input_bytes(&self) -> Option<u64> {
self.input
}
#[must_use]
pub const fn max_output_bytes(&self) -> Option<u64> {
self.output
}
}
impl Default for DecodeLimits {
fn default() -> Self {
Self::DEFAULT
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DecoderConfig {
frame_mode: FrameMode,
limits: DecodeLimits,
}
impl DecoderConfig {
pub const DEFAULT: Self = Self {
frame_mode: FrameMode::Concatenated,
limits: DecodeLimits::DEFAULT,
};
#[must_use]
pub const fn with_frame_mode(mut self, mode: FrameMode) -> Self {
self.frame_mode = mode;
self
}
#[must_use]
pub const fn with_limits(mut self, limits: DecodeLimits) -> Self {
self.limits = limits;
self
}
#[must_use]
pub const fn frame_mode(&self) -> FrameMode {
self.frame_mode
}
#[must_use]
pub const fn limits(&self) -> DecodeLimits {
self.limits
}
}
impl Default for DecoderConfig {
fn default() -> Self {
Self::DEFAULT
}
}
impl From<DecodeLimits> for DecoderConfig {
fn from(limits: DecodeLimits) -> Self {
Self::DEFAULT.with_limits(limits)
}
}
impl From<FrameMode> for DecoderConfig {
fn from(mode: FrameMode) -> Self {
Self::DEFAULT.with_frame_mode(mode)
}
}