mediatype/
lib.rs

1//! This crate provides two media type structs: [`MediaType`] and [`MediaTypeBuf`].
2//!
3//! - [`MediaType`] does not copy data during parsing
4//!     and borrows the original string. It is also const-constructible.
5//! - [`MediaTypeBuf`] is an owned  and immutable version of [`MediaType`].
6//!
7//! [`MadiaType`]: ./struct.MediaType.html
8//! [`MediaTypeBuf`]: ./struct.MediaTypeBuf.html
9//!
10//!  ```
11//! use mediatype::{names::*, MediaType, MediaTypeBuf};
12//!
13//! const TEXT_PLAIN: MediaType = MediaType::new(TEXT, PLAIN);
14//! let text_plain: MediaTypeBuf = "text/plain".parse().unwrap();
15//!
16//! assert_eq!(text_plain, TEXT_PLAIN);
17//! ```
18//!
19//! # Letter case handling
20//!
21//! [`MediaType`] and [`MediaTypeBuf`] preserve the original string's letter case.
22//! Comparisons for [`MediaType`], [`MediaTypeBuf`] and [`Name`] are case-insensitive
23//! except parameter values.
24//!
25//! [`MadiaType`]: ./struct.MediaType.html
26//! [`MediaTypeBuf`]: ./struct.MediaTypeBuf.html
27//! [`Name`]: ./struct.Name.html
28//!
29//!  ```
30//! # use mediatype::{names::*, MediaType, MediaTypeBuf};
31//! let lower: MediaTypeBuf = "text/plain; charset=UTF-8".parse().unwrap();
32//! let upper: MediaTypeBuf = "TEXT/PLAIN; CHARSET=UTF-8".parse().unwrap();
33//!
34//! assert_eq!(lower.as_str(), "text/plain; charset=UTF-8");
35//! assert_eq!(upper.as_str(), "TEXT/PLAIN; CHARSET=UTF-8");
36//! assert_eq!(lower, upper);
37//! assert_eq!(lower.ty(), "Text");
38//! assert_eq!(upper.subty(), "Plain");
39//! ```
40
41#![forbid(unsafe_code)]
42#![forbid(clippy::all)]
43#![cfg_attr(docsrs, feature(doc_cfg))]
44
45mod consts;
46mod error;
47mod media_type;
48mod media_type_buf;
49mod media_type_list;
50mod name;
51mod params;
52mod parse;
53mod serde;
54mod value;
55
56pub use consts::*;
57pub use error::*;
58pub use media_type::*;
59pub use media_type_buf::*;
60pub use media_type_list::*;
61pub use name::*;
62pub use params::*;
63pub use value::*;
64
65/// Convenient macro to construct a [`MediaType`].
66///
67/// [`MadiaType`]: ./struct.MediaType.html
68///
69///
70/// ```
71/// # use mediatype::media_type;
72/// assert_eq!(media_type!(TEXT/PLAIN).to_string(), "text/plain");
73/// assert_eq!(media_type!(IMAGE/x_::ICON).to_string(), "image/x-icon");
74///
75/// assert_eq!(
76///     media_type!(IMAGE/SVG+XML; CHARSET=UTF_8).to_string(),
77///     "image/svg+xml; charset=UTF-8"
78/// );
79/// assert_eq!(
80///     media_type!(APPLICATION/vnd::OPENSTREETMAP_DATA+XML).to_string(),
81///     "application/vnd.openstreetmap.data+xml"
82/// );
83/// ```
84#[macro_export]
85macro_rules! media_type {
86    ($ty:ident / $prefix:ident $(:: $subty:ident)* $(;$name:ident = $value:ident)*) => {
87        $crate::MediaType::from_parts(
88            $crate::names::$ty,
89            $crate::names::$prefix $(::$subty)*,
90            None,
91            &[$(($crate::names::$name, $crate::values::$value)),*],
92        )
93    };
94    ($ty:ident / $prefix:ident $(:: $subty:ident)* + $suffix:ident $(;$name:ident = $value:ident)*) => {
95        $crate::MediaType::from_parts(
96            $crate::names::$ty,
97            $crate::names::$prefix $(::$subty)*,
98            Some($crate::names::$suffix),
99            &[$(($crate::names::$name, $crate::values::$value)),*],
100        )
101    };
102}