Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<ServerName as TryFrom<_>> tweaks #21

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustls-pki-types"
version = "1.0.0"
version = "1.0.1"
edition = "2021"
rust-version = "1.60"
license = "MIT OR Apache-2.0"
Expand Down
41 changes: 36 additions & 5 deletions src/server_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ 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:
///
/// ```
/// # use rustls_pki_types::ServerName;
/// let name = "example.com".to_string();
/// #[cfg(feature = "alloc")]
/// ServerName::try_from(name).expect("invalid DNS name");
/// ```
///
/// which will yield a `ServerName<'static>` if successful.
///
/// // or, alternatively...
///
Expand Down Expand Up @@ -74,6 +86,21 @@ impl<'a> fmt::Debug for ServerName<'a> {
}
}

#[cfg(feature = "alloc")]
impl TryFrom<String> for ServerName<'static> {
cpu marked this conversation as resolved.
Show resolved Hide resolved
type Error = InvalidDnsNameError;

fn try_from(value: String) -> Result<Self, Self::Error> {
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;

Expand All @@ -91,13 +118,10 @@ impl<'a> TryFrom<&'a str> for ServerName<'a> {
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
match DnsName::try_from(s) {
Ok(dns) => Ok(Self::DnsName(dns)),
#[cfg(feature = "std")]
Err(InvalidDnsNameError) => match IpAddr::try_from(s) {
Ok(ip) => Ok(Self::IpAddress(ip)),
Err(_) => Err(InvalidDnsNameError),
},
#[cfg(not(feature = "std"))]
Err(InvalidDnsNameError) => Err(InvalidDnsNameError),
}
}
}
Expand Down Expand Up @@ -132,15 +156,22 @@ impl<'a> DnsName<'a> {
Self(DnsNameInner::Owned(s)) => s.clone(),
}))
}

#[cfg(feature = "alloc")]
fn try_from_string(s: String) -> Result<Self, String> {
match validate(s.as_bytes()) {
Ok(_) => Ok(Self(DnsNameInner::Owned(s))),
Err(_) => Err(s),
}
}
}

#[cfg(feature = "alloc")]
impl TryFrom<String> for DnsName<'static> {
type Error = InvalidDnsNameError;

fn try_from(value: String) -> Result<Self, Self::Error> {
validate(value.as_bytes())?;
Ok(Self(DnsNameInner::Owned(value)))
Self::try_from_string(value).map_err(|_| InvalidDnsNameError)
}
}

Expand Down