Skip to content

Commit f883ce8

Browse files
committed
move other header parse utils to util module
1 parent 77e68fa commit f883ce8

File tree

5 files changed

+34
-34
lines changed

5 files changed

+34
-34
lines changed

common/connection.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use header::{Header, HeaderFormat};
22
use std::fmt::{mod, Show};
3-
use super::{from_comma_delimited, fmt_comma_delimited};
43
use std::str::FromStr;
4+
use super::util::{from_comma_delimited, fmt_comma_delimited};
55

66
/// The `Connection` header.
77
#[deriving(Clone, PartialEq, Show)]

common/mod.rs

-30
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ pub use self::user_agent::UserAgent;
2121
pub use self::server::Server;
2222
pub use self::set_cookie::SetCookie;
2323

24-
use std::fmt::{mod, Show};
25-
use std::str::{FromStr, from_utf8};
26-
2724
macro_rules! bench_header(
2825
($name:ident, $ty:ty, $value:expr) => {
2926
#[cfg(test)]
@@ -105,30 +102,3 @@ pub mod upgrade;
105102
pub mod user_agent;
106103

107104
pub mod util;
108-
109-
fn from_comma_delimited<T: FromStr>(raw: &[Vec<u8>]) -> Option<Vec<T>> {
110-
if raw.len() != 1 {
111-
return None;
112-
}
113-
// we JUST checked that raw.len() == 1, so raw[0] WILL exist.
114-
match from_utf8(unsafe { raw.as_slice().unsafe_get(0).as_slice() }) {
115-
Some(s) => {
116-
Some(s.as_slice()
117-
.split([',', ' '].as_slice())
118-
.filter_map(from_str)
119-
.collect())
120-
}
121-
None => None
122-
}
123-
}
124-
125-
fn fmt_comma_delimited<T: Show>(fmt: &mut fmt::Formatter, parts: &[T]) -> fmt::Result {
126-
let last = parts.len() - 1;
127-
for (i, part) in parts.iter().enumerate() {
128-
try!(part.fmt(fmt));
129-
if i < last {
130-
try!(", ".fmt(fmt));
131-
}
132-
}
133-
Ok(())
134-
}

common/transfer_encoding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use header::{Header, HeaderFormat};
22
use std::fmt;
33
use std::str::FromStr;
4-
use super::{from_comma_delimited, fmt_comma_delimited};
4+
use super::util::{from_comma_delimited, fmt_comma_delimited};
55

66
/// The `Transfer-Encoding` header.
77
///

common/upgrade.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use header::{Header, HeaderFormat};
22
use std::fmt::{mod, Show};
3-
use super::{from_comma_delimited, fmt_comma_delimited};
43
use std::str::FromStr;
4+
use super::util::{from_comma_delimited, fmt_comma_delimited};
55

66
/// The `Upgrade` header.
77
#[deriving(Clone, PartialEq, Show)]

common/util.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Utility functions for Header implementations.
22
33
use std::str::{FromStr, from_utf8};
4+
use std::fmt::{mod, Show};
45

5-
/// Utility function that reads a single raw string when parsing a header
6+
/// Reads a single raw string when parsing a header
67
pub fn from_one_raw_str<T: FromStr>(raw: &[Vec<u8>]) -> Option<T> {
78
if raw.len() != 1 {
89
return None;
@@ -13,3 +14,32 @@ pub fn from_one_raw_str<T: FromStr>(raw: &[Vec<u8>]) -> Option<T> {
1314
None => None
1415
}
1516
}
17+
18+
/// Reads a comma-delimited raw string into a Vec.
19+
pub fn from_comma_delimited<T: FromStr>(raw: &[Vec<u8>]) -> Option<Vec<T>> {
20+
if raw.len() != 1 {
21+
return None;
22+
}
23+
// we JUST checked that raw.len() == 1, so raw[0] WILL exist.
24+
match from_utf8(unsafe { raw.as_slice().unsafe_get(0).as_slice() }) {
25+
Some(s) => {
26+
Some(s.as_slice()
27+
.split([',', ' '].as_slice())
28+
.filter_map(from_str)
29+
.collect())
30+
}
31+
None => None
32+
}
33+
}
34+
35+
/// Format an array into a comma-delimited string.
36+
pub fn fmt_comma_delimited<T: Show>(fmt: &mut fmt::Formatter, parts: &[T]) -> fmt::Result {
37+
let last = parts.len() - 1;
38+
for (i, part) in parts.iter().enumerate() {
39+
try!(part.fmt(fmt));
40+
if i < last {
41+
try!(", ".fmt(fmt));
42+
}
43+
}
44+
Ok(())
45+
}

0 commit comments

Comments
 (0)