summaryrefslogtreecommitdiffstats
path: root/vendor/mime-0.3.16
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/mime-0.3.16')
-rw-r--r--vendor/mime-0.3.16/.cargo-checksum.json1
-rw-r--r--vendor/mime-0.3.16/CONTRIBUTING.md12
-rw-r--r--vendor/mime-0.3.16/Cargo.toml23
-rw-r--r--vendor/mime-0.3.16/LICENSE-APACHE201
-rw-r--r--vendor/mime-0.3.16/LICENSE-MIT20
-rw-r--r--vendor/mime-0.3.16/README.md31
-rw-r--r--vendor/mime-0.3.16/benches/cmp.rs46
-rw-r--r--vendor/mime-0.3.16/benches/fmt.rs19
-rw-r--r--vendor/mime-0.3.16/benches/parse.rs29
-rw-r--r--vendor/mime-0.3.16/src/lib.rs937
-rw-r--r--vendor/mime-0.3.16/src/parse.rs363
11 files changed, 1682 insertions, 0 deletions
diff --git a/vendor/mime-0.3.16/.cargo-checksum.json b/vendor/mime-0.3.16/.cargo-checksum.json
new file mode 100644
index 00000000..220a4713
--- /dev/null
+++ b/vendor/mime-0.3.16/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"CONTRIBUTING.md":"7a8f1d12eb98bd09c290d31f25b03c71ff78027d9fc468e8782efa7dd3e69f1c","Cargo.toml":"75e36b40187c8edad0baae326a0903b6b462f1acd0d68102a8e4f006b8802041","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"df9cfd06d8a44d9a671eadd39ffd97f166481da015a30f45dfd27886209c5922","README.md":"4ac32f1d6d7e1ac9f89f0a6d7d0cbc26f20ef9defdc7b206ef3a77616f493bbf","benches/cmp.rs":"9deb7c222eb69e7c5160aa82d361d4883792be3b557fbf8f7c807b398ba951a1","benches/fmt.rs":"46ec1e7c7970a3eed84b303309a2395ac16d16534ea691db7f361d0016ef0673","benches/parse.rs":"af2b35fc314e39c7fb3fbe6a77b65e54d0f4bd8956950330700028a98513b7d8","src/lib.rs":"c848e55a49ae4ed6451e94c8c120451b5031ba2ab87170ed389eeb4731679446","src/parse.rs":"cfe11f611901a581245b091942bb28ef2eec57645b981e1699d247f11c9e6fe3"},"package":"2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"} \ No newline at end of file
diff --git a/vendor/mime-0.3.16/CONTRIBUTING.md b/vendor/mime-0.3.16/CONTRIBUTING.md
new file mode 100644
index 00000000..3fbc3a31
--- /dev/null
+++ b/vendor/mime-0.3.16/CONTRIBUTING.md
@@ -0,0 +1,12 @@
+# Contributing to mime
+
+## License
+
+Licensed under either of
+
+- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://apache.org/licenses/LICENSE-2.0)
+- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
+
+### Contribution
+
+Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
diff --git a/vendor/mime-0.3.16/Cargo.toml b/vendor/mime-0.3.16/Cargo.toml
new file mode 100644
index 00000000..1f34190e
--- /dev/null
+++ b/vendor/mime-0.3.16/Cargo.toml
@@ -0,0 +1,23 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies
+#
+# If you believe there's an error in this file please file an
+# issue against the rust-lang/cargo repository. If you're
+# editing this file be aware that the upstream Cargo.toml
+# will likely look very different (and much more reasonable)
+
+[package]
+name = "mime"
+version = "0.3.16"
+authors = ["Sean McArthur <sean@seanmonstar.com>"]
+description = "Strongly Typed Mimes"
+documentation = "https://docs.rs/mime"
+keywords = ["mime", "media-extensions", "media-types"]
+license = "MIT/Apache-2.0"
+repository = "https://github.com/hyperium/mime"
+
+[dependencies]
diff --git a/vendor/mime-0.3.16/LICENSE-APACHE b/vendor/mime-0.3.16/LICENSE-APACHE
new file mode 100644
index 00000000..16fe87b0
--- /dev/null
+++ b/vendor/mime-0.3.16/LICENSE-APACHE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/vendor/mime-0.3.16/LICENSE-MIT b/vendor/mime-0.3.16/LICENSE-MIT
new file mode 100644
index 00000000..557b7e5f
--- /dev/null
+++ b/vendor/mime-0.3.16/LICENSE-MIT
@@ -0,0 +1,20 @@
+Copyright (c) 2014 Sean McArthur
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
diff --git a/vendor/mime-0.3.16/README.md b/vendor/mime-0.3.16/README.md
new file mode 100644
index 00000000..dba63140
--- /dev/null
+++ b/vendor/mime-0.3.16/README.md
@@ -0,0 +1,31 @@
+# mime
+
+[![Build Status](https://travis-ci.org/hyperium/mime.svg?branch=master)](https://travis-ci.org/hyperium/mime)
+[![crates.io](https://img.shields.io/crates/v/mime.svg)](https://crates.io/crates/mime)
+[![docs.rs](https://docs.rs/mime/badge.svg)](https://docs.rs/mime)
+
+Support MIME (Media Types) as strong types in Rust.
+
+[Documentation](https://docs.rs/mime)
+
+## Usage
+
+```rust
+extern crate mime;
+
+// 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!
+ }
+}
+```
diff --git a/vendor/mime-0.3.16/benches/cmp.rs b/vendor/mime-0.3.16/benches/cmp.rs
new file mode 100644
index 00000000..b7c64f64
--- /dev/null
+++ b/vendor/mime-0.3.16/benches/cmp.rs
@@ -0,0 +1,46 @@
+#![feature(test)]
+
+extern crate mime;
+extern crate test;
+
+use mime::*;
+use test::Bencher;
+
+#[bench]
+fn bench_eq_parsed(b: &mut Bencher) {
+ let mime = "text/plain; charset=utf-8".parse::<Mime>().unwrap();
+ b.bytes = mime.as_ref().len() as u64;
+ b.iter(|| {
+ assert_eq!(mime, TEXT_PLAIN_UTF_8);
+ })
+}
+
+#[bench]
+fn bench_eq_consts(b: &mut Bencher) {
+ let mime = TEXT_PLAIN_UTF_8;
+ b.bytes = mime.as_ref().len() as u64;
+ b.iter(|| {
+ assert_eq!(mime, TEXT_PLAIN_UTF_8);
+ });
+}
+
+
+#[bench]
+fn bench_ne_consts(b: &mut Bencher) {
+ let one = TEXT_XML;
+ let two = TEXT_CSS;
+ b.bytes = one.as_ref().len() as u64;
+ b.iter(|| {
+ assert_ne!(one, two);
+ });
+}
+
+#[bench]
+fn bench_eq_type_(b: &mut Bencher) {
+ let mime = TEXT_PLAIN_UTF_8;
+ let name = TEXT;
+ b.bytes = name.as_ref().len() as u64;
+ b.iter(|| {
+ assert_eq!(mime.type_(), name);
+ });
+}
diff --git a/vendor/mime-0.3.16/benches/fmt.rs b/vendor/mime-0.3.16/benches/fmt.rs
new file mode 100644
index 00000000..106de1a1
--- /dev/null
+++ b/vendor/mime-0.3.16/benches/fmt.rs
@@ -0,0 +1,19 @@
+#![feature(test)]
+
+extern crate mime;
+extern crate test;
+
+use test::Bencher;
+
+#[bench]
+fn bench_fmt(b: &mut Bencher) {
+ use std::fmt::Write;
+ let mime = ::mime::TEXT_PLAIN_UTF_8;
+ b.bytes = mime.to_string().as_bytes().len() as u64;
+ let mut s = String::with_capacity(64);
+ b.iter(|| {
+ let _ = write!(s, "{}", mime);
+ ::test::black_box(&s);
+ unsafe { s.as_mut_vec().set_len(0); }
+ })
+}
diff --git a/vendor/mime-0.3.16/benches/parse.rs b/vendor/mime-0.3.16/benches/parse.rs
new file mode 100644
index 00000000..7d47781a
--- /dev/null
+++ b/vendor/mime-0.3.16/benches/parse.rs
@@ -0,0 +1,29 @@
+#![feature(test)]
+
+extern crate mime;
+extern crate test;
+
+use mime::Mime;
+use test::Bencher;
+
+
+#[bench]
+fn bench_from_str(b: &mut Bencher) {
+ let s = "text/plain";
+ b.bytes = s.as_bytes().len() as u64;
+ b.iter(|| s.parse::<Mime>())
+}
+
+#[bench]
+fn bench_from_str_charset_utf8(b: &mut Bencher) {
+ let s = "text/plain; charset=utf-8";
+ b.bytes = s.as_bytes().len() as u64;
+ b.iter(|| s.parse::<Mime>())
+}
+
+#[bench]
+fn bench_from_str_extended(b: &mut Bencher) {
+ let s = "text/plain; charset=utf-8; foo=bar";
+ b.bytes = s.as_bytes().len() as u64;
+ b.iter(|| s.parse::<Mime>())
+}
diff --git a/vendor/mime-0.3.16/src/lib.rs b/vendor/mime-0.3.16/src/lib.rs
new file mode 100644
index 00000000..1f24fb1c
--- /dev/null
+++ b/vendor/mime-0.3.16/src/lib.rs
@@ -0,0 +1,937 @@
+//! # Mime
+//!
+//! Mime is now Media Type, technically, but `Mime` is more immediately
+//! understandable, so the main type here is `Mime`.
+//!
+//! ## What is Mime?
+//!
+//! Example mime string: `text/plain`
+//!
+//! ```
+//! let plain_text: mime::Mime = "text/plain".parse().unwrap();
+//! assert_eq!(plain_text, mime::TEXT_PLAIN);
+//! ```
+//!
+//! ## Inspecting Mimes
+//!
+//! ```
+//! let mime = mime::TEXT_PLAIN;
+//! match (mime.type_(), mime.subtype()) {
+//! (mime::TEXT, mime::PLAIN) => println!("plain text!"),
+//! (mime::TEXT, _) => println!("structured text"),
+//! _ => println!("not text"),
+//! }
+//! ```
+
+#![doc(html_root_url = "https://docs.rs/mime/0.3.16")]
+#![deny(warnings)]
+#![deny(missing_docs)]
+#![deny(missing_debug_implementations)]
+
+
+use std::cmp::Ordering;
+use std::error::Error;
+use std::fmt;
+use std::hash::{Hash, Hasher};
+use std::str::FromStr;
+use std::slice;
+
+mod parse;
+
+/// A parsed mime or media type.
+#[derive(Clone)]
+pub struct Mime {
+ source: Source,
+ slash: usize,
+ plus: Option<usize>,
+ params: ParamSource,
+}
+
+/// A section of a `Mime`.
+///
+/// For instance, for the Mime `image/svg+xml`, it contains 3 `Name`s,
+/// `image`, `svg`, and `xml`.
+///
+/// In most cases, `Name`s are compared ignoring case.
+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub struct Name<'a> {
+ // TODO: optimize with an Atom-like thing
+ // There a `const` Names, and so it is possible for the statis strings
+ // to havea different memory address. Additionally, when used in match
+ // statements, the strings are compared with a memcmp, possibly even
+ // if the address and length are the same.
+ //
+ // Being an enum with an Atom variant that is a usize (and without a
+ // string pointer and boolean) would allow for faster comparisons.
+ source: &'a str,
+ insensitive: bool,
+}
+
+/// An error when parsing a `Mime` from a string.
+#[derive(Debug)]
+pub struct FromStrError {
+ inner: parse::ParseError,
+}
+
+impl FromStrError {
+ fn s(&self) -> &str {
+ "mime parse error"
+ }
+}
+
+impl fmt::Display for FromStrError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}: {}", self.s(), self.inner)
+ }
+}
+
+impl Error for FromStrError {
+ // Minimum Rust is 1.15, Error::description was still required then
+ #[allow(deprecated)]
+ fn description(&self) -> &str {
+ self.s()
+ }
+}
+
+#[derive(Clone)]
+enum Source {
+ Atom(u8, &'static str),
+ Dynamic(String),
+}
+
+impl Source {
+ fn as_ref(&self) -> &str {
+ match *self {
+ Source::Atom(_, s) => s,
+ Source::Dynamic(ref s) => s,
+ }
+ }
+}
+
+#[derive(Clone)]
+enum ParamSource {
+ Utf8(usize),
+ Custom(usize, Vec<(Indexed, Indexed)>),
+ None,
+}
+
+#[derive(Clone, Copy)]
+struct Indexed(usize, usize);
+
+impl Mime {
+ /// Get the top level media type for this `Mime`.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// let mime = mime::TEXT_PLAIN;
+ /// assert_eq!(mime.type_(), "text");
+ /// assert_eq!(mime.type_(), mime::TEXT);
+ /// ```
+ #[inline]
+ pub fn type_(&self) -> Name {
+ Name {
+ source: &self.source.as_ref()[..self.slash],
+ insensitive: true,
+ }
+ }
+
+ /// Get the subtype of this `Mime`.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// let mime = mime::TEXT_PLAIN;
+ /// assert_eq!(mime.subtype(), "plain");
+ /// assert_eq!(mime.subtype(), mime::PLAIN);
+ /// ```
+ #[inline]
+ pub fn subtype(&self) -> Name {
+ let end = self.plus.unwrap_or_else(|| {
+ return self.semicolon().unwrap_or(self.source.as_ref().len())
+ });
+ Name {
+ source: &self.source.as_ref()[self.slash + 1..end],
+ insensitive: true,
+ }
+ }
+
+ /// Get an optional +suffix for this `Mime`.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// let svg = "image/svg+xml".parse::<mime::Mime>().unwrap();
+ /// assert_eq!(svg.suffix(), Some(mime::XML));
+ /// assert_eq!(svg.suffix().unwrap(), "xml");
+ ///
+ ///
+ /// assert!(mime::TEXT_PLAIN.suffix().is_none());
+ /// ```
+ #[inline]
+ pub fn suffix(&self) -> Option<Name> {
+ let end = self.semicolon().unwrap_or(self.source.as_ref().len());
+ self.plus.map(|idx| Name {
+ source: &self.source.as_ref()[idx + 1..end],
+ insensitive: true,
+ })
+ }
+
+ /// Look up a parameter by name.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// let mime = mime::TEXT_PLAIN_UTF_8;
+ /// assert_eq!(mime.get_param(mime::CHARSET), Some(mime::UTF_8));
+ /// assert_eq!(mime.get_param("charset").unwrap(), "utf-8");
+ /// assert!(mime.get_param("boundary").is_none());
+ ///
+ /// let mime = "multipart/form-data; boundary=ABCDEFG".parse::<mime::Mime>().unwrap();
+ /// assert_eq!(mime.get_param(mime::BOUNDARY).unwrap(), "ABCDEFG");
+ /// ```
+ pub fn get_param<'a, N>(&'a self, attr: N) -> Option<Name<'a>>
+ where N: PartialEq<Name<'a>> {
+ self.params().find(|e| attr == e.0).map(|e| e.1)
+ }
+
+ /// Returns an iterator over the parameters.
+ #[inline]
+ pub fn params<'a>(&'a self) -> Params<'a> {
+ let inner = match self.params {
+ ParamSource::Utf8(_) => ParamsInner::Utf8,
+ ParamSource::Custom(_, ref params) => {
+ ParamsInner::Custom {
+ source: &self.source,
+ params: params.iter(),
+ }
+ }
+ ParamSource::None => ParamsInner::None,
+ };
+
+ Params(inner)
+ }
+
+ /// Return a `&str` of the Mime's ["essence"][essence].
+ ///
+ /// [essence]: https://mimesniff.spec.whatwg.org/#mime-type-essence
+ pub fn essence_str(&self) -> &str {
+ let end = self.semicolon().unwrap_or(self.source.as_ref().len());
+
+ &self.source.as_ref()[..end]
+ }
+
+ #[cfg(test)]
+ fn has_params(&self) -> bool {
+ match self.params {
+ ParamSource::None => false,
+ _ => true,
+ }
+ }
+
+ #[inline]
+ fn semicolon(&self) -> Option<usize> {
+ match self.params {
+ ParamSource::Utf8(i) |
+ ParamSource::Custom(i, _) => Some(i),
+ ParamSource::None => None,
+ }
+ }
+
+ fn atom(&self) -> u8 {
+ match self.source {
+ Source::Atom(a, _) => a,
+ _ => 0,
+ }
+ }
+}
+
+// Mime ============
+
+fn eq_ascii(a: &str, b: &str) -> bool {
+ // str::eq_ignore_ascii_case didn't stabilize until Rust 1.23.
+ // So while our MSRV is 1.15, gotta import this trait.
+ #[allow(deprecated, unused)]
+ use std::ascii::AsciiExt;
+
+ a.eq_ignore_ascii_case(b)
+}
+
+fn mime_eq_str(mime: &Mime, s: &str) -> bool {
+ if let ParamSource::Utf8(semicolon) = mime.params {
+ if mime.source.as_ref().len() == s.len() {
+ eq_ascii(mime.source.as_ref(), s)
+ } else {
+ params_eq(semicolon, mime.source.as_ref(), s)
+ }
+ } else if let Some(semicolon) = mime.semicolon() {
+ params_eq(semicolon, mime.source.as_ref(), s)
+ } else {
+ eq_ascii(mime.source.as_ref(), s)
+ }
+}
+
+fn params_eq(semicolon: usize, a: &str, b: &str) -> bool {
+ if b.len() < semicolon + 1 {
+ false
+ } else if !eq_ascii(&a[..semicolon], &b[..semicolon]) {
+ false
+ } else {
+ // gotta check for quotes, LWS, and for case senstive names
+ let mut a = &a[semicolon + 1..];
+ let mut b = &b[semicolon + 1..];
+ let mut sensitive;
+
+ loop {
+ a = a.trim();
+ b = b.trim();
+
+ match (a.is_empty(), b.is_empty()) {
+ (true, true) => return true,
+ (true, false) |
+ (false, true) => return false,
+ (false, false) => (),
+ }
+
+ //name
+ if let Some(a_idx) = a.find('=') {
+ let a_name = {
+ #[allow(deprecated)]
+ { a[..a_idx].trim_left() }
+ };
+ if let Some(b_idx) = b.find('=') {
+ let b_name = {
+ #[allow(deprecated)]
+ { b[..b_idx].trim_left() }
+ };
+ if !eq_ascii(a_name, b_name) {
+ return false;
+ }
+ sensitive = a_name != CHARSET;
+ a = &a[..a_idx];
+ b = &b[..b_idx];
+ } else {
+ return false;
+ }
+ } else {
+ return false;
+ }
+ //value
+ let a_quoted = if a.as_bytes()[0] == b'"' {
+ a = &a[1..];
+ true
+ } else {
+ false
+ };
+ let b_quoted = if b.as_bytes()[0] == b'"' {
+ b = &b[1..];
+ true
+ } else {
+ false
+ };
+
+ let a_end = if a_quoted {
+ if let Some(quote) = a.find('"') {
+ quote
+ } else {
+ return false;
+ }
+ } else {
+ a.find(';').unwrap_or(a.len())
+ };
+
+ let b_end = if b_quoted {
+ if let Some(quote) = b.find('"') {
+ quote
+ } else {
+ return false;
+ }
+ } else {
+ b.find(';').unwrap_or(b.len())
+ };
+
+ if sensitive {
+ if !eq_ascii(&a[..a_end], &b[..b_end]) {
+ return false;
+ }
+ } else {
+ if &a[..a_end] != &b[..b_end] {
+ return false;
+ }
+ }
+ a = &a[a_end..];
+ b = &b[b_end..];
+ }
+ }
+}
+
+impl PartialEq for Mime {
+ #[inline]
+ fn eq(&self, other: &Mime) -> bool {
+ match (self.atom(), other.atom()) {
+ // TODO:
+ // This could optimize for when there are no customs parameters.
+ // Any parsed mime has already been lowercased, so if there aren't
+ // any parameters that are case sensistive, this can skip the
+ // eq_ascii, and just use a memcmp instead.
+ (0, _) |
+ (_, 0) => mime_eq_str(self, other.source.as_ref()),
+ (a, b) => a == b,
+ }
+ }
+}
+
+impl Eq for Mime {}
+
+impl PartialOrd for Mime {
+ fn partial_cmp(&self, other: &Mime) -> Option<Ordering> {
+ Some(self.cmp(other))
+ }
+}
+
+impl Ord for Mime {
+ fn cmp(&self, other: &Mime) -> Ordering {
+ self.source.as_ref().cmp(other.source.as_ref())
+ }
+}
+
+impl Hash for Mime {
+ fn hash<T: Hasher>(&self, hasher: &mut T) {
+ hasher.write(self.source.as_ref().as_bytes());
+ }
+}
+
+impl<'a> PartialEq<&'a str> for Mime {
+ #[inline]
+ fn eq(&self, s: & &'a str) -> bool {
+ mime_eq_str(self, *s)
+ }
+}
+
+impl<'a> PartialEq<Mime> for &'a str {
+ #[inline]
+ fn eq(&self, mime: &Mime) -> bool {
+ mime_eq_str(mime, *self)
+ }
+}
+
+impl FromStr for Mime {
+ type Err = FromStrError;
+
+ fn from_str(s: &str) -> Result<Mime, Self::Err> {
+ parse::parse(s).map_err(|e| FromStrError { inner: e })
+ }
+}
+
+impl AsRef<str> for Mime {
+ #[inline]
+ fn as_ref(&self) -> &str {
+ self.source.as_ref()
+ }
+}
+
+impl fmt::Debug for Mime {
+ #[inline]
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Debug::fmt(self.source.as_ref(), f)
+ }
+}
+
+impl fmt::Display for Mime {
+ #[inline]
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Display::fmt(self.source.as_ref(), f)
+ }
+}
+
+// Name ============
+
+fn name_eq_str(name: &Name, s: &str) -> bool {
+ if name.insensitive {
+ eq_ascii(name.source, s)
+ } else {
+ name.source == s
+ }
+}
+
+impl<'a> Name<'a> {
+ /// Get the value of this `Name` as a string.
+ ///
+ /// Note that the borrow is not tied to `&self` but the `'a` lifetime, allowing the
+ /// string to outlive `Name`. Alternately, there is an `impl<'a> From<Name<'a>> for &'a str`
+ /// which isn't rendered by Rustdoc, that can be accessed using `str::from(name)` or `name.into()`.
+ pub fn as_str(&self) -> &'a str {
+ self.source
+