Merge pull request #485 from pyfisch/content-language

feat(headers): Implement Content-Language header field
This commit is contained in:
Sean McArthur
2015-04-27 14:15:54 -07:00
6 changed files with 74 additions and 38 deletions

View File

@@ -1,39 +1,4 @@
use header::QualityItem;
use std::str::FromStr;
use std::fmt;
/// A language tag.
/// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.10
#[derive(Clone, PartialEq, Debug)]
pub struct Language{
primary: String,
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(())
}
}
}
use header::{Language, QualityItem};
header! {
#[doc="`Accept-Language` header, defined in"]
@@ -57,7 +22,7 @@ header! {
#[cfg(test)]
mod tests {
use header::{Header, qitem, Quality, QualityItem};
use header::{Header, Language, qitem, Quality, QualityItem};
use super::*;
#[test]

View File

@@ -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"]);
}
}

View File

@@ -22,6 +22,7 @@ pub use self::cache_control::{CacheControl, CacheDirective};
pub use self::connection::{Connection, ConnectionOption};
pub use self::content_length::ContentLength;
pub use self::content_encoding::ContentEncoding;
pub use self::content_language::ContentLanguage;
pub use self::content_type::ContentType;
pub use self::cookie::Cookie;
pub use self::date::Date;
@@ -303,6 +304,7 @@ mod cache_control;
mod cookie;
mod connection;
mod content_encoding;
mod content_language;
mod content_length;
mod content_type;
mod date;

View File

@@ -19,7 +19,7 @@ use unicase::UniCase;
use self::internals::Item;
use error::HttpResult;
pub use self::shared::{Charset, Encoding, EntityTag, HttpDate, Quality, QualityItem, qitem, q};
pub use self::shared::*;
pub use self::common::*;
mod common;

View File

@@ -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(())
}
}
}

View File

@@ -2,10 +2,12 @@ pub use self::charset::Charset;
pub use self::encoding::Encoding;
pub use self::entity::EntityTag;
pub use self::httpdate::HttpDate;
pub use self::language::Language;
pub use self::quality_item::{Quality, QualityItem, qitem, q};
mod charset;
mod encoding;
mod entity;
mod httpdate;
mod language;
mod quality_item;