nntp_proxy/types/config/
cache.rs

1//! Cache capacity configuration types
2
3use std::num::NonZeroUsize;
4
5nonzero_newtype! {
6    /// A non-zero cache capacity
7    ///
8    /// Ensures caches track at least 1 item
9    pub struct CacheCapacity(NonZeroUsize: usize, serialize as serialize_u64);
10}
11
12impl CacheCapacity {
13    /// Default cache capacity (1000 items)
14    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    // Use consolidated test macros for common patterns
43    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    // Special test for zero defaulting to DEFAULT
52    #[test]
53    fn test_from_str_zero_defaults() {
54        // Zero values should default to DEFAULT (1000)
55        let capacity: CacheCapacity = "0".parse().unwrap();
56        assert_eq!(capacity.get(), 1000);
57    }
58}