use crate::error::{WireError, WireResult};
use crate::frame::header::{CRC_LEN, HEADER_LEN};
pub const DEFAULT_MAX_PAYLOAD_BYTES: usize = 64 * 1024 * 1024;
pub const MAX_SUPPORTED_PAYLOAD_BYTES: usize = u32::MAX as usize;
pub const MIN_MAX_PAYLOAD_BYTES: usize = 0;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FrameLimits {
max_payload_bytes: usize,
require_crc: bool,
}
impl FrameLimits {
#[must_use]
pub const fn new() -> Self {
Self {
max_payload_bytes: DEFAULT_MAX_PAYLOAD_BYTES,
require_crc: false,
}
}
#[must_use]
pub const fn network() -> Self {
Self {
max_payload_bytes: DEFAULT_MAX_PAYLOAD_BYTES,
require_crc: true,
}
}
#[must_use]
pub const fn uds() -> Self {
Self::new()
}
#[must_use]
pub const fn with_max_payload_bytes(mut self, bytes: usize) -> Self {
self.max_payload_bytes = if bytes > MAX_SUPPORTED_PAYLOAD_BYTES {
MAX_SUPPORTED_PAYLOAD_BYTES
} else {
bytes
};
self
}
#[must_use]
pub const fn with_require_crc(mut self, required: bool) -> Self {
self.require_crc = required;
self
}
#[must_use]
pub const fn max_payload_bytes(&self) -> usize {
self.max_payload_bytes
}
#[must_use]
pub const fn require_crc(&self) -> bool {
self.require_crc
}
#[must_use]
pub const fn max_frame_bytes(&self) -> usize {
HEADER_LEN + self.max_payload_bytes + CRC_LEN
}
pub const fn check_payload_len(&self, len: usize) -> WireResult<()> {
if len > self.max_payload_bytes {
return Err(WireError::FrameTooLarge {
len,
max: self.max_payload_bytes,
});
}
Ok(())
}
#[must_use]
pub const fn intersect(self, other: Self) -> Self {
let max_payload_bytes = if self.max_payload_bytes < other.max_payload_bytes {
self.max_payload_bytes
} else {
other.max_payload_bytes
};
Self {
max_payload_bytes,
require_crc: self.require_crc || other.require_crc,
}
}
}
impl Default for FrameLimits {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn defaults_match_the_blueprint() {
let limits = FrameLimits::default();
assert_eq!(limits.max_payload_bytes(), 64 * 1024 * 1024);
assert_eq!(limits.max_payload_bytes(), DEFAULT_MAX_PAYLOAD_BYTES);
assert!(!limits.require_crc());
assert_eq!(FrameLimits::new(), limits);
assert_eq!(FrameLimits::uds(), limits);
}
#[test]
fn network_requires_crc_but_keeps_the_size_cap() {
let network = FrameLimits::network();
assert!(network.require_crc());
assert_eq!(network.max_payload_bytes(), DEFAULT_MAX_PAYLOAD_BYTES);
}
#[test]
fn builders_are_independent() {
let limits = FrameLimits::default()
.with_max_payload_bytes(4096)
.with_require_crc(true);
assert_eq!(limits.max_payload_bytes(), 4096);
assert!(limits.require_crc());
let relaxed = limits.with_require_crc(false);
assert_eq!(relaxed.max_payload_bytes(), 4096);
assert!(!relaxed.require_crc());
}
#[test]
fn cap_is_clamped_to_the_header_field_width() {
for request in [usize::MAX, MAX_SUPPORTED_PAYLOAD_BYTES + 1] {
let limits = FrameLimits::default().with_max_payload_bytes(request);
assert_eq!(limits.max_payload_bytes(), MAX_SUPPORTED_PAYLOAD_BYTES);
}
let exact = FrameLimits::default().with_max_payload_bytes(MAX_SUPPORTED_PAYLOAD_BYTES);
assert_eq!(exact.max_payload_bytes(), MAX_SUPPORTED_PAYLOAD_BYTES);
}
#[test]
fn zero_cap_admits_only_empty_payloads() {
let limits = FrameLimits::default().with_max_payload_bytes(MIN_MAX_PAYLOAD_BYTES);
assert!(limits.check_payload_len(0).is_ok());
assert!(limits.check_payload_len(1).is_err());
}
#[test]
fn boundary_lengths_are_inclusive() {
let limits = FrameLimits::default().with_max_payload_bytes(10);
assert!(limits.check_payload_len(9).is_ok());
assert!(limits.check_payload_len(10).is_ok());
match limits.check_payload_len(11) {
Err(WireError::FrameTooLarge { len, max }) => {
assert_eq!(len, 11);
assert_eq!(max, 10);
}
other => panic!("expected FrameTooLarge, got {other:?}"),
}
}
#[test]
fn max_frame_bytes_accounts_for_header_and_trailer() {
let limits = FrameLimits::default().with_max_payload_bytes(1000);
assert_eq!(limits.max_frame_bytes(), HEADER_LEN + 1000 + CRC_LEN);
}
#[test]
fn intersect_takes_the_stricter_side() {
let a = FrameLimits::default().with_max_payload_bytes(100);
let b = FrameLimits::default()
.with_max_payload_bytes(200)
.with_require_crc(true);
let ab = a.intersect(b);
let ba = b.intersect(a);
assert_eq!(ab, ba, "intersection is commutative");
assert_eq!(ab.max_payload_bytes(), 100);
assert!(ab.require_crc());
}
#[test]
fn intersect_is_idempotent() {
let limits = FrameLimits::network().with_max_payload_bytes(77);
assert_eq!(limits.intersect(limits), limits);
}
}