Skip to main content

Crate media_type

Crate media_type 

Source
Expand description

Media types (formerly known as MIME types).

This crate provides the MediaType type to parse and inspect media types such as text/plain; charset=utf-8 or image/svg+xml, a curated set of constants for the most common types, and helpers to guess a media type from a file extension or path.

§Parsing

use media_type::MediaType;

let media_type: MediaType = "text/plain; charset=utf-8".parse().unwrap();
assert_eq!(media_type, media_type::TEXT_PLAIN_UTF_8);
assert_eq!(media_type.type_(), "text");
assert_eq!(media_type.subtype(), "plain");
assert_eq!(media_type.get_parameter("charset"), Some("utf-8"));

§Guessing from a path or extension

The file does not have to exist: only its extension is inspected.

let guess = media_type::from_path("images/logo.png");
assert_eq!(guess.first(), Some(media_type::IMAGE_PNG));

let guess = media_type::from_ext("zst");
assert_eq!(guess.first(), Some(media_type::APPLICATION_ZSTD));

§Parsing a list of media types

HTTP header fields such as Accept or Content-Type may contain a comma-separated list of media types. MediaTypeIter extracts the valid ones and reports the fragments that could not be parsed:

use media_type::MediaTypeIter;

let header = "text/html, application/json, invalid, text/xml";
let parsed: Vec<_> = MediaTypeIter::new(header).collect();

assert_eq!(parsed.len(), 4);
assert_eq!(parsed[0].as_ref().unwrap(), &media_type::TEXT_HTML);
assert_eq!(parsed[1].as_ref().unwrap(), &media_type::APPLICATION_JSON);
assert_eq!(*parsed[2].as_ref().unwrap_err(), "invalid");
assert_eq!(parsed[3].as_ref().unwrap(), &media_type::TEXT_XML);

§Cargo features

FeatureDefaultDescription
stdyesEnables std-only conveniences such as from_path and MediaTypeGuess::from_path. Implies alloc.
allocyes (via std)Enables the owned, allocating API: parsing, FromStr, MediaTypeIter, MediaTypeGuess::first, formatting, … Without it, only the constants, the MediaType views, and the extension lookup are available.
reverse-lookupyesGenerates and exposes the media type -> extensions reverse lookup (get_extensions, get_media_extensions, get_media_extensions_str). Disable it to shrink the binary for size-constrained builds.

This crate is no_std by default. Disable the default features to use it without std:

Structs§

FromStrError
An error returned when parsing a MediaType from a string.
Iter
An iterator over the MediaType values of a MediaTypeGuess.
IterRaw
An iterator over the raw media type strings of a MediaTypeGuess.
MediaType
A parsed media type (formerly known as a MIME type).
MediaTypeGuess
A guess of the media type(s) of a file extension or path, as one or more MediaType values.
MediaTypeIter
An iterator over the media types found in a string.
Parameters
An iterator over the parameters of a MediaType.

Constants§

APPLICATION
The application top-level type. application
APPLICATION_BROTLI
application/brotli application/brotli
APPLICATION_GZIP
application/gzip application/gzip
APPLICATION_JAVASCRIPT
application/javascript application/javascript
APPLICATION_JAVASCRIPT_UTF_8
application/javascript; charset=utf-8 application/javascript; charset=utf-8
APPLICATION_JSON
application/json application/json
APPLICATION_MSGPACK
application/msgpack application/msgpack
APPLICATION_OCTET_STREAM
application/octet-stream application/octet-stream
APPLICATION_PDF
application/pdf application/pdf
APPLICATION_PROTOBUF
application/protobuf application/protobuf
APPLICATION_WASM
application/wasm application/wasm
APPLICATION_WWW_FORM_URLENCODED
application/x-www-form-urlencoded application/x-www-form-urlencoded
APPLICATION_X_7Z_COMPRESSED
application/x-7z-compressed application/x-7z-compressed
APPLICATION_X_TAR
application/x-tar application/x-tar
APPLICATION_YAML
application/yaml application/yaml
APPLICATION_ZIP
application/zip application/zip
APPLICATION_ZSTD
application/zstd application/zstd
AUDIO
The audio top-level type. audio
AUDIO_FLAC
audio/flac audio/flac
AUDIO_OPUS
audio/opus audio/opus
AVIF
The avif subtype. avif
BMP
The bmp subtype. bmp
BOUNDARY
The boundary parameter name. boundary
BROTLI
The brotli subtype. brotli
CHARSET
The charset parameter name. charset
CSS
The css subtype. css
CSV
The csv subtype. csv
EVENT_STREAM
The event-stream subtype. event-stream
FLAC
The flac subtype. flac
FONT
The font top-level type. font
FONT_OTF
font/otf font/otf
FONT_TTF
font/ttf font/ttf
FONT_WOFF
font/woff font/woff
FONT_WOFF2
font/woff2 font/woff2
FORM_DATA
The form-data subtype. form-data
GIF
The gif subtype. gif
GZIP
The gzip subtype. gzip
HTML
The html subtype. html
IMAGE
The image top-level type. image
IMAGE_AVIF
image/avif image/avif
IMAGE_BMP
image/bmp image/bmp
IMAGE_GIF
image/gif image/gif
IMAGE_JPEG
image/jpeg image/jpeg
IMAGE_PNG
image/png image/png
IMAGE_STAR
image/* image/*
IMAGE_SVG
image/svg+xml image/svg+xml
IMAGE_WEBP
image/webp image/webp
JAVASCRIPT
The javascript subtype. javascript
JPEG
The jpeg subtype. jpeg
JSON
The json subtype. json
MARKDOWN
The markdown subtype. markdown
MESSAGE
The message top-level type. message
MODEL
The model top-level type. model
MP4
The mp4 subtype. mp4
MPEG
The mpeg subtype. mpeg
MSGPACK
The msgpack subtype. msgpack
MULTIPART
The multipart top-level type. multipart
MULTIPART_FORM_DATA
multipart/form-data multipart/form-data
OCTET_STREAM
The octet-stream subtype. octet-stream
OGG
The ogg subtype. ogg
OPUS
The opus subtype. opus
OTF
The otf subtype. otf
PDF
The pdf subtype. pdf
PLAIN
The plain subtype. plain
PNG
The png subtype. png
PROTOBUF
The protobuf subtype. protobuf
STAR
The * wildcard. *
STAR_STAR
*/* */*
SVG
The svg subtype. svg
TEXT
The text top-level type. text
TEXT_CSS
text/css text/css
TEXT_CSS_UTF_8
text/css; charset=utf-8 text/css; charset=utf-8
TEXT_CSV
text/csv text/csv
TEXT_CSV_UTF_8
text/csv; charset=utf-8 text/csv; charset=utf-8
TEXT_EVENT_STREAM
text/event-stream text/event-stream
TEXT_HTML
text/html text/html
TEXT_HTML_UTF_8
text/html; charset=utf-8 text/html; charset=utf-8
TEXT_JAVASCRIPT
text/javascript text/javascript
TEXT_MARKDOWN
text/markdown text/markdown
TEXT_PLAIN
text/plain text/plain
TEXT_PLAIN_UTF_8
text/plain; charset=utf-8 text/plain; charset=utf-8
TEXT_STAR
text/* text/*
TEXT_TAB_SEPARATED_VALUES
text/tab-separated-values text/tab-separated-values
TEXT_TAB_SEPARATED_VALUES_UTF_8
text/tab-separated-values; charset=utf-8 text/tab-separated-values; charset=utf-8
TEXT_VCARD
text/vcard text/vcard
TEXT_XML
text/xml text/xml
TTF
The ttf subtype. ttf
UTF_8
The utf-8 charset value. utf-8
VCARD
The vcard subtype. vcard
VIDEO
The video top-level type. video
VIDEO_MP4
video/mp4 video/mp4
VIDEO_WEBM
video/webm video/webm
WASM
The wasm subtype. wasm
WEBM
The webm subtype. webm
WEBP
The webp subtype. webp
WOFF
The woff subtype. woff
WOFF2
The woff2 subtype. woff2
WWW_FORM_URLENCODED
The x-www-form-urlencoded subtype. x-www-form-urlencoded
XML
The xml subtype or +xml suffix. xml
X_7Z_COMPRESSED
The x-7z-compressed subtype. x-7z-compressed
X_TAR
The x-tar subtype. x-tar
YAML
The yaml subtype. yaml
ZIP
The zip subtype. zip
ZSTD
The zstd subtype. zstd

Functions§

from_ext
Guess the media type(s) of a file with the given extension.
from_path
Guess the media type(s) of path by its extension.
get_extensions
Get the extensions for a given top-level and sub-level of a media type ({toplevel}/{sublevel}).
get_media_extensions
Get a list of known extensions for a given MediaType.
get_media_extensions_str
Get a list of known extensions for a media type string.