opendal_core/services/lakefs/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::LAKEFS_SCHEME;
24use super::backend::LakefsBuilder;
25
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
28#[serde(default)]
29#[non_exhaustive]
30pub struct LakefsConfig {
31 pub endpoint: Option<String>,
35 pub username: Option<String>,
39 pub password: Option<String>,
43 pub root: Option<String>,
47
48 pub repository: Option<String>,
52 pub branch: Option<String>,
56}
57
58impl Debug for LakefsConfig {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 f.debug_struct("LakefsConfig")
61 .field("endpoint", &self.endpoint)
62 .field("root", &self.root)
63 .field("repository", &self.repository)
64 .field("branch", &self.branch)
65 .finish_non_exhaustive()
66 }
67}
68
69impl crate::Configurator for LakefsConfig {
70 type Builder = LakefsBuilder;
71
72 fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
73 let raw_path = uri.root().ok_or_else(|| {
74 crate::Error::new(
75 crate::ErrorKind::ConfigInvalid,
76 "uri path must contain repository",
77 )
78 .with_context("service", LAKEFS_SCHEME)
79 })?;
80
81 let (repository, remainder) = match raw_path.split_once('/') {
82 Some((repo, rest)) => (repo, Some(rest)),
83 None => (raw_path, None),
84 };
85
86 let repository = if repository.is_empty() {
87 None
88 } else {
89 Some(repository)
90 }
91 .ok_or_else(|| {
92 crate::Error::new(
93 crate::ErrorKind::ConfigInvalid,
94 "repository is required in uri path",
95 )
96 .with_context("service", LAKEFS_SCHEME)
97 })?;
98
99 let mut map = uri.options().clone();
100 if let Some(authority) = uri.authority() {
101 map.insert("endpoint".to_string(), format!("https://{authority}"));
102 }
103 map.insert("repository".to_string(), repository.to_string());
104
105 if let Some(rest) = remainder {
106 if map.contains_key("branch") {
107 if !rest.is_empty() {
108 map.insert("root".to_string(), rest.to_string());
109 }
110 } else {
111 let (branch, maybe_root) = match rest.split_once('/') {
112 Some((branch_part, root_part)) => (branch_part, Some(root_part)),
113 None => (rest, None),
114 };
115
116 if !branch.is_empty() {
117 map.insert("branch".to_string(), branch.to_string());
118 }
119
120 if let Some(root_part) = maybe_root {
121 if !root_part.is_empty() {
122 map.insert("root".to_string(), root_part.to_string());
123 }
124 }
125 }
126 }
127
128 Self::from_iter(map)
129 }
130
131 fn into_builder(self) -> Self::Builder {
132 LakefsBuilder { config: self }
133 }
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139 use crate::Configurator;
140 use crate::types::OperatorUri;
141
142 #[test]
143 fn from_uri_sets_endpoint_repository_branch_and_root() {
144 let uri = OperatorUri::new(
145 "lakefs://api.example.com/sample/main/data/dir",
146 Vec::<(String, String)>::new(),
147 )
148 .unwrap();
149
150 let cfg = LakefsConfig::from_uri(&uri).unwrap();
151 assert_eq!(cfg.endpoint.as_deref(), Some("https://api.example.com"));
152 assert_eq!(cfg.repository.as_deref(), Some("sample"));
153 assert_eq!(cfg.branch.as_deref(), Some("main"));
154 assert_eq!(cfg.root.as_deref(), Some("data/dir"));
155 }
156
157 #[test]
158 fn from_uri_requires_repository() {
159 let uri =
160 OperatorUri::new("lakefs://api.example.com", Vec::<(String, String)>::new()).unwrap();
161
162 assert!(LakefsConfig::from_uri(&uri).is_err());
163 }
164
165 #[test]
166 fn from_uri_respects_branch_override_and_sets_root() {
167 let uri = OperatorUri::new(
168 "lakefs://api.example.com/sample/content",
169 vec![("branch".to_string(), "develop".to_string())],
170 )
171 .unwrap();
172
173 let cfg = LakefsConfig::from_uri(&uri).unwrap();
174 assert_eq!(cfg.branch.as_deref(), Some("develop"));
175 assert_eq!(cfg.root.as_deref(), Some("content"));
176 }
177}