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
44
45
46
47
48
49
50
51
mod binary;
mod json;
mod plain_text;
pub use binary::Binary;
pub use json::Json;
pub use plain_text::PlainText;
use poem::{Request, RequestBody, Result};
use crate::{
registry::{MetaSchemaRef, Registry},
ParseRequestError,
};
pub trait Payload: Send {
const CONTENT_TYPE: &'static str;
fn schema_ref() -> MetaSchemaRef;
#[allow(unused_variables)]
fn register(registry: &mut Registry) {}
}
#[poem::async_trait]
pub trait ParsePayload: Sized {
async fn from_request(
request: &Request,
body: &mut RequestBody,
) -> Result<Self, ParseRequestError>;
}
#[poem::async_trait]
impl<T: ParsePayload> ParsePayload for Result<T> {
async fn from_request(
request: &Request,
body: &mut RequestBody,
) -> Result<Self, ParseRequestError> {
match T::from_request(request, body).await {
Ok(payload) => Ok(Ok(payload)),
Err(err) => Ok(Err(err.into())),
}
}
}