script/
mime.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use data_url::mime::Mime;
6use headers::ContentType;
7
8pub(crate) static APPLICATION: &str = "application";
9pub(crate) static CHARSET: &str = "charset";
10pub(crate) static HTML: &str = "html";
11pub(crate) static TEXT: &str = "text";
12pub(crate) static XML: &str = "xml";
13
14/// Convenience methods to make the data_url Mime type more ergonomic.
15pub(crate) trait MimeExt {
16    /// Checks that the subtype has a given suffix.
17    /// Eg. image/svg+xml has the the xml suffix.
18    fn has_suffix(&self, suffix: &str) -> bool;
19
20    /// TODO: replace by a derive on data_url.
21    fn clone(&self) -> Self;
22
23    /// Build a Mime from the value of a Content-Type header.
24    fn from_ct(ct: ContentType) -> Self;
25}
26
27impl MimeExt for Mime {
28    fn has_suffix(&self, suffix: &str) -> bool {
29        self.subtype.ends_with(&format!("+{}", suffix))
30    }
31
32    fn clone(&self) -> Self {
33        Self {
34            type_: self.type_.clone(),
35            subtype: self.subtype.clone(),
36            parameters: self.parameters.clone(),
37        }
38    }
39
40    fn from_ct(ct: ContentType) -> Self {
41        ct.to_string().parse().unwrap()
42    }
43}