opendal/services/sled/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::SledBuilder;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct SledConfig {
30 pub datadir: Option<String>,
32 pub tree: Option<String>,
34 pub root: Option<String>,
36}
37
38impl Debug for SledConfig {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 f.debug_struct("SledConfig")
41 .field("tree", &self.tree)
42 .field("root", &self.root)
43 .finish_non_exhaustive()
44 }
45}
46
47impl crate::Configurator for SledConfig {
48 type Builder = SledBuilder;
49
50 fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
51 let mut map = uri.options().clone();
52
53 if let Some(path) = uri.root() {
54 if !path.is_empty() {
55 map.entry("datadir".to_string())
56 .or_insert_with(|| format!("/{path}"));
57 }
58 }
59
60 Self::from_iter(map)
61 }
62
63 fn into_builder(self) -> Self::Builder {
64 SledBuilder { config: self }
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71 use crate::Configurator;
72 use crate::types::OperatorUri;
73
74 #[test]
75 fn from_uri_sets_datadir_tree_and_root() {
76 let uri = OperatorUri::new(
77 "sled:///var/data/sled?tree=cache&root=items",
78 Vec::<(String, String)>::new(),
79 )
80 .unwrap();
81
82 let cfg = SledConfig::from_uri(&uri).unwrap();
83 assert_eq!(cfg.datadir.as_deref(), Some("/var/data/sled"));
84 assert_eq!(cfg.tree.as_deref(), Some("cache"));
85 assert_eq!(cfg.root.as_deref(), Some("items"));
86 }
87}