From f49d4bdf99461265bce3e5ce667bc6a69d917c16 Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Sat, 13 Feb 2021 11:49:10 -0800 Subject: [PATCH] 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 --- tonic/src/transport/channel/endpoint.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tonic/src/transport/channel/endpoint.rs b/tonic/src/transport/channel/endpoint.rs index 13eb4b9..fb9d0c4 100644 --- a/tonic/src/transport/channel/endpoint.rs +++ b/tonic/src/transport/channel/endpoint.rs @@ -13,6 +13,7 @@ use http::{ use std::{ convert::{TryFrom, TryInto}, fmt, + str::FromStr, time::Duration, }; use tower::make::MakeConnection; @@ -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::try_from(s.to_string()) + } +}