Merge pull request #485 from pyfisch/content-language
feat(headers): Implement Content-Language header field
This commit is contained in:
		| @@ -1,39 +1,4 @@ | |||||||
| use header::QualityItem; | use header::{Language, 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(()) |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| header! { | header! { | ||||||
|     #[doc="`Accept-Language` header, defined in"] |     #[doc="`Accept-Language` header, defined in"] | ||||||
| @@ -57,7 +22,7 @@ header! { | |||||||
|  |  | ||||||
| #[cfg(test)] | #[cfg(test)] | ||||||
| mod tests { | mod tests { | ||||||
|     use header::{Header, qitem, Quality, QualityItem}; |     use header::{Header, Language, qitem, Quality, QualityItem}; | ||||||
|     use super::*; |     use super::*; | ||||||
|  |  | ||||||
|     #[test] |     #[test] | ||||||
|   | |||||||
							
								
								
									
										22
									
								
								src/header/common/content_language.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/header/common/content_language.rs
									
									
									
									
									
										Normal 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"]); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -22,6 +22,7 @@ pub use self::cache_control::{CacheControl, CacheDirective}; | |||||||
| pub use self::connection::{Connection, ConnectionOption}; | pub use self::connection::{Connection, ConnectionOption}; | ||||||
| pub use self::content_length::ContentLength; | pub use self::content_length::ContentLength; | ||||||
| pub use self::content_encoding::ContentEncoding; | pub use self::content_encoding::ContentEncoding; | ||||||
|  | pub use self::content_language::ContentLanguage; | ||||||
| pub use self::content_type::ContentType; | pub use self::content_type::ContentType; | ||||||
| pub use self::cookie::Cookie; | pub use self::cookie::Cookie; | ||||||
| pub use self::date::Date; | pub use self::date::Date; | ||||||
| @@ -303,6 +304,7 @@ mod cache_control; | |||||||
| mod cookie; | mod cookie; | ||||||
| mod connection; | mod connection; | ||||||
| mod content_encoding; | mod content_encoding; | ||||||
|  | mod content_language; | ||||||
| mod content_length; | mod content_length; | ||||||
| mod content_type; | mod content_type; | ||||||
| mod date; | mod date; | ||||||
|   | |||||||
| @@ -19,7 +19,7 @@ use unicase::UniCase; | |||||||
| use self::internals::Item; | use self::internals::Item; | ||||||
| use error::HttpResult; | use error::HttpResult; | ||||||
|  |  | ||||||
| pub use self::shared::{Charset, Encoding, EntityTag, HttpDate, Quality, QualityItem, qitem, q}; | pub use self::shared::*; | ||||||
| pub use self::common::*; | pub use self::common::*; | ||||||
|  |  | ||||||
| mod common; | mod common; | ||||||
|   | |||||||
							
								
								
									
										45
									
								
								src/header/shared/language.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/header/shared/language.rs
									
									
									
									
									
										Normal 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(()) | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -2,10 +2,12 @@ pub use self::charset::Charset; | |||||||
| pub use self::encoding::Encoding; | pub use self::encoding::Encoding; | ||||||
| pub use self::entity::EntityTag; | pub use self::entity::EntityTag; | ||||||
| pub use self::httpdate::HttpDate; | pub use self::httpdate::HttpDate; | ||||||
|  | pub use self::language::Language; | ||||||
| pub use self::quality_item::{Quality, QualityItem, qitem, q}; | pub use self::quality_item::{Quality, QualityItem, qitem, q}; | ||||||
|  |  | ||||||
| mod charset; | mod charset; | ||||||
| mod encoding; | mod encoding; | ||||||
| mod entity; | mod entity; | ||||||
| mod httpdate; | mod httpdate; | ||||||
|  | mod language; | ||||||
| mod quality_item; | mod quality_item; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user