Skip to main content

Operation

Enum Operation 

Source
pub enum Operation {
    Limit {
        bytes: usize,
    },
    Plain(PlainOperation),
    Jq {
        filter: JqExpression,
        operation: JqOperation,
    },
}
Expand description

Matching operations for HTTP body content.

§Caveats

Body predicates may consume bytes from the stream. The body is transitioned to BufferedBody::Partial or BufferedBody::Complete after evaluation.

§Examples

§Size Limit

Only cache responses smaller than 1MB:

use hitbox_http::predicates::body::Operation;

let op = Operation::Limit { bytes: 1024 * 1024 };

§Plain Text Matching

Cache only if body contains a success marker:

use bytes::Bytes;
use hitbox_http::predicates::body::{Operation, PlainOperation};

let op = Operation::Plain(PlainOperation::Contains(Bytes::from("\"success\":true")));

Cache only if body starts with JSON array:

use bytes::Bytes;
use hitbox_http::predicates::body::{Operation, PlainOperation};

let op = Operation::Plain(PlainOperation::Starts(Bytes::from("[")));

Cache only if body matches a regex pattern:

use hitbox_http::predicates::body::{Operation, PlainOperation};

let regex = regex::bytes::Regex::new(r#""status":\s*"(ok|success)""#).unwrap();
let op = Operation::Plain(PlainOperation::RegExp(regex));

§JQ (JSON) Matching

Cache only if response has non-empty items array:

use hitbox_http::predicates::body::{Operation, JqExpression, JqOperation};

let op = Operation::Jq {
    filter: JqExpression::compile(".items | length > 0").unwrap(),
    operation: JqOperation::Eq(serde_json::Value::Bool(true)),
};

Cache only if user role exists:

use hitbox_http::predicates::body::{Operation, JqExpression, JqOperation};

let op = Operation::Jq {
    filter: JqExpression::compile(".user.role").unwrap(),
    operation: JqOperation::Exist,
};

Cache only if status is one of allowed values:

use hitbox_http::predicates::body::{Operation, JqExpression, JqOperation};

let op = Operation::Jq {
    filter: JqExpression::compile(".status").unwrap(),
    operation: JqOperation::In(vec![
        serde_json::json!("published"),
        serde_json::json!("approved"),
    ]),
};

Variants§

§

Limit

Use when you need to limit cached response sizes.

Best for preventing cache bloat from large responses like file downloads. Reads up to bytes + 1 to determine if the limit is exceeded.

Fields

§bytes: usize

Maximum body size in bytes.

§

Plain(PlainOperation)

Use when matching raw body bytes without parsing.

Best for text responses, checking signatures, or simple content matching.

§

Jq

Use when caching depends on JSON content structure or values.

Best for APIs where cacheability depends on response data (e.g., user roles, feature flags).

Fields

§filter: JqExpression

The compiled JQ filter expression.

§operation: JqOperation

The operation to apply to the filter result.

Implementations§

Source§

impl Operation

Source

pub async fn check<B>( &self, body: BufferedBody<B>, ) -> PredicateResult<BufferedBody<B>>
where B: HttpBody + Unpin, B::Data: Send,

Check if the operation matches the body. Returns PredicateResult::Cacheable if the operation is satisfied, PredicateResult::NonCacheable otherwise.

Trait Implementations§

Source§

impl Debug for Operation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more