opendal_service_redb/
config.rs1use serde::Deserialize;
19use serde::Serialize;
20
21use super::backend::RedbBuilder;
22
23#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
25#[serde(default)]
26#[non_exhaustive]
27pub struct RedbConfig {
28 pub datadir: Option<String>,
30 pub table: Option<String>,
32 pub root: Option<String>,
34}
35
36impl opendal_core::Configurator for RedbConfig {
37 type Builder = RedbBuilder;
38
39 fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
40 let mut map = uri.options().clone();
41
42 if let Some(path) = uri.root()
43 && !path.is_empty()
44 {
45 map.entry("datadir".to_string())
46 .or_insert_with(|| format!("/{path}"));
47 }
48
49 if !uri.options().contains_key("datadir")
53 && let Some(host) = uri.name()
54 && !host.eq_ignore_ascii_case("localhost")
55 {
56 return Err(opendal_core::Error::new(
57 opendal_core::ErrorKind::ConfigInvalid,
58 "redb uri must not contain a host, use redb:///path",
59 )
60 .with_context("host", host));
61 }
62
63 Self::from_iter(map)
64 }
65
66 fn into_builder(self) -> Self::Builder {
67 RedbBuilder {
68 config: self,
69 database: None,
70 }
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use opendal_core::Configurator;
78 use opendal_core::OperatorUri;
79
80 #[test]
81 fn from_uri_sets_datadir_table_and_root() {
82 let uri = OperatorUri::new(
83 "redb:///tmp/redb?table=op_table&root=cache",
84 Vec::<(String, String)>::new(),
85 )
86 .unwrap();
87
88 let cfg = RedbConfig::from_uri(&uri).unwrap();
89 assert_eq!(cfg.datadir.as_deref(), Some("/tmp/redb"));
90 assert_eq!(cfg.table.as_deref(), Some("op_table"));
91 assert_eq!(cfg.root.as_deref(), Some("cache"));
92 }
93
94 #[test]
95 fn from_uri_rejects_host() {
96 let no_opts = Vec::<(String, String)>::new;
97 let uri = OperatorUri::new("redb://tmp/data", no_opts()).unwrap();
98 let err = RedbConfig::from_uri(&uri).unwrap_err();
99 assert_eq!(err.kind(), opendal_core::ErrorKind::ConfigInvalid);
100
101 let uri = OperatorUri::new("redb://localhost/data", no_opts()).unwrap();
102 assert_eq!(
103 RedbConfig::from_uri(&uri).unwrap().datadir.as_deref(),
104 Some("/data")
105 );
106
107 let uri = OperatorUri::new(
109 "redb://tmp/data",
110 [("datadir".to_string(), "/explicit".to_string())],
111 )
112 .unwrap();
113 assert_eq!(
114 RedbConfig::from_uri(&uri).unwrap().datadir.as_deref(),
115 Some("/explicit")
116 );
117 }
118}