1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
mod service_name;
pub use service_name::{InvalidServiceName, ServiceName};

mod endpoints;
pub use endpoints::{Endpoints, EndpointsBuilder};

mod regions_provider_endpoints;
pub use regions_provider_endpoints::RegionsProviderEndpoints;

mod bucket_domains_provider;
pub use bucket_domains_provider::{BucketDomainsProvider, BucketDomainsQueryer, BucketDomainsQueryerBuilder};

mod endpoints_cache;

use super::{super::ApiResult, Endpoint};
use auto_impl::auto_impl;
use dyn_clonable::clonable;
use std::{borrow::Cow, fmt, mem::take};

#[cfg(feature = "async")]
type BoxFuture<'a, T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + 'a + Send>>;

/// 终端地址列表获取接口
///
/// 同时提供阻塞获取接口和异步获取接口,异步获取接口则需要启用 `async` 功能
#[clonable]
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait EndpointsProvider: Clone + fmt::Debug + Send + Sync {
    /// 获取终端地址列表
    ///
    /// 该方法的异步版本为 [`Self::async_get_endpoints`]。
    fn get_endpoints<'e>(&'e self, options: GetOptions<'_>) -> ApiResult<Cow<'e, Endpoints>>;

    /// 异步获取终端地址列表
    #[inline]
    #[cfg(feature = "async")]
    #[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
    fn async_get_endpoints<'a>(&'a self, options: GetOptions<'a>) -> BoxFuture<'a, ApiResult<Cow<'a, Endpoints>>> {
        Box::pin(async move { self.get_endpoints(options) })
    }
}

/// 获取终端地址列表的选项
#[derive(Copy, Clone, Debug, Default)]
pub struct GetOptions<'a> {
    service_names: &'a [ServiceName],
}

impl<'a> GetOptions<'a> {
    /// 创建获取终端地址列表的选项构建器
    #[inline]
    pub fn builder() -> GetOptionsBuilder<'a> {
        Default::default()
    }

    /// 获取服务列表
    #[inline]
    pub fn service_names(&'a self) -> &'a [ServiceName] {
        self.service_names
    }
}

/// 获取终端地址列表的选项构建器
#[derive(Clone, Debug, Default)]
pub struct GetOptionsBuilder<'a>(GetOptions<'a>);

impl<'a> GetOptionsBuilder<'a> {
    /// 设置服务列表
    #[inline]
    pub fn service_names(&mut self, service_names: &'a [ServiceName]) -> &mut Self {
        self.0.service_names = service_names;
        self
    }

    /// 构建获取终端地址列表的选项
    #[inline]
    pub fn build(&mut self) -> GetOptions<'a> {
        take(&mut self.0)
    }
}

impl EndpointsProvider for Endpoint {
    #[inline]
    fn get_endpoints<'e>(&'e self, _services: GetOptions<'_>) -> ApiResult<Cow<'e, Endpoints>> {
        Ok(Cow::Owned(Endpoints::builder(self.to_owned()).build()))
    }
}

impl EndpointsProvider for Endpoints {
    #[inline]
    fn get_endpoints<'e>(&'e self, _services: GetOptions<'_>) -> ApiResult<Cow<'e, Endpoints>> {
        Ok(Cow::Borrowed(self))
    }
}