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