poem_openapi/payload/
mod.rs

1//! Commonly used payload types.
2
3mod attachment;
4mod base64_payload;
5mod binary;
6mod event_stream;
7mod form;
8mod html;
9mod json;
10mod plain_text;
11mod response;
12mod xml;
13mod yaml;
14
15use std::future::Future;
16
17use poem::{Request, RequestBody, Result};
18
19pub use self::{
20    attachment::{Attachment, AttachmentType},
21    base64_payload::Base64,
22    binary::Binary,
23    event_stream::EventStream,
24    form::Form,
25    html::Html,
26    json::Json,
27    plain_text::PlainText,
28    response::Response,
29    xml::Xml,
30    yaml::Yaml,
31};
32use crate::registry::{MetaSchemaRef, Registry};
33
34/// Represents a payload type.
35pub trait Payload: Send {
36    /// The content type of this payload.
37    const CONTENT_TYPE: &'static str;
38
39    /// Check the content type of incoming request
40    fn check_content_type(content_type: &str) -> bool {
41        content_type == Self::CONTENT_TYPE
42    }
43
44    /// Gets schema reference of this payload.
45    fn schema_ref() -> MetaSchemaRef;
46
47    /// Register the schema contained in this payload to the registry.
48    #[allow(unused_variables)]
49    fn register(registry: &mut Registry) {}
50}
51
52/// Represents a payload that can parse from HTTP request.
53pub trait ParsePayload: Sized {
54    /// If it is `true`, it means that this payload is required.
55    const IS_REQUIRED: bool;
56
57    /// Parse the payload object from the HTTP request.
58    fn from_request(
59        request: &Request,
60        body: &mut RequestBody,
61    ) -> impl Future<Output = Result<Self>> + Send;
62}