Skip to content

Commit

Permalink
fix(rustup): update FromStr
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Atashian <retep998@gmail.com>
  • Loading branch information
retep998 authored and seanmonstar committed Feb 4, 2015
1 parent c983ebf commit 742081c
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 79 deletions.
2 changes: 1 addition & 1 deletion benches/client_mock_tcp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(core, collections, io, test)]
#![feature(collections, io, test)]
extern crate hyper;

extern crate test;
Expand Down
15 changes: 8 additions & 7 deletions src/header/common/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ impl<S: Scheme> Header for Authorization<S> {
match (from_utf8(unsafe { &raw[].get_unchecked(0)[] }), Scheme::scheme(None::<S>)) {
(Ok(header), Some(scheme))
if header.starts_with(scheme) && header.len() > scheme.len() + 1 => {
header[scheme.len() + 1..].parse::<S>().map(|s| Authorization(s))
header[scheme.len() + 1..].parse::<S>().map(|s| Authorization(s)).ok()
},
(Ok(header), None) => header.parse::<S>().map(|s| Authorization(s)),
(Ok(header), None) => header.parse::<S>().map(|s| Authorization(s)).ok(),
_ => None
}
} else {
Expand Down Expand Up @@ -108,32 +108,33 @@ impl Scheme for Basic {
}

impl FromStr for Basic {
fn from_str(s: &str) -> Option<Basic> {
type Err = ();
fn from_str(s: &str) -> Result<Basic, ()> {
match s.from_base64() {
Ok(decoded) => match String::from_utf8(decoded) {
Ok(text) => {
let mut parts = &mut text[].split(':');
let user = match parts.next() {
Some(part) => part.to_string(),
None => return None
None => return Err(())
};
let password = match parts.next() {
Some(part) => Some(part.to_string()),
None => None
};
Some(Basic {
Ok(Basic {
username: user,
password: password
})
},
Err(e) => {
debug!("Basic::from_utf8 error={:?}", e);
None
Err(())
}
},
Err(e) => {
debug!("Basic::from_base64 error={:?}", e);
None
Err(())
}
}
}
Expand Down
35 changes: 18 additions & 17 deletions src/header/common/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,29 @@ impl fmt::Display for CacheDirective {
}

impl FromStr for CacheDirective {
fn from_str(s: &str) -> Option<CacheDirective> {
type Err = Option<<u32 as FromStr>::Err>;
fn from_str(s: &str) -> Result<CacheDirective, Option<<u32 as FromStr>::Err>> {
use self::CacheDirective::*;
match s {
"no-cache" => Some(NoCache),
"no-store" => Some(NoStore),
"no-transform" => Some(NoTransform),
"only-if-cached" => Some(OnlyIfCached),
"must-revalidate" => Some(MustRevalidate),
"public" => Some(Public),
"private" => Some(Private),
"proxy-revalidate" => Some(ProxyRevalidate),
"" => None,
"no-cache" => Ok(NoCache),
"no-store" => Ok(NoStore),
"no-transform" => Ok(NoTransform),
"only-if-cached" => Ok(OnlyIfCached),
"must-revalidate" => Ok(MustRevalidate),
"public" => Ok(Public),
"private" => Ok(Private),
"proxy-revalidate" => Ok(ProxyRevalidate),
"" => Err(None),
_ => match s.find('=') {
Some(idx) if idx+1 < s.len() => match (&s[..idx], &s[idx+1..].trim_matches('"')) {
("max-age" , secs) => secs.parse().map(MaxAge),
("max-stale", secs) => secs.parse().map(MaxStale),
("min-fresh", secs) => secs.parse().map(MinFresh),
("s-maxage", secs) => secs.parse().map(SMaxAge),
(left, right) => Some(Extension(left.to_string(), Some(right.to_string())))
("max-age" , secs) => secs.parse().map(MaxAge).map_err(|x| Some(x)),
("max-stale", secs) => secs.parse().map(MaxStale).map_err(|x| Some(x)),
("min-fresh", secs) => secs.parse().map(MinFresh).map_err(|x| Some(x)),
("s-maxage", secs) => secs.parse().map(SMaxAge).map_err(|x| Some(x)),
(left, right) => Ok(Extension(left.to_string(), Some(right.to_string())))
},
Some(_) => None,
None => Some(Extension(s.to_string(), None))
Some(_) => Err(None),
None => Ok(Extension(s.to_string(), None))
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/header/common/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ pub enum ConnectionOption {
}

impl FromStr for ConnectionOption {
fn from_str(s: &str) -> Option<ConnectionOption> {
type Err = ();
fn from_str(s: &str) -> Result<ConnectionOption, ()> {
match s {
"keep-alive" => Some(KeepAlive),
"close" => Some(Close),
s => Some(ConnectionHeader(UniCase(s.to_string())))
"keep-alive" => Ok(KeepAlive),
"close" => Ok(Close),
s => Ok(ConnectionHeader(UniCase(s.to_string())))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/header/common/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ impl Header for Cookie {
Ok(cookies_str) => {
for cookie_str in cookies_str.split(';') {
match cookie_str.trim().parse() {
Some(cookie) => cookies.push(cookie),
None => return None
Ok(cookie) => cookies.push(cookie),
Err(_) => return None
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions src/header/common/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ impl HeaderFormat for Date {
}

impl FromStr for Date {
fn from_str(s: &str) -> Option<Date> {
tm_from_str(s).map(Date)
type Err = ();
fn from_str(s: &str) -> Result<Date, ()> {
tm_from_str(s).map(Date).ok_or(())
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/header/common/expires.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ impl HeaderFormat for Expires {
}

impl FromStr for Expires {
fn from_str(s: &str) -> Option<Expires> {
tm_from_str(s).map(Expires)
type Err = ();
fn from_str(s: &str) -> Result<Expires, ()> {
tm_from_str(s).map(Expires).ok_or(())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/header/common/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Header for Host {
};

let port = match idx {
Some(idx) => s[idx + 1..].parse(),
Some(idx) => s[idx + 1..].parse().ok(),
None => None
};

Expand Down
5 changes: 3 additions & 2 deletions src/header/common/if_modified_since.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ impl HeaderFormat for IfModifiedSince {
}

impl FromStr for IfModifiedSince {
fn from_str(s: &str) -> Option<IfModifiedSince> {
tm_from_str(s).map(IfModifiedSince)
type Err = ();
fn from_str(s: &str) -> Result<IfModifiedSince, ()> {
tm_from_str(s).map(IfModifiedSince).ok_or(())
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/header/common/last_modified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ impl HeaderFormat for LastModified {
}

impl FromStr for LastModified {
fn from_str(s: &str) -> Option<LastModified> {
tm_from_str(s).map(LastModified)
type Err = ();
fn from_str(s: &str) -> Result<LastModified, ()> {
tm_from_str(s).map(LastModified).ok_or(())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/header/common/set_cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ impl Header for SetCookie {
match from_utf8(&set_cookies_raw[]) {
Ok(s) if !s.is_empty() => {
match s.parse() {
Some(cookie) => set_cookies.push(cookie),
None => ()
Ok(cookie) => set_cookies.push(cookie),
Err(_) => ()
}
},
_ => ()
Expand Down
7 changes: 4 additions & 3 deletions src/header/common/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ pub enum Protocol {
}

impl FromStr for Protocol {
fn from_str(s: &str) -> Option<Protocol> {
type Err = ();
fn from_str(s: &str) -> Result<Protocol, ()> {
if UniCase(s) == UniCase("websocket") {
Some(WebSocket)
Ok(WebSocket)
}
else {
Some(ProtocolExt(s.to_string()))
Ok(ProtocolExt(s.to_string()))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl<'a> fmt::Debug for HeaderView<'a> {
}

impl<'a> Extend<HeaderView<'a>> for Headers {
fn extend<I: Iterator<Item=HeaderView<'a>>>(&mut self, mut iter: I) {
fn extend<I: Iterator<Item=HeaderView<'a>>>(&mut self, iter: I) {
for header in iter {
self.data.insert((*header.0).clone(), (*header.1).clone());
}
Expand Down Expand Up @@ -571,7 +571,7 @@ mod tests {
}
// we JUST checked that raw.len() == 1, so raw[0] WILL exist.
match from_utf8(unsafe { &raw[].get_unchecked(0)[] }) {
Ok(s) => FromStr::from_str(s),
Ok(s) => FromStr::from_str(s).ok(),
Err(_) => None
}.map(|u| CrazyLength(Some(false), u))
}
Expand Down
4 changes: 2 additions & 2 deletions src/header/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn from_one_raw_str<T: str::FromStr>(raw: &[Vec<u8>]) -> Option<T> {
}
// we JUST checked that raw.len() == 1, so raw[0] WILL exist.
match str::from_utf8(&raw[0][]) {
Ok(s) => str::FromStr::from_str(s),
Ok(s) => str::FromStr::from_str(s).ok(),
Err(_) => None
}
}
Expand All @@ -34,7 +34,7 @@ pub fn from_one_comma_delimited<T: str::FromStr>(raw: &[u8]) -> Option<Vec<T>> {
Some(s.as_slice()
.split(',')
.map(|x| x.trim())
.filter_map(str::FromStr::from_str)
.filter_map(|x| x.parse().ok())
.collect())
}
Err(_) => None
Expand Down
15 changes: 8 additions & 7 deletions src/header/shared/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ impl fmt::Display for Encoding {
}

impl str::FromStr for Encoding {
fn from_str(s: &str) -> Option<Encoding> {
type Err = ();
fn from_str(s: &str) -> Result<Encoding, ()> {
match s {
"chunked" => Some(Chunked),
"deflate" => Some(Deflate),
"gzip" => Some(Gzip),
"compress" => Some(Compress),
"identity" => Some(Identity),
_ => Some(EncodingExt(s.to_string()))
"chunked" => Ok(Chunked),
"deflate" => Ok(Deflate),
"gzip" => Ok(Gzip),
"compress" => Ok(Compress),
"identity" => Ok(Identity),
_ => Ok(EncodingExt(s.to_string()))
}
}
}
33 changes: 17 additions & 16 deletions src/header/shared/quality_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ impl<T: fmt::Display> fmt::Display for QualityItem<T> {
}

impl<T: str::FromStr> str::FromStr for QualityItem<T> {
fn from_str(s: &str) -> Option<Self> {
type Err = ();
fn from_str(s: &str) -> Result<Self, ()> {
// Set defaults used if parsing fails.
let mut raw_item = s;
let mut quality = 1f32;
Expand All @@ -49,28 +50,28 @@ impl<T: str::FromStr> str::FromStr for QualityItem<T> {
if start == "q=" || start == "Q=" {
let q_part = &parts[0][2..parts[0].len()];
if q_part.len() > 5 {
return None;
return Err(());
}
let x: Option<f32> = q_part.parse();
let x: Result<f32, _> = q_part.parse();
match x {
Some(q_value) => {
Ok(q_value) => {
if 0f32 <= q_value && q_value <= 1f32 {
quality = q_value;
raw_item = parts[1];
} else {
return None;
return Err(());
}
},
None => return None,
Err(_) => return Err(()),
}
}
}
let x: Option<T> = raw_item.parse();
let x: Result<T, _> = raw_item.parse();
match x {
Some(item) => {
Some(QualityItem{ item: item, quality: quality, })
Ok(item) => {
Ok(QualityItem{ item: item, quality: quality, })
},
None => return None,
Err(_) => return Err(()),
}
}
}
Expand Down Expand Up @@ -103,26 +104,26 @@ fn test_quality_item_show3() {

#[test]
fn test_quality_item_from_str1() {
let x: Option<QualityItem<Encoding>> = "chunked".parse();
let x: Result<QualityItem<Encoding>, ()> = "chunked".parse();
assert_eq!(x.unwrap(), QualityItem{ item: Chunked, quality: 1f32, });
}
#[test]
fn test_quality_item_from_str2() {
let x: Option<QualityItem<Encoding>> = "chunked; q=1".parse();
let x: Result<QualityItem<Encoding>, ()> = "chunked; q=1".parse();
assert_eq!(x.unwrap(), QualityItem{ item: Chunked, quality: 1f32, });
}
#[test]
fn test_quality_item_from_str3() {
let x: Option<QualityItem<Encoding>> = "gzip; q=0.5".parse();
let x: Result<QualityItem<Encoding>, ()> = "gzip; q=0.5".parse();
assert_eq!(x.unwrap(), QualityItem{ item: Gzip, quality: 0.5f32, });
}
#[test]
fn test_quality_item_from_str4() {
let x: Option<QualityItem<Encoding>> = "gzip; q=0.273".parse();
let x: Result<QualityItem<Encoding>, ()> = "gzip; q=0.273".parse();
assert_eq!(x.unwrap(), QualityItem{ item: Gzip, quality: 0.273f32, });
}
#[test]
fn test_quality_item_from_str5() {
let x: Option<QualityItem<Encoding>> = "gzip; q=0.2739999".parse();
assert_eq!(x, None);
let x: Result<QualityItem<Encoding>, ()> = "gzip; q=0.2739999".parse();
assert_eq!(x, Err(()));
}
4 changes: 2 additions & 2 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::borrow::IntoCow;
use std::cmp::min;
use std::old_io::{self, Reader, IoResult, BufWriter};
use std::num::from_u16;
use std::str::{self, FromStr};
use std::str;
use std::string::CowString;

use url::Url;
Expand Down Expand Up @@ -625,7 +625,7 @@ pub fn read_status<R: Reader>(stream: &mut R) -> HttpResult<RawStatus> {
try!(stream.read_byte()),
];

let code = match str::from_utf8(code.as_slice()).ok().and_then(FromStr::from_str) {
let code = match str::from_utf8(code.as_slice()).ok().and_then(|x| x.parse().ok()) {
Some(num) => num,
None => return Err(HttpStatusError)
};
Expand Down
Loading

0 comments on commit 742081c

Please sign in to comment.