opendal/services/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 crate::Configurator for GhacConfig {
51 type Builder = GhacBuilder;
52
53 fn from_uri(uri: &crate::types::OperatorUri) -> crate::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 {
83 config: self,
84 http_client: None,
85 }
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92 use crate::Configurator;
93 use crate::types::OperatorUri;
94
95 #[test]
96 fn from_uri_sets_endpoint_version_and_root() {
97 let uri = OperatorUri::new(
98 "ghac://cache.githubactions.io/v1/cache-prefix",
99 Vec::<(String, String)>::new(),
100 )
101 .unwrap();
102
103 let cfg = GhacConfig::from_uri(&uri).unwrap();
104 assert_eq!(
105 cfg.endpoint.as_deref(),
106 Some("https://cache.githubactions.io")
107 );
108 assert_eq!(cfg.version.as_deref(), Some("v1"));
109 assert_eq!(cfg.root.as_deref(), Some("cache-prefix"));
110 }
111
112 #[test]
113 fn from_uri_respects_version_override() {
114 let uri = OperatorUri::new(
115 "ghac://cache.githubactions.io/cache-prefix",
116 vec![("version".to_string(), "v2".to_string())],
117 )
118 .unwrap();
119
120 let cfg = GhacConfig::from_uri(&uri).unwrap();
121 assert_eq!(cfg.version.as_deref(), Some("v2"));
122 assert_eq!(cfg.root.as_deref(), Some("cache-prefix"));
123 }
124}