qiniu_download/config/
configurable.rs1use super::{
2 multi_clusters::MultipleClustersConfig,
3 single_cluster::{Config, SingleClusterConfig},
4 ClustersConfigParseError, Timeouts,
5};
6use std::{
7 collections::HashSet,
8 path::{Path, PathBuf},
9};
10
11#[derive(Debug, Clone)]
13pub struct Configurable(ConfigurableInner);
14
15#[allow(clippy::large_enum_variant)]
16#[derive(Debug, Clone)]
17enum ConfigurableInner {
18 Single(SingleClusterConfig),
19 Multi(MultipleClustersConfig),
20}
21
22impl Configurable {
23 #[inline]
25 pub fn new_single(config: SingleClusterConfig) -> Self {
26 Self(ConfigurableInner::Single(config))
27 }
28
29 #[inline]
31 pub fn as_single(&self) -> Option<&SingleClusterConfig> {
32 if let ConfigurableInner::Single(single) = &self.0 {
33 Some(single)
34 } else {
35 None
36 }
37 }
38
39 #[inline]
41 pub fn as_single_mut(&mut self) -> Option<&mut SingleClusterConfig> {
42 if let ConfigurableInner::Single(single) = &mut self.0 {
43 Some(single)
44 } else {
45 None
46 }
47 }
48
49 #[inline]
51 pub fn new_multi(config: MultipleClustersConfig) -> Self {
52 Self(ConfigurableInner::Multi(config))
53 }
54
55 #[inline]
57 pub fn as_multi(&self) -> Option<&MultipleClustersConfig> {
58 if let ConfigurableInner::Multi(multi) = &self.0 {
59 Some(multi)
60 } else {
61 None
62 }
63 }
64
65 #[inline]
67 pub fn as_multi_mut(&mut self) -> Option<&mut MultipleClustersConfig> {
68 if let ConfigurableInner::Multi(multi) = &mut self.0 {
69 Some(multi)
70 } else {
71 None
72 }
73 }
74
75 #[inline]
76 pub(crate) fn with_key<T>(&self, key: &str, f: impl FnOnce(&Config) -> T) -> Option<T> {
77 match &self.0 {
78 ConfigurableInner::Single(single) => single.with_key(key, f),
79 ConfigurableInner::Multi(multi) => multi.with_key(key, f),
80 }
81 }
82
83 #[inline]
84 pub(super) fn parse(
85 path: impl AsRef<Path>,
86 bytes: &[u8],
87 multi: bool,
88 ) -> Result<Self, ClustersConfigParseError> {
89 if multi {
90 MultipleClustersConfig::parse(path.as_ref(), bytes)
91 .map(ConfigurableInner::Multi)
92 .map(Self)
93 } else {
94 SingleClusterConfig::parse(path.as_ref(), bytes)
95 .map(ConfigurableInner::Single)
96 .map(Self)
97 }
98 }
99
100 #[inline]
101 pub(super) fn config_paths(&self) -> Vec<PathBuf> {
102 match &self.0 {
103 ConfigurableInner::Single(single) => single.config_paths(),
104 ConfigurableInner::Multi(multi) => multi.config_paths(),
105 }
106 }
107
108 #[inline]
109 pub(super) fn timeouts_set(&self) -> HashSet<Timeouts> {
110 match &self.0 {
111 ConfigurableInner::Single(single) => single.timeouts_set(),
112 ConfigurableInner::Multi(multi) => multi.timeouts_set(),
113 }
114 }
115}
116
117impl From<SingleClusterConfig> for Configurable {
118 #[inline]
119 fn from(config: SingleClusterConfig) -> Self {
120 Self::new_single(config)
121 }
122}
123
124impl From<MultipleClustersConfig> for Configurable {
125 #[inline]
126 fn from(config: MultipleClustersConfig) -> Self {
127 Self::new_multi(config)
128 }
129}