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

RUST-892 implement FromStr for ServerAddress #458

Merged
merged 3 commits into from Sep 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/client/options/mod.rs
Expand Up @@ -168,6 +168,13 @@ impl Hash for ServerAddress {
}
}

impl FromStr for ServerAddress {
type Err = crate::error::Error;
fn from_str(address: &str) -> Result<Self> {
ServerAddress::parse(address)
}
}

impl ServerAddress {
/// Parses an address string into a `StreamAddress`.
pub fn parse(address: impl AsRef<str>) -> Result<Self> {
Expand Down Expand Up @@ -1994,6 +2001,23 @@ mod tests {
}
}

#[test]
fn test_parse_address_with_from_str() {
let x = "localhost:27017".parse::<ServerAddress>().unwrap();
let ServerAddress::Tcp { host, port } = x;
assert_eq!(host, "localhost");
assert_eq!(port, Some(27017));

// Port defaults to 27017 (so this doesn't fail)
let x = "localhost".parse::<ServerAddress>().unwrap();
let ServerAddress::Tcp { host, port } = x;
assert_eq!(host, "localhost");
assert_eq!(port, None);

let x = "localhost:not a number".parse::<ServerAddress>();
assert!(x.is_err());
}

#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
async fn fails_without_scheme() {
Expand Down