monovm-whois-rust 1.0.0

Domain WHOIS and RDAP lookups with availability detection, structured record parsing, referral chasing, caching and rate limiting.
Documentation
//! [`NullCache`]: the do-nothing cache.

use crate::cache::{CacheKey, ResponseCache};
use crate::transport::RawResponse;

/// A cache that stores nothing and never hits.
///
/// The Null Object for [`ResponseCache`]. Having one means the caching layer is
/// always present and always called the same way, so no code path has to branch
/// on whether a cache exists — the "no caching" configuration is a value rather
/// than a special case.
///
/// ```
/// use monovm_whois::cache::{CacheKey, NullCache, ResponseCache};
///
/// let cache = NullCache;
/// assert!(cache.get(&CacheKey::new("whois.example", "example.com")).is_none());
/// assert_eq!(cache.len(), Some(0));
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NullCache;

impl ResponseCache for NullCache {
    fn get(&self, _key: &CacheKey) -> Option<RawResponse> {
        None
    }

    fn put(&self, _key: CacheKey, _response: RawResponse) {}

    fn clear(&self) {}

    fn len(&self) -> Option<usize> {
        Some(0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::registry::Endpoint;
    use crate::transport::ResponseKind;
    use std::time::Duration;

    #[test]
    fn never_returns_what_was_stored() {
        let cache = NullCache;
        let key = CacheKey::new("whois.example", "example.com");

        cache.put(
            key.clone(),
            RawResponse::new(
                Endpoint::whois("whois.example"),
                ResponseKind::WhoisText,
                "record",
                Duration::ZERO,
            ),
        );

        assert!(cache.get(&key).is_none());
        assert_eq!(cache.len(), Some(0));
        cache.clear();
    }
}