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
//! Commonly used payload types.

mod binary;
mod json;
mod plain_text;

pub use binary::Binary;
pub use json::Json;
pub use plain_text::PlainText;
use poem::{IntoResponse, Request, RequestBody, Result};

use crate::registry::{MetaSchemaRef, Registry};

/// Represents a payload type.
#[poem::async_trait]
pub trait Payload: IntoResponse + Sized {
    /// The content type of this payload.
    const CONTENT_TYPE: &'static str;

    /// Gets schema reference of this payload.
    fn schema_ref() -> MetaSchemaRef;

    /// Register the schema contained in this payload to the registry.
    #[allow(unused_variables)]
    fn register(registry: &mut Registry) {}

    /// Parse the payload object from the HTTP request.
    async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self>;
}