Repository: seanmonstar/reqwest (language: rust)

Issue:
FromStr trait implementation for Name
Would be useful for testing


Discussion / hints:

Fix patch:
diff --git a/src/dns/resolve.rs b/src/dns/resolve.rs
index 1300edd51..6ecd3e08a 100644
--- a/src/dns/resolve.rs
+++ b/src/dns/resolve.rs
@@ -5,6 +5,7 @@ use std::collections::HashMap;
 use std::future::Future;
 use std::net::SocketAddr;
 use std::pin::Pin;
+use std::str::FromStr;
 use std::sync::Arc;
 use std::task::{Context, Poll};
 
@@ -40,6 +41,16 @@ impl Name {
     }
 }
 
+impl FromStr for Name {
+    type Err = sealed::InvalidNameError;
+
+    fn from_str(host: &str) -> Result<Self, Self::Err> {
+        HyperName::from_str(host.into())
+            .map(Name)
+            .map_err(|_| sealed::InvalidNameError { _ext: () })
+    }
+}
+
 #[derive(Clone)]
 pub(crate) struct DynResolver {
     resolver: Arc<dyn Resolve>,
@@ -93,3 +104,20 @@ impl Resolve for DnsResolverWithOverrides {
         }
     }
 }
+
+mod sealed {
+    use std::fmt;
+
+    #[derive(Debug)]
+    pub struct InvalidNameError {
+        pub(super) _ext: (),
+    }
+
+    impl fmt::Display for InvalidNameError {
+        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+            f.write_str("invalid DNS name")
+        }
+    }
+
+    impl std::error::Error for InvalidNameError {}
+}


Test patch: