Skip to main content

http_longpoll/
interop.rs

1use crate::http_poll::Foldable;
2
3#[cfg(feature = "axum")]
4pub mod axum {
5    use crate::http_poll::{Foldable, TotalSize};
6    use crate::session::{self};
7    use axum::response::IntoResponse;
8    pub use bytes::Bytes;
9    use bytes::BytesMut;
10    use http::StatusCode;
11
12    pub type Session<T = Bytes> = session::Session<T>;
13    pub type SessionHandle<T = Bytes> = session::SessionHandle<T>;
14
15    // Allow common response types to be used
16    impl Foldable for Bytes {
17        type Start = BytesMut;
18        fn append(current: &mut Self::Start, value: Self) -> TotalSize {
19            current.extend_from_slice(&value);
20            current.len()
21        }
22    }
23
24    impl IntoResponse for session::SessionError {
25        fn into_response(self) -> axum::response::Response {
26            match self {
27                Self::PollingError => StatusCode::BAD_REQUEST.into_response(),
28                Self::PollingTimeout => StatusCode::OK.into_response(),
29                Self::Closed => StatusCode::GONE.into_response(),
30            }
31        }
32    }
33}
34
35// Simple implementations independant of frameworks
36impl<T> Foldable for Vec<T> {
37    type Start = Self;
38    fn append(current: &mut Self::Start, mut value: Self) -> crate::http_poll::TotalSize {
39        current.append(&mut value);
40        current.len()
41    }
42}