Trait ohkami::typed::Payload

source ·
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 if T: Payload + Deserialize
  • T can be used as response body if T: 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 derives Desrerialize for the struct
  • S automatically derives Serialize for the struct

respectively.

Required Associated Types§

Provided Methods§

source

fn extract<'req>(req: &'req Request) -> Option<Result<Self, impl Error>>
where Self: Deserialize<'req>,

source

fn inject(&self, res: &mut Response) -> Result<(), impl Error>
where Self: Serialize,

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Payload for Value

§

type Type = JSON

source§

impl<P: Payload<Type = JSON>> Payload for &[P]

§

type Type = JSON

source§

impl<P: Payload<Type = JSON>> Payload for Vec<P>

§

type Type = JSON

source§

impl<P: Payload<Type = JSON>, const N: usize> Payload for [P; N]

§

type Type = JSON

Implementors§