[][src]Crate mime_4

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 as text/plain.
  • A MediaRange is a range of types that an agent is willing to receive, such as text/*.

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;
// etc

A 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

InvalidMime

An error type representing an invalid MediaType or MediaRange.

MediaRange

A parsed media range used to match media types.

MediaType

A parsed media type (or "MIME").

Value

A parameter value section of a MediaType or MediaRange.

Constants

APPLICATION

The string literal "application".

APPLICATION_DNS

A MediaType representing "application/dns-message".

APPLICATION_JAVASCRIPT

A MediaType representing "application/javascript".

APPLICATION_JAVASCRIPT_UTF_8

A MediaType representing "application/javascript; charset=utf-8".

APPLICATION_JSON

A MediaType representing "application/json".

APPLICATION_MSGPACK

A MediaType representing "application/msgpack".

APPLICATION_OCTET_STREAM

A MediaType representing "application/octet-stream".

APPLICATION_PDF

A MediaType representing "application/pdf".

APPLICATION_WWW_FORM_URLENCODED

A MediaType representing "application/x-www-form-urlencoded".

AUDIO

The string literal "audio".

AUDIO_STAR

A MediaRange representing "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 MediaType representing "font/woff".

FONT_WOFF2

A MediaType representing "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 MediaType representing "image/bmp".

IMAGE_GIF

A MediaType representing "image/gif".

IMAGE_JPEG

A MediaType representing "image/jpeg".

IMAGE_PNG

A MediaType representing "image/png".

IMAGE_STAR

A MediaRange representing "image/*".

IMAGE_SVG

A MediaType representing "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".

PDF

The string literal "pdf".

PLAIN

The string literal "plain".

PNG

The string literal "png".

STAR

The string literal "*".

STAR_STAR

A MediaRange representing "*/*".

SVG

The string literal "svg+xml".

TAB_SEPARATED_VALUES

The string literal "tab-separated-values".

TEXT

The string literal "text".

TEXT_CSS

A MediaType representing "text/css".

TEXT_CSS_UTF_8

A MediaType representing "text/css; charset=utf-8".

TEXT_CSV

A MediaType representing "text/csv".

TEXT_CSV_UTF_8

A MediaType representing "text/csv; charset=utf-8".

TEXT_EVENT_STREAM

A MediaType representing "text/event-stream".

TEXT_HTML

A MediaType representing "text/html".

TEXT_HTML_UTF_8

A MediaType representing "text/html; charset=utf-8".

TEXT_JAVASCRIPT

A MediaType representing "text/javascript".

TEXT_PLAIN

A MediaType representing "text/plain".

TEXT_PLAIN_UTF_8

A MediaType representing "text/plain; charset=utf-8".

TEXT_STAR

A MediaRange representing "text/*".

TEXT_TAB_SEPARATED_VALUES

A MediaType representing "text/tab-separated-values".

TEXT_TAB_SEPARATED_VALUES_UTF_8

A MediaType representing "text/tab-separated-values; charset=utf-8".

TEXT_VCARD

A MediaType representing "text/vcard".

TEXT_XML

A MediaType representing "text/xml".

UTF_8

a Value usable for a charset parameter.

VCARD

The string literal "vcard".

VIDEO

The string literal "video".

VIDEO_STAR

A MediaRange representing "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".