Skip to content

Commit

Permalink
chore: add FromStr for Endpoint (#558)
Browse files Browse the repository at this point in the history
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 <operator@hoverbear.org>
  • Loading branch information
Hoverbear committed Feb 13, 2021
1 parent 4f5e160 commit f49d4bd
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tonic/src/transport/channel/endpoint.rs
Expand Up @@ -13,6 +13,7 @@ use http::{
use std::{
convert::{TryFrom, TryInto},
fmt,
str::FromStr,
time::Duration,
};
use tower::make::MakeConnection;
Expand Down Expand Up @@ -351,3 +352,11 @@ impl fmt::Debug for Endpoint {
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())
}
}

0 comments on commit f49d4bd

Please sign in to comment.