Crate neo_mime[−][src]
MediaType and MediaRange
The mime
crate defines two major types for representing MIMEs in HTTP
contexts:
- A
MediaType
is a concrete description of some content, such astext/plain
. - A
MediaRange
is 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 = neo_mime::TEXT_PLAIN; let svg = neo_mime::IMAGE_SVG; let json = neo_mime::APPLICATION_JSON; // etc
A MediaType
can also be parsed from a string, such as from
a Content-Type
HTTP header:
match neo_mime::MediaType::parse("text/plain; charset=utf-8") { Ok(text) => assert_eq!(text, neo_mime::TEXT_PLAIN_UTF_8), Err(err) => panic!("you should handle this parse error: {}", err), }
Inspecting MediaType
s
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 = neo_mime::TEXT_PLAIN; match (mime.type_(), mime.subtype()) { (neo_mime::TEXT, neo_mime::PLAIN) => println!("plain text!"), (neo_mime::TEXT, _) => println!("structured text"), _ => println!("not text"), }
Using Media Ranges for matching
MediaRange
s 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 MediaRange
s, and then used to check if any of
the MediaType
s you have would satisfy them.
match neo_mime::MediaRange::parse("text/*") { Ok(range) => { // There's a couple constants in case you don't need parsing... assert_eq!(range, neo_mime::TEXT_STAR); // "text/plain" is a match assert!(range.matches(&neo_mime::TEXT_PLAIN)); // "application/json" is NOT assert!(!range.matches(&neo_mime::APPLICATION_JSON)); }, Err(err) => panic!("that's a bad range: {}", err), }
Structs
InvalidMime | An error type representing an invalid |
MediaRange | A parsed media range used to match media types. |
MediaType | A parsed media type (or “MIME”). |
Value | A parameter value section of a |
Constants
APPLICATION | The string literal |
APPLICATION_DNS | A |
APPLICATION_JAVASCRIPT | A |
APPLICATION_JAVASCRIPT_UTF_8 | A |
APPLICATION_JSON | A |
APPLICATION_MSGPACK | A |
APPLICATION_OCTET_STREAM | A |
APPLICATION_PDF | A |
APPLICATION_WWW_FORM_URLENCODED | A |
AUDIO | The string literal |
AUDIO_STAR | A |
BASIC | The string literal |
BMP | The string literal |
BOUNDARY | The string literal |
CHARSET | The string literal |
CSS | The string literal |
CSV | The string literal |
EVENT_STREAM | The string literal |
FONT | The string literal |
FONT_WOFF | A |
FONT_WOFF2 | A |
FORM_DATA | The string literal |
GIF | The string literal |
HTML | The string literal |
IMAGE | The string literal |
IMAGE_BMP | A |
IMAGE_GIF | A |
IMAGE_JPEG | A |
IMAGE_PNG | A |
IMAGE_STAR | A |
IMAGE_SVG | A |
JAVASCRIPT | The string literal |
JPEG | The string literal |
JSON | The string literal |
MESSAGE | The string literal |
MODEL | The string literal |
MP4 | The string literal |
MPEG | The string literal |
MSGPACK | The string literal |
MULTIPART | The string literal |
OCTET_STREAM | The string literal |
OGG | The string literal |
The string literal | |
PLAIN | The string literal |
PNG | The string literal |
STAR | The string literal |
STAR_STAR | A |
SVG | The string literal |
TAB_SEPARATED_VALUES | The string literal |
TEXT | The string literal |
TEXT_CSS | A |
TEXT_CSS_UTF_8 | A |
TEXT_CSV | A |
TEXT_CSV_UTF_8 | A |
TEXT_EVENT_STREAM | A |
TEXT_HTML | A |
TEXT_HTML_UTF_8 | A |
TEXT_JAVASCRIPT | A |
TEXT_PLAIN | A |
TEXT_PLAIN_UTF_8 | A |
TEXT_STAR | A |
TEXT_TAB_SEPARATED_VALUES | A |
TEXT_TAB_SEPARATED_VALUES_UTF_8 | A |
TEXT_VCARD | A |
TEXT_XML | A |
UTF_8 | a |
VCARD | The string literal |
VIDEO | The string literal |
VIDEO_STAR | A |
WOFF | The string literal |
WOFF2 | The string literal |
WWW_FORM_URLENCODED | The string literal |
XML | The string literal |