Merge pull request #292 from hyperium/rustup
fix(rustup): update FromStr
This commit is contained in:
		| @@ -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 { | ||||
| @@ -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(()) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|   | ||||
| @@ -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)) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|   | ||||
| @@ -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()))) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
|                         } | ||||
|                     } | ||||
|                 }, | ||||
|   | ||||
| @@ -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(()) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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(()) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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 | ||||
|             }; | ||||
|  | ||||
|   | ||||
| @@ -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(()) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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(()) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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(_) => () | ||||
|                     } | ||||
|                 }, | ||||
|                 _ => () | ||||
|   | ||||
| @@ -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())) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -359,7 +359,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()); | ||||
|         } | ||||
| @@ -581,7 +581,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)) | ||||
|         } | ||||
|   | ||||
| @@ -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 | ||||
|     } | ||||
| } | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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())) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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; | ||||
| @@ -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(()), | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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(())); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user