1use core::fmt;
4use core::num::NonZeroUsize;
5
6use calandria::RetainedBytes;
7
8use crate::{MatchKey, write::WriteQueueLimits};
9
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub struct MatchKeySpace {
13 first: MatchKey,
14 last: MatchKey,
15}
16
17impl MatchKeySpace {
18 pub const fn new(first: u32, last: u32) -> Result<Self, LimitsError> {
20 if first > last {
21 return Err(LimitsError::EmptyMatchKeySpace);
22 }
23 Ok(Self {
24 first: MatchKey::new(first),
25 last: MatchKey::new(last),
26 })
27 }
28
29 pub const fn first(self) -> MatchKey {
31 self.first
32 }
33
34 pub const fn last(self) -> MatchKey {
36 self.last
37 }
38
39 pub fn capacity(self) -> u64 {
41 u64::from(self.last.get()) - u64::from(self.first.get()) + 1
42 }
43
44 pub(crate) const fn next(self, current: MatchKey) -> MatchKey {
45 if current.get() == self.last.get() {
46 self.first
47 } else {
48 MatchKey::new(current.get() + 1)
49 }
50 }
51}
52
53#[derive(Clone, Copy, Debug, Eq, PartialEq)]
55pub struct ConnectionLimits {
56 max_operations: NonZeroUsize,
57 max_retained_bytes: RetainedBytes,
58 max_write_frames: NonZeroUsize,
59 max_write_retained_bytes: RetainedBytes,
60 match_keys: MatchKeySpace,
61}
62
63impl ConnectionLimits {
64 pub const fn new(
66 max_operations: usize,
67 max_retained_bytes: RetainedBytes,
68 max_write_frames: usize,
69 max_write_retained_bytes: RetainedBytes,
70 match_keys: MatchKeySpace,
71 ) -> Result<Self, LimitsError> {
72 let Some(max_operations) = NonZeroUsize::new(max_operations) else {
73 return Err(LimitsError::ZeroOperationCapacity);
74 };
75 let Some(max_write_frames) = NonZeroUsize::new(max_write_frames) else {
76 return Err(LimitsError::ZeroWriteCapacity);
77 };
78 Ok(Self {
79 max_operations,
80 max_retained_bytes,
81 max_write_frames,
82 max_write_retained_bytes,
83 match_keys,
84 })
85 }
86
87 pub const fn max_operations(self) -> usize {
89 self.max_operations.get()
90 }
91
92 pub const fn max_retained_bytes(self) -> RetainedBytes {
94 self.max_retained_bytes
95 }
96
97 pub const fn max_write_frames(self) -> usize {
99 self.max_write_frames.get()
100 }
101
102 pub const fn max_write_retained_bytes(self) -> RetainedBytes {
104 self.max_write_retained_bytes
105 }
106
107 pub const fn match_keys(self) -> MatchKeySpace {
109 self.match_keys
110 }
111
112 pub(crate) const fn write_queue_limits(self) -> WriteQueueLimits {
114 WriteQueueLimits::new(self.max_write_frames, self.max_write_retained_bytes)
115 }
116}
117
118#[derive(Clone, Copy, Debug, Eq, PartialEq)]
120#[non_exhaustive]
121pub enum LimitsError {
122 ZeroOperationCapacity,
124 ZeroWriteCapacity,
126 EmptyMatchKeySpace,
128}
129
130impl fmt::Display for LimitsError {
131 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
132 formatter.write_str(match self {
133 Self::ZeroOperationCapacity => "operation capacity must be nonzero",
134 Self::ZeroWriteCapacity => "write-frame capacity must be nonzero",
135 Self::EmptyMatchKeySpace => "match-key range must be nonempty",
136 })
137 }
138}
139
140impl core::error::Error for LimitsError {}