From d67f6c514af307aa1a23c149e99600cd12544126 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 5 Dec 2023 17:37:07 +0100 Subject: [PATCH] Add a TryFrom for ServerName<'static> impl --- src/server_name.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/server_name.rs b/src/server_name.rs index 1b08c1c..97db331 100644 --- a/src/server_name.rs +++ b/src/server_name.rs @@ -24,6 +24,15 @@ use std::error::Error as StdError; /// # use rustls_pki_types::ServerName; /// ServerName::try_from("example.com").expect("invalid DNS name"); /// +/// If you have an owned `String`, you can use `TryFrom` directly: +/// +/// ``` +/// let name = "example.com".to_string(); +/// ServerName::try_from(name).expect("invalid DNS name"); +/// ``` +/// +/// which will yield a `ServerName<'static>` if successful. +/// /// // or, alternatively... /// /// let x = "example.com".try_into().expect("invalid DNS name"); @@ -74,6 +83,21 @@ impl<'a> fmt::Debug for ServerName<'a> { } } +#[cfg(feature = "alloc")] +impl TryFrom for ServerName<'static> { + type Error = InvalidDnsNameError; + + fn try_from(value: String) -> Result { + match DnsName::try_from_string(value) { + Ok(dns) => Ok(Self::DnsName(dns)), + Err(value) => match IpAddr::try_from(value.as_str()) { + Ok(ip) => Ok(Self::IpAddress(ip)), + Err(_) => Err(InvalidDnsNameError), + }, + } + } +} + impl<'a> TryFrom<&'a [u8]> for ServerName<'a> { type Error = InvalidDnsNameError; @@ -129,6 +153,14 @@ impl<'a> DnsName<'a> { Self(DnsNameInner::Owned(s)) => s.clone(), })) } + + #[cfg(feature = "alloc")] + fn try_from_string(s: String) -> Result { + match validate(s.as_bytes()) { + Ok(_) => Ok(Self(DnsNameInner::Owned(s))), + Err(_) => Err(s), + } + } } #[cfg(feature = "alloc")] @@ -136,8 +168,7 @@ impl TryFrom for DnsName<'static> { type Error = InvalidDnsNameError; fn try_from(value: String) -> Result { - validate(value.as_bytes())?; - Ok(Self(DnsNameInner::Owned(value))) + Self::try_from_string(value).map_err(|_| InvalidDnsNameError) } }