azure_storage/
connection_string_builder.rs

1use crate::connection_string::*;
2
3#[derive(Debug, Default)]
4pub struct ConnectionStringBuilder<'a>(ConnectionString<'a>);
5
6impl<'a> ConnectionStringBuilder<'a> {
7    pub fn new() -> Self {
8        Self(ConnectionString::default())
9    }
10
11    pub fn build(&self) -> String {
12        let mut kv_pairs = Vec::new();
13
14        if let Some(account_name) = self.0.account_name {
15            kv_pairs.push(format!("{ACCOUNT_NAME_KEY_NAME}={account_name}"));
16        }
17        if let Some(account_key) = self.0.account_key {
18            kv_pairs.push(format!("{ACCOUNT_KEY_KEY_NAME}={account_key}"));
19        }
20        if let Some(sas) = self.0.sas {
21            kv_pairs.push(format!("{SAS_KEY_NAME}={sas}"));
22        }
23        if let Some(use_development_storage) = self.0.use_development_storage {
24            kv_pairs.push(format!(
25                "{USE_DEVELOPMENT_STORAGE_KEY_NAME}={use_development_storage}"
26            ));
27        }
28        if let Some(development_storage_proxy_uri) = self.0.development_storage_proxy_uri {
29            kv_pairs.push(format!(
30                "{DEVELOPMENT_STORAGE_PROXY_URI_KEY_NAME}={development_storage_proxy_uri}"
31            ));
32        }
33        if let Some(endpoint_suffix) = self.0.endpoint_suffix {
34            kv_pairs.push(format!("{ENDPOINT_SUFFIX_KEY_NAME}={endpoint_suffix}"));
35        }
36        if let Some(default_endpoints_protocol) = self.0.default_endpoints_protocol.as_ref() {
37            kv_pairs.push(format!(
38                "{DEFAULT_ENDPOINTS_PROTOCOL_KEY_NAME}={default_endpoints_protocol}"
39            ));
40        }
41        if let Some(blob_endpoint) = self.0.blob_endpoint {
42            kv_pairs.push(format!("{BLOB_ENDPOINT_KEY_NAME}={blob_endpoint}"));
43        }
44        if let Some(blob_secondary_endpoint) = self.0.blob_secondary_endpoint {
45            kv_pairs.push(format!(
46                "{BLOB_SECONDARY_ENDPOINT_KEY_NAME}={blob_secondary_endpoint}"
47            ));
48        }
49        if let Some(table_endpoint) = self.0.table_endpoint {
50            kv_pairs.push(format!("{TABLE_ENDPOINT_KEY_NAME}={table_endpoint}"));
51        }
52        if let Some(table_secondary_endpoint) = self.0.table_secondary_endpoint {
53            kv_pairs.push(format!(
54                "{TABLE_SECONDARY_ENDPOINT_KEY_NAME}={table_secondary_endpoint}"
55            ));
56        }
57        if let Some(queue_endpoint) = self.0.queue_endpoint {
58            kv_pairs.push(format!("{QUEUE_ENDPOINT_KEY_NAME}={queue_endpoint}"));
59        }
60        if let Some(queue_secondary_endpoint) = self.0.queue_secondary_endpoint {
61            kv_pairs.push(format!(
62                "{QUEUE_SECONDARY_ENDPOINT_KEY_NAME}={queue_secondary_endpoint}"
63            ));
64        }
65        if let Some(file_endpoint) = self.0.file_endpoint {
66            kv_pairs.push(format!("{FILE_ENDPOINT_KEY_NAME}={file_endpoint}"));
67        }
68        if let Some(file_secondary_endpoint) = self.0.file_secondary_endpoint {
69            kv_pairs.push(format!(
70                "{FILE_SECONDARY_ENDPOINT_KEY_NAME}={file_secondary_endpoint}"
71            ));
72        }
73
74        kv_pairs.join(";")
75    }
76
77    pub fn account_name(&'a mut self, account_name: &'a str) -> &'a mut Self {
78        self.0.account_name = Some(account_name);
79        self
80    }
81
82    pub fn account_key(&'a mut self, account_key: &'a str) -> &'a mut Self {
83        self.0.account_key = Some(account_key);
84        self
85    }
86
87    pub fn sas(&'a mut self, sas: &'a str) -> &'a mut Self {
88        self.0.sas = Some(sas);
89        self
90    }
91
92    pub fn endpoint_suffix(&'a mut self, endpoint_suffix: &'a str) -> &'a mut Self {
93        self.0.endpoint_suffix = Some(endpoint_suffix);
94        self
95    }
96
97    pub fn default_endpoints_protocol(
98        &'a mut self,
99        default_endpoints_protocol: EndpointProtocol,
100    ) -> &'a mut Self {
101        self.0.default_endpoints_protocol = Some(default_endpoints_protocol);
102        self
103    }
104
105    pub fn use_development_storage(&'a mut self, use_development_storage: bool) -> &'a mut Self {
106        self.0.use_development_storage = Some(use_development_storage);
107        self
108    }
109
110    pub fn development_storage_proxy_uri(
111        &'a mut self,
112        development_storage_proxy_uri: &'a str,
113    ) -> &'a mut Self {
114        self.0.development_storage_proxy_uri = Some(development_storage_proxy_uri);
115        self
116    }
117
118    pub fn blob_endpoint(&'a mut self, blob_endpoint: &'a str) -> &'a mut Self {
119        self.0.blob_endpoint = Some(blob_endpoint);
120        self
121    }
122
123    pub fn blob_secondary_endpoint(&'a mut self, blob_secondary_endpoint: &'a str) -> &'a mut Self {
124        self.0.blob_secondary_endpoint = Some(blob_secondary_endpoint);
125        self
126    }
127
128    pub fn table_endpoint(&'a mut self, table_endpoint: &'a str) -> &'a mut Self {
129        self.0.table_endpoint = Some(table_endpoint);
130        self
131    }
132
133    pub fn table_secondary_endpoint(
134        &'a mut self,
135        table_secondary_endpoint: &'a str,
136    ) -> &'a mut Self {
137        self.0.table_secondary_endpoint = Some(table_secondary_endpoint);
138        self
139    }
140
141    pub fn queue_endpoint(&'a mut self, queue_endpoint: &'a str) -> &'a mut Self {
142        self.0.queue_endpoint = Some(queue_endpoint);
143        self
144    }
145
146    pub fn queue_secondary_endpoint(
147        &'a mut self,
148        queue_secondary_endpoint: &'a str,
149    ) -> &'a mut Self {
150        self.0.queue_secondary_endpoint = Some(queue_secondary_endpoint);
151        self
152    }
153
154    pub fn file_endpoint(&'a mut self, file_endpoint: &'a str) -> &'a mut Self {
155        self.0.file_endpoint = Some(file_endpoint);
156        self
157    }
158
159    pub fn file_secondary_endpoint(&'a mut self, file_secondary_endpoint: &'a str) -> &'a mut Self {
160        self.0.file_secondary_endpoint = Some(file_secondary_endpoint);
161        self
162    }
163}
164
165#[cfg(test)]
166mod tests {
167    #[allow(unused_imports)]
168    use super::*;
169
170    #[test]
171    fn it_builds_generic_connection_strings() {
172        assert_eq!(ConnectionStringBuilder::new().build(), "");
173        assert_eq!(
174            ConnectionStringBuilder::new()
175                .account_name("a")
176                .account_key("b")
177                .build(),
178            "AccountName=a;AccountKey=b"
179        );
180        assert_eq!(
181            ConnectionStringBuilder::new()
182                .account_name("a")
183                .sas("b")
184                .default_endpoints_protocol(EndpointProtocol::Https)
185                .blob_endpoint("c")
186                .build(),
187            "AccountName=a;SharedAccessSignature=b;DefaultEndpointsProtocol=https;BlobEndpoint=c"
188        );
189    }
190
191    #[test]
192    fn it_builds_endpoints_with_development_storage() {
193        assert_eq!(
194            ConnectionStringBuilder::new()
195                .use_development_storage(true)
196                .development_storage_proxy_uri("a")
197                .build(),
198            "UseDevelopmentStorage=true;DevelopmentStorageProxyUri=a"
199        );
200        assert_eq!(
201            ConnectionStringBuilder::new()
202                .use_development_storage(false)
203                .build(),
204            "UseDevelopmentStorage=false"
205        );
206    }
207
208    #[test]
209    fn it_builds_all_endpoints() {
210        assert_eq!(
211            ConnectionStringBuilder::new()
212                .blob_endpoint("b1")
213                .blob_secondary_endpoint("b2")
214                .table_endpoint("t1")
215                .table_secondary_endpoint("t2")
216                .queue_endpoint("q1")
217                .queue_secondary_endpoint("q2")
218                .file_endpoint("f1")
219                .file_secondary_endpoint("f2")
220                .build(),
221            "BlobEndpoint=b1;BlobSecondaryEndpoint=b2;TableEndpoint=t1;TableSecondaryEndpoint=t2;QueueEndpoint=q1;QueueSecondaryEndpoint=q2;FileEndpoint=f1;FileSecondaryEndpoint=f2"
222        );
223    }
224}