use fred::types::Key;
use http::{Request, Response};

/// Resolves a Redis key for each incoming request.
///
/// Requests with the same key are rate limited together.
pub trait KeyResolver: Clone {
    /// The type of the response body when [`KeyResolver::key`] fails.
    /// Must match the type of the response body of the underlying service.
    type ErrorBody;

    /// Attempts to resolve a key given the request.
    ///
    /// - `Ok(Some(key))` --- rate-limit this request against `key`
    /// - `Ok(None)` --- skip rate-limiting, pass the request through
    /// - `Err(response)` --- return `response` to the client immediately
    fn key<T>(&self, req: &Request<T>) -> Result<Option<Key>, Response<Self::ErrorBody>>;
}
