rust
let text = "text/plain; charset=UTF-8";
let text_plain = MediaType::parse(text).unwrap();
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)]); ```
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()); ```
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()); ```
```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("ed).unwrap(); textplain.setparam(Name::new("message").unwrap(), value);
asserteq!(textplain.to_string(), "text/plain; message=\"Hello world!\""); ```
```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" ); ```
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"); ```