opendal/services/sled/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19use std::fmt::Formatter;
20
21use serde::Deserialize;
22use serde::Serialize;
23
24use super::backend::SledBuilder;
25
26/// Config for Sled services support.
27#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
28#[serde(default)]
29#[non_exhaustive]
30pub struct SledConfig {
31    /// That path to the sled data directory.
32    pub datadir: Option<String>,
33    /// The tree for sled.
34    pub tree: Option<String>,
35    /// The root for sled.
36    pub root: Option<String>,
37}
38
39impl Debug for SledConfig {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        f.debug_struct("SledConfig")
42            .field("datadir", &self.datadir)
43            .field("tree", &self.tree)
44            .field("root", &self.root)
45            .finish()
46    }
47}
48
49impl crate::Configurator for SledConfig {
50    type Builder = SledBuilder;
51
52    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
53        let mut map = uri.options().clone();
54
55        if let Some(path) = uri.root() {
56            if !path.is_empty() {
57                map.entry("datadir".to_string())
58                    .or_insert_with(|| format!("/{path}"));
59            }
60        }
61
62        Self::from_iter(map)
63    }
64
65    fn into_builder(self) -> Self::Builder {
66        SledBuilder { config: self }
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use crate::Configurator;
74    use crate::types::OperatorUri;
75
76    #[test]
77    fn from_uri_sets_datadir_tree_and_root() {
78        let uri = OperatorUri::new(
79            "sled:///var/data/sled?tree=cache&root=items",
80            Vec::<(String, String)>::new(),
81        )
82        .unwrap();
83
84        let cfg = SledConfig::from_uri(&uri).unwrap();
85        assert_eq!(cfg.datadir.as_deref(), Some("/var/data/sled"));
86        assert_eq!(cfg.tree.as_deref(), Some("cache"));
87        assert_eq!(cfg.root.as_deref(), Some("items"));
88    }
89}