chore: add FromStr for Endpoint (#558)
This is particularly handy when combined with `clap::value_t`.
Here's demo of it working with Clap:
```bash
cargo +stable init --bin tonic-demo
cd tonic-demo
cat <<-EOF >> Cargo.toml
clap = "*"
tonic = { path = "../hyperium/tonic/tonic" }
EOF
cat <<-EOF > src/main.rs
use clap::{value_t, App, Arg};
use tonic::transport::Endpoint;
fn main() {
let matches = App::new("tonic-demo")
.arg(Arg::with_name("host"))
.get_matches();
let x = value_t!(matches.value_of("host"), Endpoint);
println!("{:?}", x);
}
EOF
cargo +stable run -- https://127.0.0.1:443
```
Signed-off-by: Ana Hobden <[email protected]>
This commit is contained in:
@@ -13,6 +13,7 @@ use http::{
|
|||||||
use std::{
|
use std::{
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
fmt,
|
fmt,
|
||||||
|
str::FromStr,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
use tower::make::MakeConnection;
|
use tower::make::MakeConnection;
|
||||||
@@ -351,3 +352,11 @@ impl fmt::Debug for Endpoint {
|
|||||||
f.debug_struct("Endpoint").finish()
|
f.debug_struct("Endpoint").finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for Endpoint {
|
||||||
|
type Err = InvalidUri;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
Self::try_from(s.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user