# 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

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

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::, MediaType};

const TEXTPLAIN: MediaType = MediaType::new(TEXT, PLAIN); const IMAGESVG: MediaType = MediaType::fromparts(TEXT, PLAIN, Some(XML), &[(CHARSET, UTF8)]); ```

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 recognizes only the last value.

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

asserteq!(textplain.tostirng(), "text/plain; charset=US-ASCII; charset=UTF-8"); asserteq!(textplain.get(CHARSET), Some(UTF8)); asserteq!(textplain, MediaType::parse("text/plain; charset=UTF-8").unwrap()); ```

Quoted String

```rust let textplain = MediaType::parse("text/plain; message=\"Hello world!\"").unwrap(); let message = textplain.get_param(Name::new("message").unwrap()).unwrap();

asserteq!(message, "Hello world!"); asserteq!(message.asstr(), "\"Hello world!\""); asserteq!(message.unquoted_str(), "Hello world!"); ```

```rust let mut text_plain = MediaType::parse("text/plain").unwrap();

let quoted = Value::quote("Hello world!"); let value = Value::new(&quoted).unwrap(); textplain.setparam(Name::new("message").unwrap(), value);

asserteq!(textplain.to_string(), "text/plain; message=\"Hello world!\""); ```

Mutation

```rust let mut multipart = MediaType::new(MULTIPART, FORM_DATA);

let boundary = Value::new("dyEV84n7XNJ").unwrap(); multipart.setparam(BOUNDARY, boundary); asserteq!( multipart.to_string(), "multipart/form-data; boundary=dyEV84n7XNJ" );

multipart.subty = RELATED; asserteq!( multipart.tostring(), "multipart/related; boundary=dyEV84n7XNJ" ); ```

Owned Type

MediaTypeBuf is an owned and immutable version of MediaType.

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

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

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