Skip to main content

autoschematic_connector_github/
addr.rs

1use std::path::{Path, PathBuf};
2
3use autoschematic_core::{connector::ResourceAddress, error_util::invalid_addr_path};
4
5#[derive(Debug, Clone)]
6pub enum GitHubResourceAddress {
7    // #need(Doc, Config)
8    Config,
9    // #need(Doc, Repository)
10    Repository { owner: String, repo: String },
11    // #need(Doc, BranchProtection)
12    BranchProtection { owner: String, repo: String, branch: String },
13}
14
15impl ResourceAddress for GitHubResourceAddress {
16    fn to_path_buf(&self) -> PathBuf {
17        match &self {
18            GitHubResourceAddress::Config => PathBuf::from("github/config.ron"),
19            GitHubResourceAddress::Repository { owner, repo } => PathBuf::from(format!("github/{owner}/{repo}/repository.ron")),
20            GitHubResourceAddress::BranchProtection { owner, repo, branch } => {
21                PathBuf::from(format!("github/{owner}/{repo}/branches/{branch}/protection.ron"))
22            }
23        }
24    }
25
26    fn from_path(path: &Path) -> Result<Self, anyhow::Error> {
27        let path_components: Vec<&str> = path.components().map(|s| s.as_os_str().to_str().unwrap()).collect();
28
29        match path_components[..] {
30            ["github", "config.ron"] => Ok(GitHubResourceAddress::Config),
31            ["github", owner, repo, "repository.ron"] => Ok(GitHubResourceAddress::Repository {
32                owner: owner.to_string(),
33                repo: repo.to_string(),
34            }),
35            ["github", owner, repo, "branches", branch, "protection.ron"] => Ok(GitHubResourceAddress::BranchProtection {
36                owner: owner.to_string(),
37                repo: repo.to_string(),
38                branch: branch.to_string(),
39            }),
40            _ => Err(invalid_addr_path(path)),
41        }
42    }
43}