Skip to content

Commit

Permalink
add DnsChainDomainResolveErr
Browse files Browse the repository at this point in the history
  • Loading branch information
parkma99 committed Oct 28, 2024
1 parent fb47c92 commit 5d3d36f
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions rama-dns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

use rama_core::error::BoxError;
use rama_net::address::Domain;
use rama_utils::macros::error::static_str_error;
use std::{
future::Future,
net::{Ipv4Addr, Ipv6Addr},
Expand Down Expand Up @@ -80,34 +79,55 @@ impl<R: DnsResolver<Error: Into<BoxError>>> DnsResolver for Option<R> {
}
}

static_str_error! {
#[doc = "domain not mapped in any dns resolver"]
pub struct DomainNotFoundErr;
#[derive(Debug)]
pub struct DnsChainDomainResolveErr<E: 'static> {
errors: Vec<E>,
}
impl<E: std::fmt::Debug + Send> std::fmt::Display for DnsChainDomainResolveErr<E> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self.errors)
}
}

impl<E: std::fmt::Debug + Send + std::error::Error> std::error::Error
for DnsChainDomainResolveErr<E>
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if self.errors.is_empty() {
None
} else {
Some(&self.errors[0])
}
}
}

impl<R: DnsResolver> DnsResolver for Vec<R> {
type Error = DomainNotFoundErr;
impl<R, E> DnsResolver for Vec<R>
where
R: DnsResolver<Error = E> + Send,
E: Send + 'static,
{
type Error = DnsChainDomainResolveErr<E>;

async fn ipv4_lookup(&self, domain: Domain) -> Result<Vec<Ipv4Addr>, Self::Error> {
let resolvers = self.iter().map(Arc::new).collect::<Vec<_>>();
for resolver in resolvers.iter() {
let mut errors = Vec::new();
for resolver in self {
match resolver.ipv4_lookup(domain.clone()).await {
Ok(ipv4s) => return Ok(ipv4s),
Err(_e) => continue,
Err(err) => errors.push(err),
}
}
Err(DomainNotFoundErr)
Err(DnsChainDomainResolveErr { errors })
}

async fn ipv6_lookup(&self, domain: Domain) -> Result<Vec<Ipv6Addr>, Self::Error> {
let resolvers = self.iter().map(Arc::new).collect::<Vec<_>>();
for resolver in resolvers.iter() {
let mut errors = Vec::new();
for resolver in self {
match resolver.ipv6_lookup(domain.clone()).await {
Ok(ipv6s) => return Ok(ipv6s),
Err(_e) => continue,
Err(err) => errors.push(err),
}
}
Err(DomainNotFoundErr)
Err(DnsChainDomainResolveErr { errors })
}
}

Expand Down

0 comments on commit 5d3d36f

Please sign in to comment.