nntp_proxy/types/config/
cache.rs1use std::num::NonZeroUsize;
4
5nonzero_newtype! {
6 pub struct CacheCapacity(NonZeroUsize: usize, serialize as serialize_u64);
10}
11
12impl CacheCapacity {
13 pub const DEFAULT: Self = Self(NonZeroUsize::new(1000).unwrap());
15}
16
17impl std::str::FromStr for CacheCapacity {
18 type Err = std::num::ParseIntError;
19
20 fn from_str(s: &str) -> Result<Self, Self::Err> {
21 let value = s.parse::<usize>()?;
22 Ok(Self::new(value).unwrap_or(Self::DEFAULT))
23 }
24}
25
26impl PartialOrd for CacheCapacity {
27 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
28 Some(self.cmp(other))
29 }
30}
31
32impl Ord for CacheCapacity {
33 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
34 self.get().cmp(&other.get())
35 }
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 test_nonzero_newtype_full!(
44 CacheCapacity,
45 default: 1000,
46 test_value: 5000,
47 ordering: (100, 1000),
48 from_str: ("2000", 2000, "not_a_number")
49 );
50
51 #[test]
53 fn test_from_str_zero_defaults() {
54 let capacity: CacheCapacity = "0".parse().unwrap();
56 assert_eq!(capacity.get(), 1000);
57 }
58}