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}