1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use crate::{Request, Response, Error, Result};
use async_trait::async_trait;
#[async_trait]
pub trait Service {
async fn handle(&self, req: &mut Request) -> Result<Option<Response>>;
}
pub async fn handle<'a>(
services: &'a Vec<&'a Box<dyn Service>>,
request: &mut Request,
) -> Result<Response> {
for service in services {
if let Some(result) = service.handle(request).await? {
return Ok(result);
}
}
let err = Error::MethodNotFound {
name: request.method().to_string(),
id: request.id.clone(),
};
Ok((request, err).into())
}
pub async fn serve<'a>(
services: &'a Vec<&'a Box<dyn Service>>,
request: &mut Request,
) -> Response {
match handle(services, request).await {
Ok(response) => response,
Err(e) => e.into(),
}
}