Skip to main content

hitbox_http/predicates/version/
operation.rs

1//! HTTP version matching operations.
2
3use http::Version;
4
5/// Operations for matching HTTP versions.
6///
7/// # Examples
8///
9/// ```
10/// use hitbox_http::predicates::version::Operation;
11/// use http::Version;
12///
13/// // Match HTTP/2 only
14/// let op = Operation::Eq(Version::HTTP_2);
15/// assert!(op.check(Version::HTTP_2));
16/// assert!(!op.check(Version::HTTP_11));
17///
18/// // Match HTTP/1.1 or HTTP/2
19/// let op = Operation::In(vec![Version::HTTP_11, Version::HTTP_2]);
20/// assert!(op.check(Version::HTTP_11));
21/// assert!(op.check(Version::HTTP_2));
22/// ```
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub enum Operation {
25    /// Use when caching should apply to a specific HTTP version only.
26    ///
27    /// Best for HTTP/2-only features or HTTP/1.1 fallback scenarios.
28    Eq(Version),
29    /// Use when caching should apply to multiple HTTP versions.
30    ///
31    /// Best for supporting both HTTP/1.1 and HTTP/2 while excluding HTTP/1.0.
32    In(Vec<Version>),
33}
34
35impl Operation {
36    /// Check if the operation matches the given version
37    pub fn check(&self, version: Version) -> bool {
38        match self {
39            Operation::Eq(expected) => version == *expected,
40            Operation::In(versions) => versions.contains(&version),
41        }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_eq_matches() {
51        let op = Operation::Eq(Version::HTTP_11);
52        assert!(op.check(Version::HTTP_11));
53        assert!(!op.check(Version::HTTP_2));
54    }
55
56    #[test]
57    fn test_in_matches() {
58        let op = Operation::In(vec![Version::HTTP_11, Version::HTTP_2]);
59        assert!(op.check(Version::HTTP_11));
60        assert!(op.check(Version::HTTP_2));
61        assert!(!op.check(Version::HTTP_10));
62    }
63}