opendal_service_ghac/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::GhacBuilder;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct GhacConfig {
30 pub root: Option<String>,
32 pub version: Option<String>,
34 pub endpoint: Option<String>,
36 pub runtime_token: Option<String>,
38}
39
40impl Debug for GhacConfig {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 f.debug_struct("GhacConfig")
43 .field("root", &self.root)
44 .field("version", &self.version)
45 .field("endpoint", &self.endpoint)
46 .finish_non_exhaustive()
47 }
48}
49
50impl opendal_core::Configurator for GhacConfig {
51 type Builder = GhacBuilder;
52
53 fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
54 let mut map = uri.options().clone();
55
56 if let Some(authority) = uri.authority() {
57 map.insert("endpoint".to_string(), format!("https://{authority}"));
58 }
59
60 if let Some(path) = uri.root() {
61 if map.contains_key("version") {
62 if !path.is_empty() {
63 map.insert("root".to_string(), path.to_string());
64 }
65 } else if let Some((version, rest)) = path.split_once('/') {
66 if !version.is_empty() {
67 map.insert("version".to_string(), version.to_string());
68 }
69 if !rest.is_empty() {
70 map.insert("root".to_string(), rest.to_string());
71 }
72 } else if !path.is_empty() {
73 map.insert("version".to_string(), path.to_string());
74 }
75 }
76
77 Self::from_iter(map)
78 }
79
80 #[allow(deprecated)]
81 fn into_builder(self) -> Self::Builder {
82 GhacBuilder { config: self }
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89 use opendal_core::Configurator;
90 use opendal_core::OperatorUri;
91
92 #[test]
93 fn from_uri_sets_endpoint_version_and_root() {
94 let uri = OperatorUri::new(
95 "ghac://cache.githubactions.io/v1/cache-prefix",
96 Vec::<(String, String)>::new(),
97 )
98 .unwrap();
99
100 let cfg = GhacConfig::from_uri(&uri).unwrap();
101 assert_eq!(
102 cfg.endpoint.as_deref(),
103 Some("https://cache.githubactions.io")
104 );
105 assert_eq!(cfg.version.as_deref(), Some("v1"));
106 assert_eq!(cfg.root.as_deref(), Some("cache-prefix"));
107 }
108
109 #[test]
110 fn from_uri_respects_version_override() {
111 let uri = OperatorUri::new(
112 "ghac://cache.githubactions.io/cache-prefix",
113 vec![("version".to_string(), "v2".to_string())],
114 )
115 .unwrap();
116
117 let cfg = GhacConfig::from_uri(&uri).unwrap();
118 assert_eq!(cfg.version.as_deref(), Some("v2"));
119 assert_eq!(cfg.root.as_deref(), Some("cache-prefix"));
120 }
121}