mod attachment;
mod binary;
mod event_stream;
mod json;
mod plain_text;
mod response;
use std::str::FromStr;
use mime::Mime;
use poem::{Request, RequestBody, Result};
pub use self::{
attachment::Attachment, binary::Binary, event_stream::EventStream, json::Json,
plain_text::PlainText, response::Response,
};
use crate::registry::{MetaSchemaRef, Registry};
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 {
const IS_REQUIRED: bool;
async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self>;
}
#[doc(hidden)]
pub struct ContentTypeTable {
items: Vec<(Mime, usize)>,
}
#[doc(hidden)]
impl ContentTypeTable {
pub fn new(types: &[&str]) -> Self {
let mut items = types
.iter()
.enumerate()
.map(|(idx, s)| (Mime::from_str(s).unwrap(), idx))
.collect::<Vec<_>>();
items.sort_by_key(|(x, _)| {
let mut n = 0;
if x.type_() == mime::STAR {
n += 1;
}
if x.subtype() == mime::STAR {
n += 2;
}
n
});
ContentTypeTable { items }
}
pub fn matches(&self, content_type: &str) -> Option<usize> {
for (mime, idx) in &self.items {
if let Ok(x) = Mime::from_str(content_type) {
if (x.type_() == mime.type_() || mime.type_() == mime::STAR)
&& (x.subtype() == mime.subtype() || mime.subtype() == mime::STAR)
{
return Some(*idx);
}
}
}
None
}
}