# MediaType MIME Media-type parsing for Rust [![Crates.io](https://img.shields.io/crates/v/mediatype.svg)](https://crates.io/crates/mediatype) [![GitHub license](https://img.shields.io/github/license/picoHz/mediatype.svg)](https://github.com/picoHz/mediatype/blob/main/LICENSE) [![Rustdoc](https://img.shields.io/badge/doc-rustdoc-green.svg)](https://docs.rs/mediatype) ![Rust](https://github.com/picoHz/mediatype/workflows/Rust/badge.svg)

Parsing

MediaType::parse runs a zero-copy parsing: A MediaType borrows the input string instead of copying it.

If you need an owned type, use MediaTypeBuf.

```rust use mediatype::{names::, values::, MediaType};

let madiatype = "image/svg+xml; charset=UTF-8"; let svg = MediaType::parse(madiatype).unwrap();

asserteq!(svg.ty, IMAGE); asserteq!(svg.subty, SVG); asserteq!(svg.suffix, Some(XML)); asserteq!(svg.getparam(CHARSET), Some(UTF8)); ```

Construction

MediaType is const-construtible. It can be defained as a constant.

Predefind names and values are defined in names and values modules.

```rust use mediatype::{names::, values::, media_type, MediaType};

const TEXT_PLAIN: MediaType = MediaType::new(TEXT, PLAIN);

const IMAGESVG: MediaType = MediaType::fromparts(TEXT, PLAIN, Some(XML), &[(CHARSET, UTF_8)]);

const TEXTMARKDOWN: MediaType = mediatype!(TEXT/MARKDOWN; CHARSET=UTF_8); ```

Parameters

Case Sensitivity

Comparisons are case-insensitive except parameter values.

```rust let textplainlower = MediaType::parse("text/plain; charset=UTF-8").unwrap(); let textplainupper = MediaType::parse("TEXT/PLAIN; CHARSET=UTF-8").unwrap();

asserteq!(textplainlower, textplainupper); asserteq!(textplainlower.ty(), "Text"); asserteq!(textplainupper.subty(), "Plain"); assert!(textplain_lower != MediaType::parse("text/plain; charset=utf-8").unwrap()); ```

Duplicate Parameter Names

The parser does not report duplicate parameter names as an error, but MediaType recognizes only the last value.

```rust let text_plain = MediaType::parse( "text/plain; charset=US-ASCII; charset=UTF-8").unwrap();

asserteq!( textplain.to_string(), "text/plain; charset=US-ASCII; charset=UTF-8" );

// Return the last charset value. asserteq!(textplain.getparam(CHARSET), Some(UTF8));

// Compare the last charset value. asserteq!( textplain, MediaType::parse("text/plain; charset=UTF-8").unwrap() ); ```

Owned Type

MediaTypeBuf is an owned version of MediaType. It is immutable but optimized for minimal stack and heap usage.

```rust use mediatype::{names::, values::, MediaType, MediaTypeBuf};

let textplain: MediaTypeBuf = "text/plain; charset=UTF-8".parse().unwrap(); asserteq!(textplain.getparam(CHARSET).unwrap(), UTF_8);

// Convert to MediaType let mut textmarkdown: MediaType = textplain.toref(); textmarkdown.subty = MARKDOWN; asserteq!(textmarkdown.to_string(), "text/markdown; charset=UTF-8"); ```

Serialize and Deserialize

To enable serialization and deserialization, specify serde feature in Cargo.toml.

toml mediatype = { version = "...", features = ["serde"] }

```rust let json = r#" [ "text/plain", "image/svg+xml; charset=UTF-8" ] "#;

let decoded: Vec = serdejson::fromstr(json).unwrap(); ```