Skip to content

Commit

Permalink
Implement Display for Referer (#122)
Browse files Browse the repository at this point in the history
Based on #95, but just adds impl Display
  • Loading branch information
AsamK authored Jan 3, 2024
1 parent ad331d0 commit e0323f5
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/common/referer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;
use std::str::FromStr;

use http::header::HeaderValue;
use util::HeaderValueString;

/// `Referer` header, defined in
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.5.2)
Expand Down Expand Up @@ -30,7 +31,7 @@ use http::header::HeaderValue;
/// let r = Referer::from_static("/People.html#tim");
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Referer(HeaderValue);
pub struct Referer(HeaderValueString);

derive_header! {
Referer(_),
Expand All @@ -44,7 +45,7 @@ impl Referer {
///
/// Panics if the string is not a legal header value.
pub fn from_static(s: &'static str) -> Referer {
Referer(HeaderValue::from_static(s))
Referer(HeaderValueString::from_static(s))
}
}

Expand All @@ -53,8 +54,14 @@ error_type!(InvalidReferer);
impl FromStr for Referer {
type Err = InvalidReferer;
fn from_str(src: &str) -> Result<Self, Self::Err> {
HeaderValue::from_str(src)
HeaderValueString::from_str(src)
.map(Referer)
.map_err(|_| InvalidReferer { _inner: () })
}
}

impl fmt::Display for Referer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}

0 comments on commit e0323f5

Please sign in to comment.