opendal/services/github/
config.rs1use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::GITHUB_SCHEME;
24use super::backend::GithubBuilder;
25
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
28#[serde(default)]
29#[non_exhaustive]
30pub struct GithubConfig {
31 pub root: Option<String>,
35 pub token: Option<String>,
41 pub owner: String,
45 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}