Expand description
§MediaType and MediaRange
The mime crate defines two major types for representing MIMEs in HTTP
contexts:
- A
MediaTypeis a concrete description of some content, such astext/plain. - A
MediaRangeis a range of types that an agent is willing to receive, such astext/*.
§Getting a MediaType
There are several constants exported for common media types:
let text = mime::TEXT_PLAIN;
let svg = mime::IMAGE_SVG;
let json = mime::APPLICATION_JSON;
// etcA MediaType can also be parsed from a string, such as from
a Content-Type HTTP header:
match mime::MediaType::parse("text/plain; charset=utf-8") {
Ok(text) => assert_eq!(text, mime::TEXT_PLAIN_UTF_8),
Err(err) => panic!("you should handle this parse error: {}", err),
}§Inspecting MediaTypes
Once you have a MediaType, you can inspect the various parts of it.
Since the type_() and subtype() methods return &str, you can make
easy-to-read match statements to handle different media types. To prevent
typos, many common type names are available as constants.
let mime = mime::TEXT_PLAIN;
match (mime.type_(), mime.subtype()) {
(mime::TEXT, mime::PLAIN) => println!("plain text!"),
(mime::TEXT, _) => println!("structured text"),
_ => println!("not text"),
}§Using Media Ranges for matching
MediaRanges are often used by agents to declare a “range”
of media types that they can understand. A common place to find these is
Accept HTTP header, perhaps like this:
GET /index.html HTTP/1.1
Accept: text/html, text/*These can be parsed as MediaRanges, and then used to check if any of
the MediaTypes you have would satisfy them.
match mime::MediaRange::parse("text/*") {
Ok(range) => {
// There's a couple constants in case you don't need parsing...
assert_eq!(range, mime::TEXT_STAR);
// "text/plain" is a match
assert!(range.matches(&mime::TEXT_PLAIN));
// "application/json" is NOT
assert!(!range.matches(&mime::APPLICATION_JSON));
},
Err(err) => panic!("that's a bad range: {}", err),
}Structs§
- Invalid
Mime - An error type representing an invalid
MediaTypeorMediaRange. - Media
Range - A parsed media range used to match media types.
- Media
Type - A parsed media type (or “MIME”).
- Value
- A parameter value section of a
MediaTypeorMediaRange.
Constants§
- APPLICATION
- The string literal
"application". - APPLICATION_
DNS - A
MediaTyperepresenting"application/dns-message". - APPLICATION_
JAVASCRIPT - A
MediaTyperepresenting"application/javascript". - APPLICATION_
JAVASCRIPT_ UTF_ 8 - A
MediaTyperepresenting"application/javascript; charset=utf-8". - APPLICATION_
JSON - A
MediaTyperepresenting"application/json". - APPLICATION_
MSGPACK - A
MediaTyperepresenting"application/msgpack". - APPLICATION_
OCTET_ STREAM - A
MediaTyperepresenting"application/octet-stream". - APPLICATION_
PDF - A
MediaTyperepresenting"application/pdf". - APPLICATION_
WWW_ FORM_ URLENCODED - A
MediaTyperepresenting"application/x-www-form-urlencoded". - AUDIO
- The string literal
"audio". - AUDIO_
STAR - A
MediaRangerepresenting"audio/*". - BASIC
- The string literal
"basic". - BMP
- The string literal
"bmp". - BOUNDARY
- The string literal
"boundary". - CHARSET
- The string literal
"charset". - CSS
- The string literal
"css". - CSV
- The string literal
"csv". - EVENT_
STREAM - The string literal
"event-stream". - FONT
- The string literal
"font". - FONT_
WOFF - A
MediaTyperepresenting"font/woff". - FONT_
WOFF2 - A
MediaTyperepresenting"font/woff2". - FORM_
DATA - The string literal
"form-data". - GIF
- The string literal
"gif". - HTML
- The string literal
"html". - IMAGE
- The string literal
"image". - IMAGE_
BMP - A
MediaTyperepresenting"image/bmp". - IMAGE_
GIF - A
MediaTyperepresenting"image/gif". - IMAGE_
JPEG - A
MediaTyperepresenting"image/jpeg". - IMAGE_
PNG - A
MediaTyperepresenting"image/png". - IMAGE_
STAR - A
MediaRangerepresenting"image/*". - IMAGE_
SVG - A
MediaTyperepresenting"image/svg+xml". - JAVASCRIPT
- The string literal
"javascript". - JPEG
- The string literal
"jpeg". - JSON
- The string literal
"json". - MESSAGE
- The string literal
"message". - MODEL
- The string literal
"model". - MP4
- The string literal
"mp4". - MPEG
- The string literal
"mpeg". - MSGPACK
- The string literal
"msgpack". - MULTIPART
- The string literal
"multipart". - OCTET_
STREAM - The string literal
"octet-stream". - OGG
- The string literal
"ogg". - The string literal
"pdf". - PLAIN
- The string literal
"plain". - PNG
- The string literal
"png". - STAR
- The string literal
"*". - STAR_
STAR - A
MediaRangerepresenting"*/*". - SVG
- The string literal
"svg+xml". - TAB_
SEPARATED_ VALUES - The string literal
"tab-separated-values". - TEXT
- The string literal
"text". - TEXT_
CSS - A
MediaTyperepresenting"text/css". - TEXT_
CSS_ UTF_ 8 - A
MediaTyperepresenting"text/css; charset=utf-8". - TEXT_
CSV - A
MediaTyperepresenting"text/csv". - TEXT_
CSV_ UTF_ 8 - A
MediaTyperepresenting"text/csv; charset=utf-8". - TEXT_
EVENT_ STREAM - A
MediaTyperepresenting"text/event-stream". - TEXT_
HTML - A
MediaTyperepresenting"text/html". - TEXT_
HTML_ UTF_ 8 - A
MediaTyperepresenting"text/html; charset=utf-8". - TEXT_
JAVASCRIPT - A
MediaTyperepresenting"text/javascript". - TEXT_
PLAIN - A
MediaTyperepresenting"text/plain". - TEXT_
PLAIN_ UTF_ 8 - A
MediaTyperepresenting"text/plain; charset=utf-8". - TEXT_
STAR - A
MediaRangerepresenting"text/*". - TEXT_
TAB_ SEPARATED_ VALUES - A
MediaTyperepresenting"text/tab-separated-values". - TEXT_
TAB_ SEPARATED_ VALUES_ UTF_ 8 - A
MediaTyperepresenting"text/tab-separated-values; charset=utf-8". - TEXT_
VCARD - A
MediaTyperepresenting"text/vcard". - TEXT_
XML - A
MediaTyperepresenting"text/xml". - UTF_8
- a
Valueusable for a charset parameter. - VCARD
- The string literal
"vcard". - VIDEO
- The string literal
"video". - VIDEO_
STAR - A
MediaRangerepresenting"video/*". - WOFF
- The string literal
"woff". - WOFF2
- The string literal
"woff2". - WWW_
FORM_ URLENCODED - The string literal
"x-www-form-urlencoded". - XML
- The string literal
"xml".