Add Header methods to get name and value as strs.

This commit is contained in:
Sergio Benitez 2017-01-06 01:03:08 -06:00
parent 5e30262378
commit 21a1bde7c0
1 changed files with 47 additions and 0 deletions

View File

@ -51,6 +51,53 @@ impl<'h> Header<'h> {
value: value.into()
}
}
/// Returns the name of this header with casing preserved. To do a
/// case-insensitive equality check, use `.name` directly.
///
/// # Example
///
/// A case-sensitive equality check:
///
/// ```rust
/// use rocket::http::Header;
///
/// let value = format!("{} value", "custom");
/// let header = Header::new("X-Custom-Header", value);
/// assert_eq!(header.name(), "X-Custom-Header");
/// assert!(header.name() != "X-CUSTOM-HEADER");
/// ```
///
/// A case-insensitive equality check via `.name`:
///
/// ```rust
/// use rocket::http::Header;
///
/// let header = Header::new("X-Custom-Header", "custom value");
/// assert_eq!(header.name, "X-Custom-Header");
/// assert_eq!(header.name, "X-CUSTOM-HEADER");
/// ```
#[inline(always)]
pub fn name(&self) -> &str {
self.name.as_str()
}
/// Returns the value of this header.
///
/// # Example
///
/// A case-sensitive equality check:
///
/// ```rust
/// use rocket::http::Header;
///
/// let header = Header::new("X-Custom-Header", "custom value");
/// assert_eq!(header.value(), "custom value");
/// ```
#[inline(always)]
pub fn value(&self) -> &str {
&self.value
}
}
impl<'h> fmt::Display for Header<'h> {