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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
use super::{
    super::{cache::IsCacheValid, ApiResult},
    Region,
};
use auto_impl::auto_impl;
use dyn_clonable::clonable;
use serde::{Deserialize, Serialize};
use std::{
    error::Error,
    fmt::{self, Debug, Display},
    ops::{Deref, DerefMut},
    time::{Duration, SystemTime},
};

mod regions_cache;

mod bucket_regions_queryer;
pub use bucket_regions_queryer::{BucketRegionsProvider, BucketRegionsQueryer, BucketRegionsQueryerBuilder};

mod all_regions_provider;
pub use all_regions_provider::{AllRegionsProvider, AllRegionsProviderBuilder};

mod static_regions_provider;
pub use static_regions_provider::StaticRegionsProvider;

mod structs;

#[cfg(feature = "async")]
use futures::future::BoxFuture;

/// 区域信息获取接口
///
/// 可以获取一个区域也可以获取多个区域
///
/// 同时提供阻塞获取接口和异步获取接口,异步获取接口则需要启用 `async` 功能
#[clonable]
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait RegionsProvider: Clone + Debug + Sync + Send {
    /// 返回七牛区域信息
    ///
    /// 该方法的异步版本为 [`Self::async_get`]。
    fn get(&self, opts: GetOptions) -> ApiResult<GotRegion>;

    /// 返回多个七牛区域信息
    ///
    /// 该方法的异步版本为 [`Self::async_get_all`]。
    #[inline]
    fn get_all(&self, opts: GetOptions) -> ApiResult<GotRegions> {
        let region = self.get(opts)?.into_region();
        Ok(vec![region].into())
    }

    /// 异步返回七牛区域信息
    #[inline]
    #[cfg(feature = "async")]
    #[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
    fn async_get(&self, opts: GetOptions) -> BoxFuture<'_, ApiResult<GotRegion>> {
        Box::pin(async move { self.get(opts) })
    }

    /// 异步返回多个七牛区域信息
    #[inline]
    #[cfg(feature = "async")]
    #[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
    fn async_get_all(&self, opts: GetOptions) -> BoxFuture<'_, ApiResult<GotRegions>> {
        Box::pin(async move { self.get_all(opts) })
    }
}

/// 获取区域信息的选项
#[derive(Copy, Clone, Debug, Default)]
pub struct GetOptions {}

/// 获取的区域信息
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GotRegion {
    region: Region,
    lifetime: Option<Duration>,
    got_at: SystemTime,
}

impl From<GotRegion> for Region {
    #[inline]
    fn from(result: GotRegion) -> Self {
        result.region
    }
}

impl From<Region> for GotRegion {
    #[inline]
    fn from(region: Region) -> Self {
        Self {
            region,
            got_at: SystemTime::now(),
            lifetime: None,
        }
    }
}

impl GotRegion {
    /// 获取的区域信息
    #[inline]
    pub fn region(&self) -> &Region {
        &self.region
    }

    /// 获取的区域信息的可变引用
    #[inline]
    pub fn region_mut(&mut self) -> &mut Region {
        &mut self.region
    }

    /// 获取的生命周期
    #[inline]
    pub fn lifetime(&self) -> Option<Duration> {
        self.lifetime
    }

    /// 获取的生命周期的可变引用
    #[inline]
    pub fn lifetime_mut(&mut self) -> &mut Option<Duration> {
        &mut self.lifetime
    }

    /// 转换为区域信息
    #[inline]
    pub fn into_region(self) -> Region {
        self.region
    }
}

impl IsCacheValid for GotRegion {
    fn is_valid(&self) -> bool {
        if let Some(lifetime) = self.lifetime {
            if let Ok(elapsed) = self.got_at.elapsed() {
                elapsed <= lifetime
            } else {
                false // 如果发生时间倒流,则立即判定为 INVALID
            }
        } else {
            true
        }
    }
}

impl PartialEq for GotRegion {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.region == other.region
    }
}

impl Eq for GotRegion {}

impl Deref for GotRegion {
    type Target = Region;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.region()
    }
}

impl DerefMut for GotRegion {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.region_mut()
    }
}

impl RegionsProvider for Region {
    #[inline]
    fn get(&self, _opts: GetOptions) -> ApiResult<GotRegion> {
        Ok(self.to_owned().into())
    }
}

impl RegionsProvider for GotRegion {
    #[inline]
    fn get(&self, _opts: GetOptions) -> ApiResult<GotRegion> {
        Ok(self.to_owned())
    }
}

/// 获取的区域列表信息
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GotRegions {
    regions: Vec<Region>,
    lifetime: Option<Duration>,
    got_at: SystemTime,
}

impl From<GotRegions> for Vec<Region> {
    #[inline]
    fn from(results: GotRegions) -> Self {
        results.regions
    }
}

impl From<Vec<Region>> for GotRegions {
    #[inline]
    fn from(regions: Vec<Region>) -> Self {
        Self {
            regions,
            got_at: SystemTime::now(),
            lifetime: None,
        }
    }
}

impl FromIterator<Region> for GotRegions {
    #[inline]
    fn from_iter<T: IntoIterator<Item = Region>>(iter: T) -> Self {
        Vec::from_iter(iter).into()
    }
}

impl Extend<Region> for GotRegions {
    #[inline]
    fn extend<T: IntoIterator<Item = Region>>(&mut self, iter: T) {
        self.regions.extend(iter)
    }
}

impl<'a> IntoIterator for &'a GotRegions {
    type Item = &'a Region;
    type IntoIter = std::slice::Iter<'a, Region>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.regions.iter()
    }
}

impl IntoIterator for GotRegions {
    type Item = Region;
    type IntoIter = std::vec::IntoIter<Region>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.regions.into_iter()
    }
}

impl GotRegions {
    /// 获取的区域信息列表
    #[inline]
    pub fn regions(&self) -> &[Region] {
        &self.regions
    }

    /// 获取的区域信息列表的可变引用
    #[inline]
    pub fn regions_mut(&mut self) -> &mut Vec<Region> {
        &mut self.regions
    }

    /// 获取的生命周期
    #[inline]
    pub fn lifetime(&self) -> Option<Duration> {
        self.lifetime
    }

    /// 获取的生命周期的可变引用
    #[inline]
    pub fn lifetime_mut(&mut self) -> &mut Option<Duration> {
        &mut self.lifetime
    }

    /// 转换为区域信息列表
    #[inline]
    pub fn into_regions(self) -> Vec<Region> {
        self.regions
    }
}

impl IsCacheValid for GotRegions {
    #[inline]
    fn is_valid(&self) -> bool {
        if let Some(lifetime) = self.lifetime {
            if let Ok(elapsed) = self.got_at.elapsed() {
                elapsed <= lifetime
            } else {
                false // 如果发生时间倒流,则立即判定为 INVALID
            }
        } else {
            true
        }
    }
}

impl PartialEq for GotRegions {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.regions == other.regions
    }
}

impl Eq for GotRegions {}

impl AsRef<[Region]> for GotRegions {
    #[inline]
    fn as_ref(&self) -> &[Region] {
        self.regions()
    }
}

impl AsMut<[Region]> for GotRegions {
    #[inline]
    fn as_mut(&mut self) -> &mut [Region] {
        self.regions_mut()
    }
}

impl Deref for GotRegions {
    type Target = [Region];

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.regions()
    }
}

impl DerefMut for GotRegions {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.regions_mut()
    }
}

impl RegionsProvider for GotRegions {
    #[inline]
    fn get(&self, opts: GetOptions) -> ApiResult<GotRegion> {
        self.get_all(opts).map(|regions| {
            regions
                .into_regions()
                .into_iter()
                .next()
                .expect("Regions are empty")
                .into()
        })
    }

    #[inline]
    fn get_all(&self, _opts: GetOptions) -> ApiResult<GotRegions> {
        Ok(self.to_owned())
    }
}

#[derive(Clone, Copy, Debug)]
pub struct EmptyRegionError;

impl Display for EmptyRegionError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&"regions must not be empty", f)
    }
}

impl Error for EmptyRegionError {}

impl TryFrom<GotRegions> for GotRegion {
    type Error = EmptyRegionError;

    fn try_from(value: GotRegions) -> Result<Self, Self::Error> {
        if let Some(region) = value.regions.into_iter().next() {
            Ok(Self {
                region,
                lifetime: value.lifetime,
                got_at: value.got_at,
            })
        } else {
            Err(EmptyRegionError)
        }
    }
}

impl From<GotRegion> for GotRegions {
    #[inline]
    fn from(value: GotRegion) -> Self {
        Self {
            regions: vec![value.region],
            lifetime: value.lifetime,
            got_at: value.got_at,
        }
    }
}