opendal/services/tikv/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::TikvBuilder;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct TikvConfig {
30 pub endpoints: Option<Vec<String>>,
32 pub insecure: bool,
34 pub ca_path: Option<String>,
36 pub cert_path: Option<String>,
38 pub key_path: Option<String>,
40}
41
42impl Debug for TikvConfig {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 f.debug_struct("TikvConfig")
45 .field("endpoints", &self.endpoints)
46 .field("insecure", &self.insecure)
47 .field("ca_path", &self.ca_path)
48 .field("cert_path", &self.cert_path)
49 .field("key_path", &self.key_path)
50 .finish_non_exhaustive()
51 }
52}
53
54impl crate::Configurator for TikvConfig {
55 type Builder = TikvBuilder;
56
57 fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
58 let map = uri.options().clone();
59
60 let mut endpoints = Vec::new();
61 if let Some(authority) = uri.authority() {
62 if !authority.is_empty() {
63 endpoints.push(authority.to_string());
64 }
65 }
66
67 if let Some(path) = uri.root() {
68 for segment in path.split('/') {
69 for endpoint in segment.split(',') {
70 let trimmed = endpoint.trim();
71 if !trimmed.is_empty() {
72 endpoints.push(trimmed.to_string());
73 }
74 }
75 }
76 }
77
78 let mut cfg = Self::from_iter(map)?;
79
80 if !endpoints.is_empty() {
81 if let Some(existing) = cfg.endpoints.as_mut() {
82 existing.extend(endpoints);
83 } else {
84 cfg.endpoints = Some(endpoints);
85 }
86 }
87
88 Ok(cfg)
89 }
90
91 fn into_builder(self) -> Self::Builder {
92 TikvBuilder { config: self }
93 }
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99 use crate::Configurator;
100 use crate::types::OperatorUri;
101
102 #[test]
103 fn from_uri_collects_endpoints() {
104 let uri = OperatorUri::new(
105 "tikv://pd1:2379/pd2:2379,pd3:2379",
106 Vec::<(String, String)>::new(),
107 )
108 .unwrap();
109
110 let cfg = TikvConfig::from_uri(&uri).unwrap();
111 assert_eq!(
112 cfg.endpoints,
113 Some(vec![
114 "pd1:2379".to_string(),
115 "pd2:2379".to_string(),
116 "pd3:2379".to_string()
117 ])
118 );
119 }
120}