mirror of https://github.com/rwf2/Rocket.git
Make 'MediaType::with_params()' a builder method.
Instead of a constructor, 'MediaType::with_params()' and 'ContentType::with_params()' are now both builder methods. This allow chaining the method to associated constants.
This commit is contained in:
parent
bb920e6e09
commit
efce2bc958
|
@ -235,39 +235,37 @@ impl ContentType {
|
|||
|
||||
known_extensions!(from_extension);
|
||||
|
||||
/// Creates a new `ContentType` with top-level type `top`, subtype `sub`,
|
||||
/// and parameters `ps`. This should _only_ be used to construct uncommon or
|
||||
/// custom content types. Use an associated constant for everything else.
|
||||
/// Sets the parameters `parameters` on `self`.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// Create a custom `application/x-id; id=1` content type:
|
||||
/// Create a custom `application/x-id; id=1` media type:
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate rocket;
|
||||
/// use rocket::http::ContentType;
|
||||
///
|
||||
/// let id = ContentType::with_params("application", "x-id", ("id", "1"));
|
||||
/// let id = ContentType::new("application", "x-id").with_params(("id", "1"));
|
||||
/// assert_eq!(id.to_string(), "application/x-id; id=1".to_string());
|
||||
/// ```
|
||||
///
|
||||
/// Create a custom `text/person; name=bob; weight=175` content type:
|
||||
/// Create a custom `text/person; name=bob; weight=175` media type:
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate rocket;
|
||||
/// use rocket::http::ContentType;
|
||||
///
|
||||
/// let params = vec![("name", "bob"), ("ref", "2382")];
|
||||
/// let mt = ContentType::with_params("text", "person", params);
|
||||
/// let mt = ContentType::new("text", "person")
|
||||
/// .with_params([("name", "bob"), ("ref", "2382")]);
|
||||
///
|
||||
/// assert_eq!(mt.to_string(), "text/person; name=bob; ref=2382".to_string());
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn with_params<T, S, K, V, P>(top: T, sub: S, ps: P) -> ContentType
|
||||
where T: Into<Cow<'static, str>>, S: Into<Cow<'static, str>>,
|
||||
K: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>,
|
||||
pub fn with_params<K, V, P>(self, parameters: P) -> ContentType
|
||||
where K: Into<Cow<'static, str>>,
|
||||
V: Into<Cow<'static, str>>,
|
||||
P: IntoCollection<(K, V)>
|
||||
{
|
||||
ContentType(MediaType::with_params(top, sub, ps))
|
||||
ContentType(self.0.with_params(parameters))
|
||||
}
|
||||
|
||||
/// Borrows the inner `MediaType` of `self`.
|
||||
|
|
|
@ -305,9 +305,7 @@ impl MediaType {
|
|||
}
|
||||
}
|
||||
|
||||
/// Creates a new `MediaType` with top-level type `top`, subtype `sub`, and
|
||||
/// parameters `ps`. This should _only_ be used to construct uncommon or
|
||||
/// custom media types. Use an associated constant for everything else.
|
||||
/// Sets the parameters `parameters` on `self`.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
|
@ -317,7 +315,7 @@ impl MediaType {
|
|||
/// # extern crate rocket;
|
||||
/// use rocket::http::MediaType;
|
||||
///
|
||||
/// let id = MediaType::with_params("application", "x-id", ("id", "1"));
|
||||
/// let id = MediaType::new("application", "x-id").with_params(("id", "1"));
|
||||
/// assert_eq!(id.to_string(), "application/x-id; id=1".to_string());
|
||||
/// ```
|
||||
///
|
||||
|
@ -327,27 +325,21 @@ impl MediaType {
|
|||
/// # extern crate rocket;
|
||||
/// use rocket::http::MediaType;
|
||||
///
|
||||
/// let params = vec![("name", "bob"), ("ref", "2382")];
|
||||
/// let mt = MediaType::with_params("text", "person", params);
|
||||
/// let mt = MediaType::new("text", "person")
|
||||
/// .with_params([("name", "bob"), ("ref", "2382")]);
|
||||
///
|
||||
/// assert_eq!(mt.to_string(), "text/person; name=bob; ref=2382".to_string());
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn with_params<T, S, K, V, P>(top: T, sub: S, ps: P) -> MediaType
|
||||
where T: Into<Cow<'static, str>>, S: Into<Cow<'static, str>>,
|
||||
K: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>,
|
||||
pub fn with_params<K, V, P>(mut self, ps: P) -> MediaType
|
||||
where K: Into<Cow<'static, str>>,
|
||||
V: Into<Cow<'static, str>>,
|
||||
P: IntoCollection<(K, V)>
|
||||
{
|
||||
let params = ps.mapped(|(key, val)| (
|
||||
Indexed::Concrete(key.into()),
|
||||
Indexed::Concrete(val.into())
|
||||
));
|
||||
use Indexed::Concrete;
|
||||
|
||||
MediaType {
|
||||
source: Source::None,
|
||||
top: Indexed::Concrete(top.into()),
|
||||
sub: Indexed::Concrete(sub.into()),
|
||||
params: MediaParams::Dynamic(params)
|
||||
}
|
||||
let params = ps.mapped(|(k, v)| (Concrete(k.into()), Concrete(v.into())));
|
||||
self.params = MediaParams::Dynamic(params);
|
||||
self
|
||||
}
|
||||
|
||||
/// A `const` variant of [`MediaType::with_params()`]. Creates a new
|
||||
|
@ -490,7 +482,7 @@ impl MediaType {
|
|||
/// use rocket::http::MediaType;
|
||||
///
|
||||
/// let plain = MediaType::Plain;
|
||||
/// let plain2 = MediaType::with_params("text", "plain", ("charset", "utf-8"));
|
||||
/// let plain2 = MediaType::new("text", "plain").with_params(("charset", "utf-8"));
|
||||
/// let just_plain = MediaType::new("text", "plain");
|
||||
///
|
||||
/// // The `PartialEq` implementation doesn't consider parameters.
|
||||
|
@ -504,18 +496,7 @@ impl MediaType {
|
|||
/// assert!(plain.exact_eq(&plain2));
|
||||
/// ```
|
||||
pub fn exact_eq(&self, other: &MediaType) -> bool {
|
||||
self == other && {
|
||||
let (mut a_params, mut b_params) = (self.params(), other.params());
|
||||
loop {
|
||||
match (a_params.next(), b_params.next()) {
|
||||
(Some(a), Some(b)) if a != b => return false,
|
||||
(Some(_), Some(_)) => continue,
|
||||
(Some(_), None) => return false,
|
||||
(None, Some(_)) => return false,
|
||||
(None, None) => return true
|
||||
}
|
||||
}
|
||||
}
|
||||
self == other && self.params().eq(other.params())
|
||||
}
|
||||
|
||||
/// Returns an iterator over the (key, value) pairs of the media type's
|
||||
|
|
Loading…
Reference in New Issue