1use crate::http::{ResponseError, error::DispatchError};
2use crate::{Ctx, Service, ServiceFactory, io::Filter};
3
4use super::control::{Control, ControlAck};
5
6#[derive(Debug, Default)]
7pub struct DefaultControlService;
10
11impl<St, F, Err> Service<St, Control<F, Err>> for DefaultControlService
12where
13 F: Filter,
14 Err: ResponseError,
15{
16 type Res = ControlAck<F>;
17 type Error = DispatchError;
18
19 #[inline]
20 async fn call(
21 &self,
22 r: Control<F, Err>,
23 _: Ctx<'_, Self, St>,
24 ) -> Result<Self::Res, Self::Error> {
25 Ok(r.ack())
26 }
27}
28
29impl<St, F, Err> ServiceFactory<St, Control<F, Err>> for DefaultControlService
30where
31 F: Filter,
32 Err: ResponseError,
33{
34 type Res = ControlAck<F>;
35 type Error = DispatchError;
36
37 type Service = DefaultControlService;
38 type InitError = DispatchError;
39
40 async fn create(&self, _: &St) -> Result<Self::Service, Self::InitError> {
41 Ok(DefaultControlService)
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use std::io;
48
49 use super::*;
50 use crate::http::{Request, h1::control::ControlResult};
51 use crate::{Pipeline, io::Base};
52
53 #[crate::rt_test]
58 async fn default_acks() {
59 let svc: Pipeline<Control<Base, io::Error>, _, _> =
60 ServiceFactory::pipeline(&DefaultControlService, ())
61 .await
62 .unwrap();
63
64 let ack = svc
65 .call(Control::<Base, io::Error>::request(Request::new()))
66 .await
67 .unwrap();
68 assert!(matches!(ack.result, ControlResult::Publish(_)));
69
70 let ack = svc
71 .call(Control::<Base, io::Error>::keepalive(false))
72 .await
73 .unwrap();
74 assert!(matches!(ack.result, ControlResult::Stop));
75 }
76}