Skip to content

Commit

Permalink
fix: examples
Browse files Browse the repository at this point in the history
  • Loading branch information
PanGan21 committed May 18, 2024
1 parent 1a3732a commit cda394e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
3 changes: 2 additions & 1 deletion examples/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use wait_for_rs::WaitService;

fn main() {
let urls = ["google.com:443".to_string(), "github.com:443".to_string()].to_vec();
// let urls = ["google.com:443".to_string(), "github.com:443".to_string()].to_vec();
let urls = ["google.com:443".to_string()].to_vec();
let timeout = 10;

let wait_service = WaitService::new(urls, timeout).unwrap();
Expand Down
46 changes: 30 additions & 16 deletions src/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,29 @@ impl WaitService {
fn resolve_address(url: &str) -> Result<SocketAddr> {
// Try parsing the input as a URL
if let Ok(parsed_url) = Url::parse(url) {
// Check if the URL includes a port
let port = match parsed_url.port() {
Some(port) => port,
None => get_default_port(&parsed_url)?,
};

let host = parsed_url
.host_str()
.ok_or(WaitServiceError::UrlNotParsed)?;
if parsed_url.has_host() {
// Check if the URL includes a port
let port = match parsed_url.port() {
Some(port) => port,
None => get_default_port(&parsed_url)?,
};

// Construct the address
let addr = socket_addr_from_host_and_port(&host, port)?;
let host = parsed_url
.host_str()
.ok_or(WaitServiceError::UrlNotParsed)?;

Ok(addr)
} else {
// If parsing as URL fails, try parsing as TCP address
let addr = socket_addr_from_tcp(url)?;
// Construct the address
let addr = socket_addr_from_host_and_port(&host, port)?;

Ok(addr)
return Ok(addr);
} else {
let addr = socket_addr_from_url(url)?;
return Ok(addr);
}
}
let addr = socket_addr_from_tcp(url)?;

return Ok(addr);
}

/// Default port based on scheme
Expand All @@ -113,6 +116,17 @@ fn socket_addr_from_host_and_port(host: &str, port: u16) -> Result<SocketAddr> {
Ok(addr)
}

/// Construct a `SocketAddr` from a url
fn socket_addr_from_url(url: &str) -> Result<SocketAddr> {
let addr = url
.to_socket_addrs()
.map_err(|e| WaitServiceError::Io(e))?
.next()
.ok_or(WaitServiceError::UrlNotParsed)?;

Ok(addr)
}

/// Construct a `SocketAddr` from a tcp address
fn socket_addr_from_tcp(address: &str) -> Result<SocketAddr> {
let addr = address
Expand Down

0 comments on commit cda394e

Please sign in to comment.