Debian Codegraph @

src/lib.rs

raw symbols SCIP
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 #![no_std]
42 #![forbid(unsafe_code)]
43 #![forbid(clippy::all)]
44 #![cfg_attr(docsrs, feature(doc_cfg))]
45
46 extern crate alloc;
47
48 #[cfg(test)]
49 extern crate std;
50
51 mod consts;
52 mod error;
53 mod media_type;
54 mod media_type_buf;
55 mod media_type_list;
56 mod name;
57 mod params;
58 mod parse;
59 mod serde;
60 mod value;
61
62 pub use consts::*;
63 pub use error::*;
64 pub use media_type::*;
65 pub use media_type_buf::*;
66 pub use media_type_list::*;
67 pub use name::*;
68 pub use params::*;
69 pub use value::*;
70
71 /// Convenient macro to construct a [`MediaType`].
72 ///
73 /// [`MadiaType`]: ./struct.MediaType.html
74 ///
75 ///
76 /// ```
77 /// # use mediatype::media_type;
78 /// assert_eq!(media_type!(TEXT/PLAIN).to_string(), "text/plain");
79 /// assert_eq!(media_type!(IMAGE/x_::ICON).to_string(), "image/x-icon");
80 ///
81 /// assert_eq!(
82 /// media_type!(IMAGE/SVG+XML; CHARSET=UTF_8).to_string(),
83 /// "image/svg+xml; charset=UTF-8"
84 /// );
85 /// assert_eq!(
86 /// media_type!(APPLICATION/vnd::OPENSTREETMAP_DATA+XML).to_string(),
87 /// "application/vnd.openstreetmap.data+xml"
88 /// );
89 /// ```
90 #[macro_export]
91 macro_rules! media_type {
92 ($ty:ident / $prefix:ident $(:: $subty:ident)* $(;$name:ident = $value:ident)*) => {
93 const {
94 $crate::MediaType::from_parts(
95 $crate::names::$ty,
96 $crate::names::$prefix $(::$subty)*,
97 None,
98 &[$(($crate::names::$name, $crate::values::$value)),*],
99 )
100 }
101 };
102 ($ty:ident / $prefix:ident $(:: $subty:ident)* + $suffix:ident $(;$name:ident = $value:ident)*) => {
103 const {
104 $crate::MediaType::from_parts(
105 $crate::names::$ty,
106 $crate::names::$prefix $(::$subty)*,
107 Some($crate::names::$suffix),
108 &[$(($crate::names::$name, $crate::values::$value)),*],
109 )
110 }
111 };
112 }