pub trait Payload: Sized {
type Type: PayloadType;
// Provided methods
fn extract<'req>(req: &'req Request) -> Option<Result<Self, impl Error>>
where Self: Deserialize<'req> { ... }
fn inject(&self, res: &mut Response) -> Result<(), impl Error>
where Self: Serialize { ... }
}
Expand description
§Request/Response Payload
T
can be used as request body ifT: Payload + Deserialize
T
can be used as response body ifT: Payload + Serialize
in ohkami’s typed system.
It’s recommended to impl this by #[Payload]
attribute with a PayloadType
argument.
base.rs
/* This trait and the attribute */
use ohkami::typed::Payload;
/* A `PayloadType` for application/json payload */
use ohkami::builtin::payload::JSON;
use ohkami::serde::{Deserialize, Serialize};
#[Payload(JSON)]
#[derive(
Deserialize, // derive to use `User` as request body
Serialize // derive to use `User` as response body
)]
struct User {
id: usize,
name: String,
}
We derive Deserialize
or Serialize
in most cases, so #[Payload]
supports shorthands for that:
shorthand.rs
use ohkami::typed::Payload;
use ohkami::builtin::payload::JSON;
#[Payload(JSON/DS)]
struct User {
id: usize,
name: String,
}
After /
,
D
automatically derivesDesrerialize
for the structS
automatically derivesSerialize
for the struct
respectively.
Required Associated Types§
type Type: PayloadType
Provided Methods§
fn extract<'req>(req: &'req Request) -> Option<Result<Self, impl Error>>where
Self: Deserialize<'req>,
fn inject(&self, res: &mut Response) -> Result<(), impl Error>where
Self: Serialize,
Object Safety§
This trait is not object safe.