Skip to content

Commit

Permalink
feat(headers): Implement Content-Language header field
Browse files Browse the repository at this point in the history
Closes #475
  • Loading branch information
pyfisch committed Apr 27, 2015
1 parent f7f0361 commit 308880b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 38 deletions.
39 changes: 2 additions & 37 deletions src/header/common/accept_language.rs
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -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]
Expand Down
22 changes: 22 additions & 0 deletions src/header/common/content_language.rs
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"]);
}
}
2 changes: 2 additions & 0 deletions src/header/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions src/header/shared/language.rs
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(())
}
}
}
2 changes: 2 additions & 0 deletions src/header/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 308880b

Please sign in to comment.