opendal/services/ghac/
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;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::GhacBuilder;
24
25/// Config for GitHub Action Cache Services support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct GhacConfig {
30    /// The root path for ghac.
31    pub root: Option<String>,
32    /// The version that used by cache.
33    pub version: Option<String>,
34    /// The endpoint for ghac service.
35    pub endpoint: Option<String>,
36    /// The runtime token for ghac service.
37    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}