|
7 | 7 | //! - The [`Connect`](Connect) trait and related types to build custom connectors.
|
8 | 8 | use std::error::Error as StdError;
|
9 | 9 | use std::{fmt, mem};
|
| 10 | +use std::convert::TryFrom; |
10 | 11 |
|
11 | 12 | use bytes::{BufMut, Bytes, BytesMut};
|
12 | 13 | use futures::Future;
|
@@ -251,6 +252,17 @@ impl Destination {
|
251 | 252 | */
|
252 | 253 | }
|
253 | 254 |
|
| 255 | +#[cfg(try_from)] |
| 256 | +impl TryFrom<Uri> for Destination { |
| 257 | + type Error = ::error::Error; |
| 258 | + |
| 259 | + fn try_from(uri: Uri) -> Result<Self, Self::Error> { |
| 260 | + uri.authority_part().ok_or(::error::Parse::Uri)?; |
| 261 | + uri.scheme_part().ok_or(::error::Parse::Uri)?; |
| 262 | + Ok(Destination { uri }) |
| 263 | + } |
| 264 | +} |
| 265 | + |
254 | 266 | impl Connected {
|
255 | 267 | /// Create new `Connected` type with empty metadata.
|
256 | 268 | pub fn new() -> Connected {
|
@@ -381,7 +393,7 @@ where
|
381 | 393 |
|
382 | 394 | #[cfg(test)]
|
383 | 395 | mod tests {
|
384 |
| - use super::{Connected, Destination}; |
| 396 | + use super::{Connected, Destination, TryFrom}; |
385 | 397 |
|
386 | 398 | #[test]
|
387 | 399 | fn test_destination_set_scheme() {
|
@@ -527,6 +539,22 @@ mod tests {
|
527 | 539 | assert_eq!(dst.port(), None);
|
528 | 540 | }
|
529 | 541 |
|
| 542 | + #[cfg(try_from)] |
| 543 | + #[test] |
| 544 | + fn test_try_from_destination() { |
| 545 | + let uri: http::Uri = "http://hyper.rs".parse().expect("initial parse"); |
| 546 | + let result = Destination::try_from(uri); |
| 547 | + assert_eq!(result.is_ok(), true); |
| 548 | + } |
| 549 | + |
| 550 | + #[cfg(try_from)] |
| 551 | + #[test] |
| 552 | + fn test_try_from_no_scheme() { |
| 553 | + let uri: http::Uri = "hyper.rs".parse().expect("initial parse error"); |
| 554 | + let result = Destination::try_from(uri); |
| 555 | + assert_eq!(result.is_err(), true); |
| 556 | + } |
| 557 | + |
530 | 558 | #[derive(Clone, Debug, PartialEq)]
|
531 | 559 | struct Ex1(usize);
|
532 | 560 |
|
|
0 commit comments