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:
Sergio Benitez 2021-07-04 13:48:54 -07:00
parent bb920e6e09
commit efce2bc958
2 changed files with 25 additions and 46 deletions

View File

@ -235,39 +235,37 @@ impl ContentType {
known_extensions!(from_extension); known_extensions!(from_extension);
/// Creates a new `ContentType` with top-level type `top`, subtype `sub`, /// Sets the parameters `parameters` on `self`.
/// and parameters `ps`. This should _only_ be used to construct uncommon or
/// custom content types. Use an associated constant for everything else.
/// ///
/// # Example /// # Example
/// ///
/// Create a custom `application/x-id; id=1` content type: /// Create a custom `application/x-id; id=1` media type:
/// ///
/// ```rust /// ```rust
/// # extern crate rocket; /// # extern crate rocket;
/// use rocket::http::ContentType; /// 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()); /// 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 /// ```rust
/// # extern crate rocket; /// # extern crate rocket;
/// use rocket::http::ContentType; /// use rocket::http::ContentType;
/// ///
/// let params = vec![("name", "bob"), ("ref", "2382")]; /// let mt = ContentType::new("text", "person")
/// let mt = ContentType::with_params("text", "person", params); /// .with_params([("name", "bob"), ("ref", "2382")]);
///
/// assert_eq!(mt.to_string(), "text/person; name=bob; ref=2382".to_string()); /// assert_eq!(mt.to_string(), "text/person; name=bob; ref=2382".to_string());
/// ``` /// ```
#[inline] pub fn with_params<K, V, P>(self, parameters: P) -> ContentType
pub fn with_params<T, S, K, V, P>(top: T, sub: S, ps: P) -> ContentType where K: Into<Cow<'static, str>>,
where T: Into<Cow<'static, str>>, S: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>,
K: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>,
P: IntoCollection<(K, V)> P: IntoCollection<(K, V)>
{ {
ContentType(MediaType::with_params(top, sub, ps)) ContentType(self.0.with_params(parameters))
} }
/// Borrows the inner `MediaType` of `self`. /// Borrows the inner `MediaType` of `self`.

View File

@ -305,9 +305,7 @@ impl MediaType {
} }
} }
/// Creates a new `MediaType` with top-level type `top`, subtype `sub`, and /// Sets the parameters `parameters` on `self`.
/// parameters `ps`. This should _only_ be used to construct uncommon or
/// custom media types. Use an associated constant for everything else.
/// ///
/// # Example /// # Example
/// ///
@ -317,7 +315,7 @@ impl MediaType {
/// # extern crate rocket; /// # extern crate rocket;
/// use rocket::http::MediaType; /// 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()); /// assert_eq!(id.to_string(), "application/x-id; id=1".to_string());
/// ``` /// ```
/// ///
@ -327,27 +325,21 @@ impl MediaType {
/// # extern crate rocket; /// # extern crate rocket;
/// use rocket::http::MediaType; /// use rocket::http::MediaType;
/// ///
/// let params = vec![("name", "bob"), ("ref", "2382")]; /// let mt = MediaType::new("text", "person")
/// let mt = MediaType::with_params("text", "person", params); /// .with_params([("name", "bob"), ("ref", "2382")]);
///
/// assert_eq!(mt.to_string(), "text/person; name=bob; ref=2382".to_string()); /// assert_eq!(mt.to_string(), "text/person; name=bob; ref=2382".to_string());
/// ``` /// ```
#[inline] pub fn with_params<K, V, P>(mut self, ps: P) -> MediaType
pub fn with_params<T, S, K, V, P>(top: T, sub: S, ps: P) -> MediaType where K: Into<Cow<'static, str>>,
where T: Into<Cow<'static, str>>, S: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>,
K: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>,
P: IntoCollection<(K, V)> P: IntoCollection<(K, V)>
{ {
let params = ps.mapped(|(key, val)| ( use Indexed::Concrete;
Indexed::Concrete(key.into()),
Indexed::Concrete(val.into())
));
MediaType { let params = ps.mapped(|(k, v)| (Concrete(k.into()), Concrete(v.into())));
source: Source::None, self.params = MediaParams::Dynamic(params);
top: Indexed::Concrete(top.into()), self
sub: Indexed::Concrete(sub.into()),
params: MediaParams::Dynamic(params)
}
} }
/// A `const` variant of [`MediaType::with_params()`]. Creates a new /// A `const` variant of [`MediaType::with_params()`]. Creates a new
@ -490,7 +482,7 @@ impl MediaType {
/// use rocket::http::MediaType; /// use rocket::http::MediaType;
/// ///
/// let plain = MediaType::Plain; /// 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"); /// let just_plain = MediaType::new("text", "plain");
/// ///
/// // The `PartialEq` implementation doesn't consider parameters. /// // The `PartialEq` implementation doesn't consider parameters.
@ -504,18 +496,7 @@ impl MediaType {
/// assert!(plain.exact_eq(&plain2)); /// assert!(plain.exact_eq(&plain2));
/// ``` /// ```
pub fn exact_eq(&self, other: &MediaType) -> bool { pub fn exact_eq(&self, other: &MediaType) -> bool {
self == other && { self == other && self.params().eq(other.params())
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
}
}
}
} }
/// Returns an iterator over the (key, value) pairs of the media type's /// Returns an iterator over the (key, value) pairs of the media type's