Skip to content

Commit bd5e8f1

Browse files
committed
Merge pull request #485 from pyfisch/content-language
feat(headers): Implement Content-Language header field
2 parents f7f0361 + 308880b commit bd5e8f1

File tree

6 files changed

+74
-38
lines changed

6 files changed

+74
-38
lines changed

src/header/common/accept_language.rs

+2-37
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,4 @@
1-
use header::QualityItem;
2-
use std::str::FromStr;
3-
use std::fmt;
4-
5-
/// A language tag.
6-
/// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.10
7-
#[derive(Clone, PartialEq, Debug)]
8-
pub struct Language{
9-
primary: String,
10-
sub: Option<String>
11-
}
12-
13-
impl FromStr for Language {
14-
type Err = ();
15-
fn from_str(s: &str) -> Result<Language, ()> {
16-
let mut i = s.split("-");
17-
let p = i.next();
18-
let s = i.next();
19-
match (p, s) {
20-
(Some(p),Some(s)) => Ok(Language{primary: p.to_string(),
21-
sub: Some(s.to_string())}),
22-
(Some(p),_) => Ok(Language{primary: p.to_string(), sub: None}),
23-
_ => Err(())
24-
}
25-
}
26-
}
27-
28-
impl fmt::Display for Language {
29-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30-
try!(write!(f, "{}", self.primary));
31-
match self.sub {
32-
Some(ref s) => write!(f, "-{}", s),
33-
None => Ok(())
34-
}
35-
}
36-
}
1+
use header::{Language, QualityItem};
372

383
header! {
394
#[doc="`Accept-Language` header, defined in"]
@@ -57,7 +22,7 @@ header! {
5722

5823
#[cfg(test)]
5924
mod tests {
60-
use header::{Header, qitem, Quality, QualityItem};
25+
use header::{Header, Language, qitem, Quality, QualityItem};
6126
use super::*;
6227

6328
#[test]

src/header/common/content_language.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use header::{Language, QualityItem};
2+
3+
header! {
4+
#[doc="`Content-Language` header, defined in"]
5+
#[doc="[RFC7231](https://tools.ietf.org/html/rfc7231#section-3.1.3.2)"]
6+
#[doc=""]
7+
#[doc="The `Content-Language` header field describes the natural language(s)"]
8+
#[doc="of the intended audience for the representation. Note that this"]
9+
#[doc="might not be equivalent to all the languages used within the"]
10+
#[doc="representation."]
11+
#[doc=""]
12+
#[doc="# ABNF"]
13+
#[doc="```plain"]
14+
#[doc="Content-Language = 1#language-tag"]
15+
#[doc="```"]
16+
(ContentLanguage, "Content-Language") => (QualityItem<Language>)+
17+
18+
test_content_language {
19+
test_header!(test1, vec![b"da"]);
20+
test_header!(test2, vec![b"mi, en"]);
21+
}
22+
}

src/header/common/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub use self::cache_control::{CacheControl, CacheDirective};
2222
pub use self::connection::{Connection, ConnectionOption};
2323
pub use self::content_length::ContentLength;
2424
pub use self::content_encoding::ContentEncoding;
25+
pub use self::content_language::ContentLanguage;
2526
pub use self::content_type::ContentType;
2627
pub use self::cookie::Cookie;
2728
pub use self::date::Date;
@@ -303,6 +304,7 @@ mod cache_control;
303304
mod cookie;
304305
mod connection;
305306
mod content_encoding;
307+
mod content_language;
306308
mod content_length;
307309
mod content_type;
308310
mod date;

src/header/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use unicase::UniCase;
1919
use self::internals::Item;
2020
use error::HttpResult;
2121

22-
pub use self::shared::{Charset, Encoding, EntityTag, HttpDate, Quality, QualityItem, qitem, q};
22+
pub use self::shared::*;
2323
pub use self::common::*;
2424

2525
mod common;

src/header/shared/language.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::str::FromStr;
2+
use std::fmt;
3+
4+
/// A language tag.
5+
/// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.10
6+
///
7+
/// Note: This is no complete language tag implementation, it should be replaced with
8+
/// github.com/pyfisch/rust-language-tag once it is ready.
9+
#[derive(Clone, PartialEq, Debug)]
10+
pub struct Language {
11+
/// The language tag
12+
pub primary: String,
13+
/// A language subtag or country code
14+
pub sub: Option<String>
15+
}
16+
17+
impl FromStr for Language {
18+
type Err = ();
19+
fn from_str(s: &str) -> Result<Language, ()> {
20+
let mut i = s.split("-");
21+
let p = i.next();
22+
let s = i.next();
23+
match (p, s) {
24+
(Some(p), Some(s)) => Ok(Language {
25+
primary: p.to_string(),
26+
sub: Some(s.to_string())
27+
}),
28+
(Some(p), _) => Ok(Language {
29+
primary: p.to_string(),
30+
sub: None
31+
}),
32+
_ => Err(())
33+
}
34+
}
35+
}
36+
37+
impl fmt::Display for Language {
38+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39+
try!(write!(f, "{}", self.primary));
40+
match self.sub {
41+
Some(ref s) => write!(f, "-{}", s),
42+
None => Ok(())
43+
}
44+
}
45+
}

src/header/shared/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ pub use self::charset::Charset;
22
pub use self::encoding::Encoding;
33
pub use self::entity::EntityTag;
44
pub use self::httpdate::HttpDate;
5+
pub use self::language::Language;
56
pub use self::quality_item::{Quality, QualityItem, qitem, q};
67

78
mod charset;
89
mod encoding;
910
mod entity;
1011
mod httpdate;
12+
mod language;
1113
mod quality_item;

0 commit comments

Comments
 (0)