opendal/services/github/
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::GITHUB_SCHEME;
24use super::backend::GithubBuilder;
25
26/// Config for GitHub services support.
27#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
28#[serde(default)]
29#[non_exhaustive]
30pub struct GithubConfig {
31    /// root of this backend.
32    ///
33    /// All operations will happen under this root.
34    pub root: Option<String>,
35    /// GitHub access_token.
36    ///
37    /// optional.
38    /// If not provided, the backend will only support read operations for public repositories.
39    /// And rate limit will be limited to 60 requests per hour.
40    pub token: Option<String>,
41    /// GitHub repo owner.
42    ///
43    /// required.
44    pub owner: String,
45    /// GitHub repo name.
46    ///
47    /// required.
48    pub repo: String,
49}
50
51impl Debug for GithubConfig {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        f.debug_struct("GithubConfig")
54            .field("root", &self.root)
55            .field("owner", &self.owner)
56            .field("repo", &self.repo)
57            .finish_non_exhaustive()
58    }
59}
60
61impl crate::Configurator for GithubConfig {
62    type Builder = GithubBuilder;
63
64    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
65        let owner = uri.name().ok_or_else(|| {
66            crate::Error::new(
67                crate::ErrorKind::ConfigInvalid,
68                "uri host must contain owner",
69            )
70            .with_context("service", GITHUB_SCHEME)
71        })?;
72
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", GITHUB_SCHEME)
79        })?;
80
81        let (repo, remainder) = match raw_path.split_once('/') {
82            Some((repo, rest)) => (repo, Some(rest)),
83            None => (raw_path, None),
84        };
85
86        if repo.is_empty() {
87            return Err(crate::Error::new(
88                crate::ErrorKind::ConfigInvalid,
89                "repository name is required",
90            )
91            .with_context("service", GITHUB_SCHEME));
92        }
93
94        let mut map = uri.options().clone();
95        map.insert("owner".to_string(), owner.to_string());
96        map.insert("repo".to_string(), repo.to_string());
97
98        if let Some(rest) = remainder {
99            if !rest.is_empty() {
100                map.insert("root".to_string(), rest.to_string());
101            }
102        }
103
104        Self::from_iter(map)
105    }
106
107    #[allow(deprecated)]
108    fn into_builder(self) -> Self::Builder {
109        GithubBuilder {
110            config: self,
111            http_client: None,
112        }
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119    use crate::Configurator;
120    use crate::types::OperatorUri;
121
122    #[test]
123    fn from_uri_sets_owner_repo_and_root() {
124        let uri = OperatorUri::new(
125            "github://apache/opendal/src/services",
126            Vec::<(String, String)>::new(),
127        )
128        .unwrap();
129
130        let cfg = GithubConfig::from_uri(&uri).unwrap();
131        assert_eq!(cfg.owner, "apache".to_string());
132        assert_eq!(cfg.repo, "opendal".to_string());
133        assert_eq!(cfg.root.as_deref(), Some("src/services"));
134    }
135
136    #[test]
137    fn from_uri_requires_repository() {
138        let uri = OperatorUri::new("github://apache", Vec::<(String, String)>::new()).unwrap();
139
140        assert!(GithubConfig::from_uri(&uri).is_err());
141    }
142}