#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)]
pub enum FrameMode {
#[default]
Single,
Concatenated,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)]
pub struct DecodeLimits {
input: Option<u64>,
output: Option<u64>,
}
impl DecodeLimits {
pub const DEFAULT: Self = Self {
input: None,
output: None,
};
#[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_input_bytes(&self) -> Option<u64> {
self.input
}
#[must_use]
pub const fn max_output_bytes(&self) -> Option<u64> {
self.output
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct DecoderConfig {
frame_mode: FrameMode,
limits: DecodeLimits,
content_checksum: bool,
compressed_checksum: bool,
}
impl DecoderConfig {
pub const DEFAULT: Self = Self {
frame_mode: FrameMode::Single,
limits: DecodeLimits::DEFAULT,
content_checksum: true,
compressed_checksum: true,
};
#[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 with_content_checksum_check(mut self, enabled: bool) -> Self {
self.content_checksum = enabled;
self
}
#[must_use]
pub const fn with_compressed_checksum_check(mut self, enabled: bool) -> Self {
self.compressed_checksum = enabled;
self
}
#[must_use]
pub const fn frame_mode(&self) -> FrameMode {
self.frame_mode
}
#[must_use]
pub const fn limits(&self) -> DecodeLimits {
self.limits
}
#[must_use]
pub const fn content_checksum_check(&self) -> bool {
self.content_checksum
}
#[must_use]
pub const fn compressed_checksum_check(&self) -> bool {
self.compressed_checksum
}
}
impl Default for DecoderConfig {
fn default() -> Self {
Self::DEFAULT
}
}
impl From<FrameMode> for DecoderConfig {
fn from(mode: FrameMode) -> Self {
Self::DEFAULT.with_frame_mode(mode)
}
}
impl From<DecodeLimits> for DecoderConfig {
fn from(limits: DecodeLimits) -> Self {
Self::DEFAULT.with_limits(limits)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builders_set_every_field() {
let limits = DecodeLimits::DEFAULT
.with_max_input_bytes(Some(1))
.with_max_output_bytes(Some(2));
let config = DecoderConfig::from(limits)
.with_frame_mode(FrameMode::Concatenated)
.with_content_checksum_check(false)
.with_compressed_checksum_check(false);
assert_eq!(config.limits(), limits);
assert_eq!(config.frame_mode(), FrameMode::Concatenated);
assert!(!config.content_checksum_check());
assert!(!config.compressed_checksum_check());
assert_eq!(DecoderConfig::default(), DecoderConfig::DEFAULT);
assert_eq!(DecodeLimits::default(), DecodeLimits::DEFAULT);
}
}