diff --git a/src/header/common/accept_language.rs b/src/header/common/accept_language.rs index ef478c4c97..945c7de0c9 100644 --- a/src/header/common/accept_language.rs +++ b/src/header/common/accept_language.rs @@ -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 -} - -impl FromStr for Language { - type Err = (); - fn from_str(s: &str) -> Result { - 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] diff --git a/src/header/common/content_language.rs b/src/header/common/content_language.rs new file mode 100644 index 0000000000..0680dca0a7 --- /dev/null +++ b/src/header/common/content_language.rs @@ -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)+ + + test_content_language { + test_header!(test1, vec![b"da"]); + test_header!(test2, vec![b"mi, en"]); + } +} diff --git a/src/header/common/mod.rs b/src/header/common/mod.rs index f1218f43a8..4dd2db80e6 100644 --- a/src/header/common/mod.rs +++ b/src/header/common/mod.rs @@ -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; diff --git a/src/header/mod.rs b/src/header/mod.rs index 4071d9f3f4..739ab5293c 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -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; diff --git a/src/header/shared/language.rs b/src/header/shared/language.rs new file mode 100644 index 0000000000..e670abd933 --- /dev/null +++ b/src/header/shared/language.rs @@ -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 +} + +impl FromStr for Language { + type Err = (); + fn from_str(s: &str) -> Result { + 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(()) + } + } +} diff --git a/src/header/shared/mod.rs b/src/header/shared/mod.rs index d2c2355bc0..1349b483b4 100644 --- a/src/header/shared/mod.rs +++ b/src/header/shared/mod.rs @@ -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;