| 1 | <div align="center"> |
| 2 | |
| 3 | # MediaType |
| 4 | |
| 5 | MIME Media-type parsing for Rust |
| 6 | F |
| 7 | [](https://crates.io/crates/mediatype) |
| 8 | [](https://deps.rs/crate/mediatype/0.23.0) |
| 9 | [](https://github.com/picoHz/mediatype/blob/main/LICENSE) |
| 10 | [](https://docs.rs/mediatype) |
| 11 |  |
| 12 | |
| 13 | </div> |
| 14 | |
| 15 | - [Parsing](#parsing) |
| 16 | - [Construction](#construction) |
| 17 | - [Parameters](#parameters) |
| 18 | - [Case Sensitivity](#case-sensitivity) |
| 19 | - [Duplicate Parameter Names](#duplicate-parameter-names) |
| 20 | - [Owned Type](#owned-type) |
| 21 | - [MediaTypeList](#mediatypelist) |
| 22 | - [Serialize and Deserialize](serialize-and-deserialize) |
| 23 | |
| 24 | ## Parsing |
| 25 | |
| 26 | `MediaType::parse` runs a zero-copy parsing: A `MediaType` borrows the input |
| 27 | string instead of copying it. |
| 28 | |
| 29 | If you need an owned type, use [`MediaTypeBuf`](#owned-type). |
| 30 | |
| 31 | ```rust |
| 32 | use mediatype::{names::*, values::*, MediaType}; |
| 33 | |
| 34 | let madia_type = "image/svg+xml; charset=UTF-8"; |
| 35 | let svg = MediaType::parse(madia_type).unwrap(); |
| 36 | |
| 37 | assert_eq!(svg.ty, IMAGE); |
| 38 | assert_eq!(svg.subty, SVG); |
| 39 | assert_eq!(svg.suffix, Some(XML)); |
| 40 | assert_eq!(svg.get_param(CHARSET), Some(UTF_8)); |
| 41 | ``` |
| 42 | |
| 43 | ## Construction |
| 44 | |
| 45 | `MediaType` is const-constructible. It can be defined as a constant. |
| 46 | |
| 47 | Predefind names and values are defined in |
| 48 | [`names`](https://docs.rs/mediatype/latest/mediatype/names/index.html) and |
| 49 | [`values`](https://docs.rs/mediatype/latest/mediatype/values/index.html) |
| 50 | modules. |
| 51 | |
| 52 | ```rust |
| 53 | use mediatype::{names::*, values::*, media_type, MediaType}; |
| 54 | |
| 55 | const TEXT_PLAIN: MediaType = MediaType::new(TEXT, PLAIN); |
| 56 | |
| 57 | const IMAGE_SVG: MediaType = |
| 58 | MediaType::from_parts(TEXT, PLAIN, Some(XML), &[(CHARSET, UTF_8)]); |
| 59 | |
| 60 | const TEXT_MARKDOWN: MediaType = |
| 61 | media_type!(TEXT/MARKDOWN; CHARSET=UTF_8); |
| 62 | ``` |
| 63 | |
| 64 | ## Parameters |
| 65 | |
| 66 | ### Case Sensitivity |
| 67 | |
| 68 | Comparisons are case-insensitive except parameter values. |
| 69 | |
| 70 | ```rust |
| 71 | let text_plain_lower = MediaType::parse("text/plain; charset=UTF-8").unwrap(); |
| 72 | let text_plain_upper = MediaType::parse("TEXT/PLAIN; CHARSET=UTF-8").unwrap(); |
| 73 | |
| 74 | assert_eq!(text_plain_lower, text_plain_upper); |
| 75 | assert_eq!(text_plain_lower.ty(), "Text"); |
| 76 | assert_eq!(text_plain_upper.subty(), "Plain"); |
| 77 | assert!(text_plain_lower != |
| 78 | MediaType::parse("text/plain; charset=utf-8").unwrap()); |
| 79 | ``` |
| 80 | |
| 81 | ### Duplicate Parameter Names |
| 82 | |
| 83 | The parser does not report duplicate parameter names as an error, but |
| 84 | `MediaType` recognizes only the last value. |
| 85 | |
| 86 | ```rust |
| 87 | let text_plain = MediaType::parse( |
| 88 | "text/plain; charset=US-ASCII; charset=UTF-8").unwrap(); |
| 89 | |
| 90 | assert_eq!( |
| 91 | text_plain.to_string(), |
| 92 | "text/plain; charset=US-ASCII; charset=UTF-8" |
| 93 | ); |
| 94 | |
| 95 | // Return the last charset value. |
| 96 | assert_eq!(text_plain.get_param(CHARSET), Some(UTF_8)); |
| 97 | |
| 98 | // Compare the last charset value. |
| 99 | assert_eq!( |
| 100 | text_plain, |
| 101 | MediaType::parse("text/plain; charset=UTF-8").unwrap() |
| 102 | ); |
| 103 | ``` |
| 104 | |
| 105 | ## Owned Type |
| 106 | |
| 107 | [`MediaTypeBuf`](https://docs.rs/mediatype/latest/mediatype/struct.MediaTypeBuf.html) |
| 108 | is an owned version of `MediaType`. It is immutable but optimized for minimal |
| 109 | stack and heap usage. |
| 110 | |
| 111 | ```rust |
| 112 | use mediatype::{names::*, values::*, MediaType, MediaTypeBuf}; |
| 113 | |
| 114 | let text_plain: MediaTypeBuf = "text/plain; charset=UTF-8".parse().unwrap(); |
| 115 | assert_eq!(text_plain.get_param(CHARSET).unwrap(), UTF_8); |
| 116 | |
| 117 | // Convert to MediaType |
| 118 | let mut text_markdown: MediaType = text_plain.to_ref(); |
| 119 | text_markdown.subty = MARKDOWN; |
| 120 | assert_eq!(text_markdown.to_string(), "text/markdown; charset=UTF-8"); |
| 121 | ``` |
| 122 | |
| 123 | ## MediaTypeList |
| 124 | |
| 125 | [`MediaTypeList`](https://docs.rs/mediatype/latest/mediatype/struct.MediaTypeList.html) |
| 126 | parses a comma-separated list of `MediaType`s used in the HTTP `Accept` header. |
| 127 | ([RFC 7231](https://www.rfc-editor.org/rfc/rfc7231#section-5.3.2)) |
| 128 | |
| 129 | ```rust |
| 130 | use mediatype::{MediaType, MediaTypeList}; |
| 131 | |
| 132 | let mut list = MediaTypeList::new( |
| 133 | "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8", |
| 134 | ); |
| 135 | assert_eq!(list.next(), Some(MediaType::parse("text/html"))); |
| 136 | assert_eq!(list.next(), Some(MediaType::parse("application/xhtml+xml"))); |
| 137 | assert_eq!(list.next(), Some(MediaType::parse("application/xml;q=0.9"))); |
| 138 | assert_eq!(list.next(), Some(MediaType::parse("*/*;q=0.8"))); |
| 139 | assert_eq!(list.next(), None); |
| 140 | ``` |
| 141 | |
| 142 | ## Serialize and Deserialize |
| 143 | |
| 144 | To enable serialization and deserialization, specify `serde` feature in |
| 145 | `Cargo.toml`. |
| 146 | |
| 147 | ```toml |
| 148 | mediatype = { version = "...", features = ["serde"] } |
| 149 | ``` |
| 150 | |
| 151 | ```rust |
| 152 | let json = r#" |
| 153 | [ |
| 154 | "text/plain", |
| 155 | "image/svg+xml; charset=UTF-8" |
| 156 | ] |
| 157 | "#; |
| 158 | |
| 159 | let decoded: Vec<MediaType> = serde_json::from_str(json).unwrap(); |
| 160 | ``` |