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
| Feature | Default | Description |
|---|---|---|
std | yes | Enables std-only conveniences such as from_path and MediaTypeGuess::from_path. Implies alloc. |
alloc | yes (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-lookup | yes | Generates 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:
- without
alloc: only the constants,MediaTypeviews, and the extension lookup are available; - with
alloc: parsing, formatting,MediaTypeIter, andMediaTypeGuess::firstbecome available; - with
std:from_pathandMediaTypeGuess::from_pathare also available.
Structs§
- From
StrError - An error returned when parsing a
MediaTypefrom a string. - Iter
- An iterator over the
MediaTypevalues of aMediaTypeGuess. - IterRaw
- An iterator over the raw media type strings of a
MediaTypeGuess. - Media
Type - A parsed media type (formerly known as a MIME type).
- Media
Type Guess - A guess of the media type(s) of a file extension or path, as one or more
MediaTypevalues. - Media
Type Iter - An iterator over the media types found in a string.
- Parameters
- An iterator over the parameters of a
MediaType.
Constants§
- APPLICATION
- The
applicationtop-level type.application - APPLICATION_
BROTLI application/brotliapplication/brotli- APPLICATION_
GZIP application/gzipapplication/gzip- APPLICATION_
JAVASCRIPT application/javascriptapplication/javascript- APPLICATION_
JAVASCRIPT_ UTF_ 8 application/javascript; charset=utf-8application/javascript; charset=utf-8- APPLICATION_
JSON application/jsonapplication/json- APPLICATION_
MSGPACK application/msgpackapplication/msgpack- APPLICATION_
OCTET_ STREAM application/octet-streamapplication/octet-stream- APPLICATION_
PDF application/pdfapplication/pdf- APPLICATION_
PROTOBUF application/protobufapplication/protobuf- APPLICATION_
WASM application/wasmapplication/wasm- APPLICATION_
WWW_ FORM_ URLENCODED application/x-www-form-urlencodedapplication/x-www-form-urlencoded- APPLICATION_
X_ 7Z_ COMPRESSED application/x-7z-compressedapplication/x-7z-compressed- APPLICATION_
X_ TAR application/x-tarapplication/x-tar- APPLICATION_
YAML application/yamlapplication/yaml- APPLICATION_
ZIP application/zipapplication/zip- APPLICATION_
ZSTD application/zstdapplication/zstd- AUDIO
- The
audiotop-level type.audio - AUDIO_
FLAC audio/flacaudio/flac- AUDIO_
OPUS audio/opusaudio/opus- AVIF
- The
avifsubtype.avif - BMP
- The
bmpsubtype.bmp - BOUNDARY
- The
boundaryparameter name.boundary - BROTLI
- The
brotlisubtype.brotli - CHARSET
- The
charsetparameter name.charset - CSS
- The
csssubtype.css - CSV
- The
csvsubtype.csv - EVENT_
STREAM - The
event-streamsubtype.event-stream - FLAC
- The
flacsubtype.flac - FONT
- The
fonttop-level type.font - FONT_
OTF font/otffont/otf- FONT_
TTF font/ttffont/ttf- FONT_
WOFF font/wofffont/woff- FONT_
WOFF2 font/woff2font/woff2- FORM_
DATA - The
form-datasubtype.form-data - GIF
- The
gifsubtype.gif - GZIP
- The
gzipsubtype.gzip - HTML
- The
htmlsubtype.html - IMAGE
- The
imagetop-level type.image - IMAGE_
AVIF image/avifimage/avif- IMAGE_
BMP image/bmpimage/bmp- IMAGE_
GIF image/gifimage/gif- IMAGE_
JPEG image/jpegimage/jpeg- IMAGE_
PNG image/pngimage/png- IMAGE_
STAR image/*image/*- IMAGE_
SVG image/svg+xmlimage/svg+xml- IMAGE_
WEBP image/webpimage/webp- JAVASCRIPT
- The
javascriptsubtype.javascript - JPEG
- The
jpegsubtype.jpeg - JSON
- The
jsonsubtype.json - MARKDOWN
- The
markdownsubtype.markdown - MESSAGE
- The
messagetop-level type.message - MODEL
- The
modeltop-level type.model - MP4
- The
mp4subtype.mp4 - MPEG
- The
mpegsubtype.mpeg - MSGPACK
- The
msgpacksubtype.msgpack - MULTIPART
- The
multiparttop-level type.multipart - MULTIPART_
FORM_ DATA multipart/form-datamultipart/form-data- OCTET_
STREAM - The
octet-streamsubtype.octet-stream - OGG
- The
oggsubtype.ogg - OPUS
- The
opussubtype.opus - OTF
- The
otfsubtype.otf - The
pdfsubtype.pdf - PLAIN
- The
plainsubtype.plain - PNG
- The
pngsubtype.png - PROTOBUF
- The
protobufsubtype.protobuf - STAR
- The
*wildcard.* - STAR_
STAR */**/*- SVG
- The
svgsubtype.svg - TEXT
- The
texttop-level type.text - TEXT_
CSS text/csstext/css- TEXT_
CSS_ UTF_ 8 text/css; charset=utf-8text/css; charset=utf-8- TEXT_
CSV text/csvtext/csv- TEXT_
CSV_ UTF_ 8 text/csv; charset=utf-8text/csv; charset=utf-8- TEXT_
EVENT_ STREAM text/event-streamtext/event-stream- TEXT_
HTML text/htmltext/html- TEXT_
HTML_ UTF_ 8 text/html; charset=utf-8text/html; charset=utf-8- TEXT_
JAVASCRIPT text/javascripttext/javascript- TEXT_
MARKDOWN text/markdowntext/markdown- TEXT_
PLAIN text/plaintext/plain- TEXT_
PLAIN_ UTF_ 8 text/plain; charset=utf-8text/plain; charset=utf-8- TEXT_
STAR text/*text/*- TEXT_
TAB_ SEPARATED_ VALUES text/tab-separated-valuestext/tab-separated-values- TEXT_
TAB_ SEPARATED_ VALUES_ UTF_ 8 text/tab-separated-values; charset=utf-8text/tab-separated-values; charset=utf-8- TEXT_
VCARD text/vcardtext/vcard- TEXT_
XML text/xmltext/xml- TTF
- The
ttfsubtype.ttf - UTF_8
- The
utf-8charset value.utf-8 - VCARD
- The
vcardsubtype.vcard - VIDEO
- The
videotop-level type.video - VIDEO_
MP4 video/mp4video/mp4- VIDEO_
WEBM video/webmvideo/webm- WASM
- The
wasmsubtype.wasm - WEBM
- The
webmsubtype.webm - WEBP
- The
webpsubtype.webp - WOFF
- The
woffsubtype.woff - WOFF2
- The
woff2subtype.woff2 - WWW_
FORM_ URLENCODED - The
x-www-form-urlencodedsubtype.x-www-form-urlencoded - XML
- The
xmlsubtype or+xmlsuffix.xml - X_
7Z_ COMPRESSED - The
x-7z-compressedsubtype.x-7z-compressed - X_TAR
- The
x-tarsubtype.x-tar - YAML
- The
yamlsubtype.yaml - ZIP
- The
zipsubtype.zip - ZSTD
- The
zstdsubtype.zstd
Functions§
- from_
ext - Guess the media type(s) of a file with the given extension.
- from_
path - Guess the media type(s) of
pathby 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.