feat(headers): Implement Content-Language header field

Closes #475
This commit is contained in:
Pyfisch
2015-04-27 21:11:21 +02:00
parent f7f0361626
commit 308880b455
6 changed files with 74 additions and 38 deletions

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