github_proxy/
resource.rs

1use strum_macros::EnumIter;
2
3use crate::proxy::Proxy;
4
5/// GitHub resource types
6#[cfg_attr(feature = "wasm", wasm_bindgen::prelude::wasm_bindgen)]
7#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
8#[derive(EnumIter, Debug, PartialEq, Hash, Eq, Clone)]
9pub enum GitHubResource {
10    /// Raw file in a repository
11    /// Format: owner/repo/reference/path
12    /// reference can be: branch name, tag, commit hash, or refs/heads/branch
13    File {
14        owner: String,
15        repo: String,
16        reference: String,
17        path: String,
18    },
19    /// Release asset
20    /// Format: owner/repo/tag/filename
21    Release {
22        owner: String,
23        repo: String,
24        tag: String,
25        name: String,
26    },
27}
28
29impl GitHubResource {
30    /// Create a new file resource
31    ///
32    /// # Arguments
33    /// * `owner` - Repository owner
34    /// * `repo` - Repository name
35    /// * `reference` - Git reference (branch, tag, commit hash, or refs/heads/branch)
36    /// * `path` - File path in the repository
37    pub fn file(owner: String, repo: String, reference: String, path: String) -> Self {
38        GitHubResource::File {
39            owner,
40            repo,
41            reference,
42            path,
43        }
44    }
45
46    /// Create a new release resource
47    pub fn release(owner: String, repo: String, tag: String, name: String) -> Self {
48        GitHubResource::Release {
49            owner,
50            repo,
51            tag,
52            name,
53        }
54    }
55
56    /// Convert the resource to a proxied URL
57    pub fn url(&self, proxy_type: &Proxy) -> String {
58        match self {
59            GitHubResource::File {
60                owner,
61                repo,
62                reference,
63                path,
64            } => match proxy_type {
65                Proxy::GitHub => {
66                    format!(
67                        "https://github.com/{}/{}/raw/{}/{}",
68                        owner, repo, reference, path
69                    )
70                }
71                Proxy::Xget => {
72                    format!(
73                        "https://xget.xi-xu.me/gh/{}/{}/raw/{}/{}",
74                        owner, repo, reference, path
75                    )
76                }
77                Proxy::GhProxy => {
78                    format!(
79                        "https://gh-proxy.com/https://github.com/{}/{}/raw/{}/{}",
80                        owner, repo, reference, path
81                    )
82                }
83                Proxy::Jsdelivr => {
84                    format!(
85                        "https://cdn.jsdelivr.net/gh/{}/{}@{}/{}",
86                        owner, repo, reference, path
87                    )
88                }
89            },
90            GitHubResource::Release {
91                owner,
92                repo,
93                tag,
94                name,
95            } => match proxy_type {
96                Proxy::GitHub => {
97                    format!(
98                        "https://github.com/{}/{}/releases/download/{}/{}",
99                        owner, repo, tag, name
100                    )
101                }
102                Proxy::Xget => {
103                    format!(
104                        "https://xget.xi-xu.me/gh/{}/{}/releases/download/{}/{}",
105                        owner, repo, tag, name
106                    )
107                }
108                Proxy::GhProxy => {
109                    format!(
110                        "https://gh-proxy.com/https://github.com/{}/{}/releases/download/{}/{}",
111                        owner, repo, tag, name
112                    )
113                }
114                Proxy::Jsdelivr => {
115                    format!(
116                        "https://cdn.jsdelivr.net/gh/{}/{}@{}/{}",
117                        owner, repo, tag, name
118                    )
119                }
120            },
121        }
122    }
123}