use core::future::Future;
use embassy_net::{Stack, dns::DnsQueryType};
pub use embassy_net::IpAddress;
pub type Addresses = heapless::Vec<IpAddress, 4>;
pub trait Dns {
type Error;
fn resolve(&mut self, hostname: &str) -> impl Future<Output = Result<Addresses, Self::Error>>;
}
pub struct DnsWithStack<'a> {
stack: Stack<'a>,
}
impl<'a> DnsWithStack<'a> {
pub const fn new(stack: Stack<'a>) -> Self {
Self { stack }
}
}
impl Dns for DnsWithStack<'_> {
type Error = embassy_net::dns::Error;
async fn resolve(&mut self, hostname: &str) -> Result<Addresses, Self::Error> {
let resolved_addresses = self.stack.dns_query(hostname, DnsQueryType::A).await?;
let mut addresses = Addresses::new();
for address in resolved_addresses {
if addresses.push(address).is_err() {
break;
}
}
Ok(addresses)
}
}