actix_chain/
next.rs

1//! All tools and utilities related to [`Link::next`](crate::Link::next)
2
3use actix_web::{
4    HttpResponse,
5    http::{StatusCode, header::HeaderName},
6};
7
8/// Response equivalent of [`actix_web::guard::Guard`].
9///
10/// Blocks responses that contain matching criteria
11/// and allows the request to be forwarded to the next
12/// [`Link`](crate::Link) in the [`Chain`](crate::Chain).
13pub trait Next {
14    fn next(&self, res: &HttpResponse) -> bool;
15}
16
17/// Simple [`StatusCode`] response guard.
18///
19/// Blocks the response the specified status-code is present.
20pub struct IsStatus(pub StatusCode);
21
22impl IsStatus {
23    pub fn new(status: StatusCode) -> Self {
24        Self(status)
25    }
26}
27
28impl From<StatusCode> for IsStatus {
29    #[inline]
30    fn from(value: StatusCode) -> Self {
31        Self::new(value)
32    }
33}
34
35impl Next for IsStatus {
36    #[inline]
37    fn next(&self, res: &HttpResponse) -> bool {
38        res.status() == self.0
39    }
40}
41
42/// Simple [`HeaderName`]
43/// response guard.
44///
45/// Blocks the response if the specified header is present.
46pub struct HasHeader(pub HeaderName);
47
48impl HasHeader {
49    pub fn new(name: HeaderName) -> Self {
50        Self(name)
51    }
52}
53
54impl From<HeaderName> for HasHeader {
55    #[inline]
56    fn from(value: HeaderName) -> Self {
57        Self::new(value)
58    }
59}
60
61impl Next for HasHeader {
62    #[inline]
63    fn next(&self, res: &HttpResponse) -> bool {
64        res.headers().contains_key(&self.0)
65    }
66}