-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(headers): Implement Content-Language header field
Closes #475
- Loading branch information
Showing
6 changed files
with
74 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use header::{Language, QualityItem}; | ||
|
||
header! { | ||
#[doc="`Content-Language` header, defined in"] | ||
#[doc="[RFC7231](https://tools.ietf.org/html/rfc7231#section-3.1.3.2)"] | ||
#[doc=""] | ||
#[doc="The `Content-Language` header field describes the natural language(s)"] | ||
#[doc="of the intended audience for the representation. Note that this"] | ||
#[doc="might not be equivalent to all the languages used within the"] | ||
#[doc="representation."] | ||
#[doc=""] | ||
#[doc="# ABNF"] | ||
#[doc="```plain"] | ||
#[doc="Content-Language = 1#language-tag"] | ||
#[doc="```"] | ||
(ContentLanguage, "Content-Language") => (QualityItem<Language>)+ | ||
|
||
test_content_language { | ||
test_header!(test1, vec![b"da"]); | ||
test_header!(test2, vec![b"mi, en"]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::str::FromStr; | ||
use std::fmt; | ||
|
||
/// A language tag. | ||
/// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.10 | ||
/// | ||
/// Note: This is no complete language tag implementation, it should be replaced with | ||
/// github.com/pyfisch/rust-language-tag once it is ready. | ||
#[derive(Clone, PartialEq, Debug)] | ||
pub struct Language { | ||
/// The language tag | ||
pub primary: String, | ||
/// A language subtag or country code | ||
pub sub: Option<String> | ||
} | ||
|
||
impl FromStr for Language { | ||
type Err = (); | ||
fn from_str(s: &str) -> Result<Language, ()> { | ||
let mut i = s.split("-"); | ||
let p = i.next(); | ||
let s = i.next(); | ||
match (p, s) { | ||
(Some(p), Some(s)) => Ok(Language { | ||
primary: p.to_string(), | ||
sub: Some(s.to_string()) | ||
}), | ||
(Some(p), _) => Ok(Language { | ||
primary: p.to_string(), | ||
sub: None | ||
}), | ||
_ => Err(()) | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for Language { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
try!(write!(f, "{}", self.primary)); | ||
match self.sub { | ||
Some(ref s) => write!(f, "-{}", s), | ||
None => Ok(()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters