use std::{future::Future, net::SocketAddr, pin::Pin};
use crate::Error;
pub trait Resolver: Send {
fn resolve(
&self,
host: &str,
port: u16,
) -> Pin<Box<dyn Future<Output = Result<SocketAddr, Error>> + Send>>;
}
pub struct Gai;
impl Resolver for Gai {
fn resolve(
&self,
host: &str,
port: u16,
) -> Pin<Box<dyn Future<Output = Result<SocketAddr, Error>> + Send>> {
let host = host.to_owned();
Box::pin(async move {
tokio::net::lookup_host((host, port))
.await
.map_err(|_| Error::CannotResolveHost)?
.next()
.ok_or(Error::CannotResolveHost)
})
}
}