use crate::rejection::{ContentTypeRejection, MimeParsingFailed, MissingJsonContentType};
use axum::extract::RequestParts;
pub fn check_json_content_type<B>(req: &RequestParts<B>) -> Result<(), ContentTypeRejection> {
let mime = req
.headers()
.ok_or(MissingJsonContentType)?
.get(http::header::CONTENT_TYPE)
.ok_or(MissingJsonContentType)?
.to_str()
.map_err(|_| MissingJsonContentType)?
.parse::<mime::Mime>()
.map_err(|_| MimeParsingFailed)?;
if mime.type_() == "application"
&& (mime.subtype() == "json" || mime.suffix().filter(|name| *name == "json").is_some())
{
Ok(())
} else {
Err(ContentTypeRejection::MissingJsonContentType(MissingJsonContentType))
}
}