axum_serde/
lib.rs

1#![doc = include_str!("../README.md")]
2#![deny(unsafe_code, missing_docs, clippy::unwrap_used)]
3
4pub mod macros;
5#[cfg(feature = "msgpack")]
6pub mod msgpack;
7pub mod rejection;
8#[cfg(feature = "sonic")]
9pub mod sonic;
10#[cfg(feature = "toml")]
11pub mod toml;
12#[cfg(feature = "xml")]
13pub mod xml;
14#[cfg(feature = "yaml")]
15pub mod yaml;
16
17#[cfg(feature = "cbor")]
18pub mod cbor;
19
20use http::{header, HeaderMap};
21use mime::Mime;
22
23#[cfg(feature = "cbor")]
24pub use cbor::Cbor;
25#[cfg(feature = "msgpack")]
26pub use msgpack::{MsgPack, MsgPackRaw};
27pub use rejection::Rejection;
28#[cfg(feature = "sonic")]
29pub use sonic::Sonic;
30#[cfg(feature = "toml")]
31pub use toml::Toml;
32#[cfg(feature = "xml")]
33pub use xml::Xml;
34#[cfg(feature = "yaml")]
35pub use yaml::Yaml;
36
37/// Checks if the content type in the given headers matches the expected content type.
38///
39/// # Arguments
40///
41/// * `headers` - A reference to the `HeaderMap` containing the headers.
42/// * `expected_content_type` - A reference to the `mime::Mime` representing the expected content type.
43///
44/// # Returns
45///
46/// Returns `true` if the content type in the headers matches the expected content type, otherwise `false`.
47pub fn check_content_type(headers: &HeaderMap, expected_content_type: &str) -> bool {
48    let content_type = if let Some(content_type) = headers.get(header::CONTENT_TYPE) {
49        content_type
50    } else {
51        return false;
52    };
53
54    let content_type = if let Ok(content_type) = content_type.to_str() {
55        content_type
56    } else {
57        return false;
58    };
59
60    let mime = if let Ok(mime) = content_type.parse::<mime::Mime>() {
61        mime
62    } else {
63        return false;
64    };
65
66    <Mime as PartialEq<&str>>::eq(&mime, &expected_content_type)
67}