2 releases

0.1.1 Apr 27, 2021
0.1.0 Apr 27, 2021

#901 in Text processing

Download history 348/week @ 2024-07-20 710/week @ 2024-07-27 412/week @ 2024-08-03 246/week @ 2024-08-10 317/week @ 2024-08-17 253/week @ 2024-08-24 403/week @ 2024-08-31 656/week @ 2024-09-07 549/week @ 2024-09-14 684/week @ 2024-09-21 615/week @ 2024-09-28 840/week @ 2024-10-05 1321/week @ 2024-10-12 1105/week @ 2024-10-19 1254/week @ 2024-10-26 1301/week @ 2024-11-02

5,185 downloads per month
Used in utube

MIT license

98KB
2.5K SLoC

Based on the apparently unmaintained mime crate

mime

Build Status crates.io docs.rs

Support MIME (HTTP Media Types) as strong types in Rust.

Documentation

Usage

extern crate mime;

fn main() {
    // common types are constants
    let text = mime::TEXT_PLAIN;

    // deconstruct Mimes to match on them
    match (text.type_(), text.subtype()) {
        (mime::TEXT, mime::PLAIN) => {
            // plain text!
        },
        (mime::TEXT, _) => {
            // structured text!
        },
        _ => {
            // not text!
        }
    }
}

lib.rs:

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 = 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 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 = 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

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 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),
}

Dependencies

~95–385KB