Struct neo_mime::MediaType [−][src]
pub struct MediaType { /* fields omitted */ }
A parsed media type (or “MIME”).
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 MediaType
s
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"), }
Note about wildcards (*
)
A MediaType
represents an exact format type. The HTTP Accept
header
can include “media ranges”, which can match multiple media types. Those
“media ranges” should be represented as MediaRange
.
Implementations
impl MediaType
[src]
impl MediaType
[src]pub fn parse(source: impl Parse) -> Result<Self, InvalidMime>
[src]
Parse a string as a MediaType
.
Example
let mt = neo_mime::MediaType::parse("text/plain").unwrap(); assert_eq!(mt, neo_mime::TEXT_PLAIN);
Errors
Returns an error if the source is not a valid media type.
pub fn type_(&self) -> &str
[src]
Get the top level media type for this MediaType
.
Example
let mime = neo_mime::TEXT_PLAIN; assert_eq!(mime.type_(), "text"); assert_eq!(mime.type_(), neo_mime::TEXT);
pub fn subtype(&self) -> &str
[src]
Get the subtype of this MediaType
.
Example
let mime = neo_mime::TEXT_PLAIN; assert_eq!(mime.subtype(), "plain"); assert_eq!(mime.subtype(), neo_mime::PLAIN); let svg = neo_mime::IMAGE_SVG; assert_eq!(svg.subtype(), neo_mime::SVG); assert_eq!(svg.subtype(), "svg+xml");
pub fn suffix(&self) -> Option<&str>
[src]
Get an optional +suffix for this MediaType
.
Example
let svg = neo_mime::IMAGE_SVG; assert_eq!(svg.suffix(), Some(neo_mime::XML)); assert_eq!(svg.suffix(), Some("xml")); assert!(neo_mime::TEXT_PLAIN.suffix().is_none());
pub fn param<'a>(&'a self, attr: &str) -> Option<Value<'a>>
[src]
Look up a parameter by name.
Example
let mime = neo_mime::TEXT_PLAIN_UTF_8; assert_eq!(mime.param(neo_mime::CHARSET), Some(neo_mime::UTF_8)); assert_eq!(mime.param("charset").unwrap(), "utf-8"); assert!(mime.param("boundary").is_none()); let mime = "multipart/form-data; boundary=ABCDEFG".parse::<neo_mime::MediaType>().unwrap(); assert_eq!(mime.param(neo_mime::BOUNDARY).unwrap(), "ABCDEFG");
pub fn params(&self) -> impl Iterator<Item = (&str, Value<'_>)>
[src]
Returns an iterator over the parameters.
Example
let pkcs7 = neo_mime::MediaType::parse( "application/pkcs7-mime; smime-type=enveloped-data; name=smime.p7m" ).unwrap(); let mut params = pkcs7.params(); let (name, value) = params.next().unwrap(); assert_eq!(name, "smime-type"); assert_eq!(value, "enveloped-data"); let (name, value) = params.next().unwrap(); assert_eq!(name, "name"); assert_eq!(value, "smime.p7m"); assert!(params.next().is_none());
pub fn has_params(&self) -> bool
[src]
Returns true if the media type has at last one parameter.
Example
let plain_text: neo_mime::MediaType = "text/plain".parse().unwrap(); assert_eq!(plain_text.has_params(), false); let plain_text_utf8: neo_mime::MediaType = "text/plain; charset=utf-8".parse().unwrap(); assert_eq!(plain_text_utf8.has_params(), true);
pub fn without_params(self) -> Self
[src]
Transforms the media type into its non-parametrized form.
Example
use neo_mime::MediaType; let html_xml_utf8 = MediaType::parse("text/html+xml; charset=utf-8").unwrap(); assert!(html_xml_utf8.has_params()); let html_xml = html_xml_utf8.without_params(); assert!(!html_xml.has_params()); assert_eq!(html_xml, "text/html+xml");
Trait Implementations
impl From<MediaType> for MediaRange
[src]
impl From<MediaType> for MediaRange
[src]Any MediaType
can freely be a MediaRange
.
Example
// If we only supported `text/plain`: let range = neo_mime::MediaRange::from(neo_mime::TEXT_PLAIN);
fn from(mt: MediaType) -> MediaRange
[src]
Auto Trait Implementations
impl RefUnwindSafe for MediaType
impl RefUnwindSafe for MediaType
impl UnwindSafe for MediaType
impl UnwindSafe for MediaType