aws_smithy_runtime_api/client/
dns.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Interfaces for resolving DNS
7
8use crate::box_error::BoxError;
9use crate::impl_shared_conversions;
10use std::error::Error as StdError;
11use std::fmt;
12use std::net::IpAddr;
13use std::sync::Arc;
14
15/// Error that occurs when failing to perform a DNS lookup.
16#[derive(Debug)]
17pub struct ResolveDnsError {
18    source: BoxError,
19}
20
21impl ResolveDnsError {
22    /// Creates a new `DnsLookupFailed` error.
23    pub fn new(source: impl Into<BoxError>) -> Self {
24        ResolveDnsError {
25            source: source.into(),
26        }
27    }
28}
29
30impl fmt::Display for ResolveDnsError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "failed to perform DNS lookup")
33    }
34}
35
36impl StdError for ResolveDnsError {
37    fn source(&self) -> Option<&(dyn StdError + 'static)> {
38        Some(&*self.source as _)
39    }
40}
41
42new_type_future! {
43    #[doc = "New-type for the future returned by the [`ResolveDns`] trait."]
44    pub struct DnsFuture<'a, Vec<IpAddr>, ResolveDnsError>;
45}
46
47/// Trait for resolving domain names
48pub trait ResolveDns: fmt::Debug + Send + Sync {
49    /// Asynchronously resolve the given domain name
50    fn resolve_dns<'a>(&'a self, name: &'a str) -> DnsFuture<'a>;
51}
52
53/// Shared instance of [`ResolveDns`].
54#[derive(Clone, Debug)]
55pub struct SharedDnsResolver(Arc<dyn ResolveDns>);
56
57impl SharedDnsResolver {
58    /// Create a new `SharedDnsResolver`.
59    pub fn new(resolver: impl ResolveDns + 'static) -> Self {
60        Self(Arc::new(resolver))
61    }
62}
63
64impl ResolveDns for SharedDnsResolver {
65    fn resolve_dns<'a>(&'a self, name: &'a str) -> DnsFuture<'a> {
66        self.0.resolve_dns(name)
67    }
68}
69
70impl_shared_conversions!(convert SharedDnsResolver from ResolveDns using SharedDnsResolver::new);
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn check_send() {
78        fn is_send<T: Send>() {}
79        is_send::<DnsFuture<'_>>();
80    }
81}