Exposed the 'from_one_raw_str' function for use in defining new header structs.

This commit is contained in:
Gilman Tolle
2014-10-15 23:40:40 -07:00
parent 2ea75c967d
commit 5615ab276e
10 changed files with 26 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
use header::Header;
use std::fmt::{mod, Show};
use super::from_one_raw_str;
use super::util::from_one_raw_str;
use std::from_str::FromStr;
/// The `Connection` header.

View File

@@ -1,7 +1,7 @@
use std::fmt::{mod, Show};
use header::Header;
use super::from_one_raw_str;
use super::util::from_one_raw_str;
/// The `Content-Length` header.
///

View File

@@ -1,6 +1,6 @@
use header::Header;
use std::fmt::{mod, Show};
use super::from_one_raw_str;
use super::util::from_one_raw_str;
use mime::Mime;
/// The `Content-Type` header.

View File

@@ -1,6 +1,6 @@
use header::Header;
use std::fmt::{mod, Show};
use super::from_one_raw_str;
use super::util::from_one_raw_str;
use std::from_str::FromStr;
use time::{Tm, strptime};

View File

@@ -1,6 +1,6 @@
use header::Header;
use std::fmt::{mod, Show};
use super::from_one_raw_str;
use super::util::from_one_raw_str;
/// The `Host` header.
///

View File

@@ -1,6 +1,6 @@
use header::Header;
use std::fmt::{mod, Show};
use super::from_one_raw_str;
use super::util::from_one_raw_str;
/// The `Location` header.
///

View File

@@ -17,9 +17,6 @@ pub use self::server::Server;
pub use self::date::Date;
pub use self::location::Location;
use std::from_str::FromStr;
use std::str::from_utf8;
/// Exposes the Host header.
pub mod host;
@@ -50,14 +47,6 @@ pub mod date;
/// Exposes the Location header.
pub mod location;
fn from_one_raw_str<T: FromStr>(raw: &[Vec<u8>]) -> Option<T> {
if raw.len() != 1 {
return None;
}
// we JUST checked that raw.len() == 1, so raw[0] WILL exist.
match from_utf8(unsafe { raw.as_slice().unsafe_get(0).as_slice() }) {
Some(s) => FromStr::from_str(s),
None => None
}
}
pub mod util;

View File

@@ -1,6 +1,6 @@
use header::Header;
use std::fmt::{mod, Show};
use super::from_one_raw_str;
use super::util::from_one_raw_str;
/// The `Server` header field.
///

View File

@@ -1,6 +1,6 @@
use header::Header;
use std::fmt::{mod, Show};
use super::from_one_raw_str;
use super::util::from_one_raw_str;
/// The `User-Agent` header field.
///

16
src/header/common/util.rs Normal file
View File

@@ -0,0 +1,16 @@
//! Utility functions for Header implementations.
use std::from_str::FromStr;
use std::str::from_utf8;
/// Utility function that reads a single raw string when parsing a header
pub fn from_one_raw_str<T: FromStr>(raw: &[Vec<u8>]) -> Option<T> {
if raw.len() != 1 {
return None;
}
// we JUST checked that raw.len() == 1, so raw[0] WILL exist.
match from_utf8(unsafe { raw.as_slice().unsafe_get(0).as_slice() }) {
Some(s) => FromStr::from_str(s),
None => None
}
}