opendal_service_sqlite/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::SqliteBuilder;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct SqliteConfig {
30 pub connection_string: Option<String>,
42
43 pub table: Option<String>,
45 pub key_field: Option<String>,
49 pub value_field: Option<String>,
53 pub root: Option<String>,
57}
58
59impl Debug for SqliteConfig {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 f.debug_struct("SqliteConfig")
62 .field("table", &self.table)
63 .field("key_field", &self.key_field)
64 .field("value_field", &self.value_field)
65 .field("root", &self.root)
66 .finish_non_exhaustive()
67 }
68}
69
70impl opendal_core::Configurator for SqliteConfig {
71 type Builder = SqliteBuilder;
72
73 fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
74 let mut map = uri.options().clone();
75
76 if let Some(authority) = uri.authority() {
77 map.entry("connection_string".to_string())
78 .or_insert_with(|| format!("sqlite://{authority}"));
79 }
80
81 if let Some(path) = uri.root()
82 && !path.is_empty()
83 {
84 let (table, rest) = match path.split_once('/') {
85 Some((table, remainder)) => (table, Some(remainder)),
86 None => (path, None),
87 };
88
89 if !table.is_empty() {
90 map.entry("table".to_string())
91 .or_insert_with(|| table.to_string());
92 }
93
94 if let Some(root) = rest
95 && !root.is_empty()
96 {
97 map.insert("root".to_string(), root.to_string());
98 }
99 }
100
101 Self::from_iter(map)
102 }
103
104 fn into_builder(self) -> Self::Builder {
105 SqliteBuilder { config: self }
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112 use opendal_core::Configurator;
113 use opendal_core::OperatorUri;
114
115 #[test]
116 fn from_uri_sets_connection_string_table_and_root() {
117 let uri =
118 OperatorUri::new("sqlite://data.db/kv/cache", Vec::<(String, String)>::new()).unwrap();
119
120 let cfg = SqliteConfig::from_uri(&uri).unwrap();
121 assert_eq!(cfg.connection_string.as_deref(), Some("sqlite://data.db"));
122 assert_eq!(cfg.table.as_deref(), Some("kv"));
123 assert_eq!(cfg.root.as_deref(), Some("cache"));
124 }
125}