github_proxy/
lib.rs

1//! GitHub Proxy URL Generator
2//!
3//! This library provides functionality to generate proxied URLs for GitHub resources
4//! (raw files and release assets) using various proxy services.
5//!
6//! # Examples
7//!
8//! ```
9//! use github_proxy::{GitHubResource, Proxy};
10//!
11//! // Create a file resource
12//! let resource = GitHubResource::file(
13//!     "owner".to_string(),
14//!     "repo".to_string(),
15//!     "main".to_string(),
16//!     "file.sh".to_string()
17//! );
18//!
19//! // Generate URL with xget proxy
20//! let proxy = Proxy::Xget;
21//! let url = resource.url(&proxy);
22//! println!("{}", url);
23//! ```
24
25pub mod cli;
26mod error;
27mod proxy;
28mod resource;
29
30pub use error::ConversionError;
31pub use proxy::Proxy;
32pub use resource::GitHubResource;
33
34#[cfg(test)]
35mod tests {
36    use std::str::FromStr as _;
37
38    use super::*;
39
40    #[test]
41    fn test_file_resource_xget() {
42        let resource = GitHubResource::file(
43            "easy-install".to_string(),
44            "easy-install".to_string(),
45            "main".to_string(),
46            "install.sh".to_string(),
47        );
48        let url = resource.url(&Proxy::Xget);
49        assert_eq!(
50            url,
51            "https://xget.xi-xu.me/gh/easy-install/easy-install/raw/main/install.sh"
52        );
53    }
54
55    #[test]
56    fn test_file_resource_gh_proxy() {
57        let resource = GitHubResource::file(
58            "owner".to_string(),
59            "repo".to_string(),
60            "main".to_string(),
61            "file.sh".to_string(),
62        );
63        let url = resource.url(&Proxy::GhProxy);
64        assert_eq!(
65            url,
66            "https://gh-proxy.com/https://github.com/owner/repo/raw/main/file.sh"
67        );
68    }
69
70    #[test]
71    fn test_file_resource_jsdelivr() {
72        let resource = GitHubResource::file(
73            "owner".to_string(),
74            "repo".to_string(),
75            "main".to_string(),
76            "file.js".to_string(),
77        );
78        let url = resource.url(&Proxy::Jsdelivr);
79        assert_eq!(url, "https://cdn.jsdelivr.net/gh/owner/repo@main/file.js");
80    }
81
82    #[test]
83    fn test_file_resource_github() {
84        let resource = GitHubResource::file(
85            "owner".to_string(),
86            "repo".to_string(),
87            "main".to_string(),
88            "file.sh".to_string(),
89        );
90        let url = resource.url(&Proxy::GitHub);
91        assert_eq!(url, "https://github.com/owner/repo/raw/main/file.sh");
92    }
93
94    #[test]
95    fn test_release_resource_xget() {
96        let resource = GitHubResource::release(
97            "easy-install".to_string(),
98            "easy-install".to_string(),
99            "nightly".to_string(),
100            "ei-aarch64-apple-darwin.tar.gz".to_string(),
101        );
102        let url = resource.url(&Proxy::Xget);
103        assert_eq!(
104            url,
105            "https://xget.xi-xu.me/gh/easy-install/easy-install/releases/download/nightly/ei-aarch64-apple-darwin.tar.gz"
106        );
107    }
108
109    #[test]
110    fn test_release_resource_gh_proxy() {
111        let resource = GitHubResource::release(
112            "owner".to_string(),
113            "repo".to_string(),
114            "v1.0.0".to_string(),
115            "app.tar.gz".to_string(),
116        );
117        let url = resource.url(&Proxy::GhProxy);
118        assert_eq!(
119            url,
120            "https://gh-proxy.com/https://github.com/owner/repo/releases/download/v1.0.0/app.tar.gz"
121        );
122    }
123
124    #[test]
125    fn test_release_resource_jsdelivr() {
126        let resource = GitHubResource::release(
127            "owner".to_string(),
128            "repo".to_string(),
129            "v1.0.0".to_string(),
130            "app.tar.gz".to_string(),
131        );
132        let url = resource.url(&Proxy::Jsdelivr);
133        assert_eq!(
134            url,
135            "https://cdn.jsdelivr.net/gh/owner/repo@v1.0.0/app.tar.gz"
136        );
137    }
138
139    #[test]
140    fn test_release_resource_github() {
141        let resource = GitHubResource::release(
142            "owner".to_string(),
143            "repo".to_string(),
144            "v1.0.0".to_string(),
145            "app.tar.gz".to_string(),
146        );
147        let url = resource.url(&Proxy::GitHub);
148        assert_eq!(
149            url,
150            "https://github.com/owner/repo/releases/download/v1.0.0/app.tar.gz"
151        );
152    }
153
154    #[test]
155    fn test_proxy_type_from_str() {
156        assert_eq!(Proxy::from_str("github").unwrap(), Proxy::GitHub);
157        assert_eq!(Proxy::from_str("gh-proxy").unwrap(), Proxy::GhProxy);
158        assert_eq!(Proxy::from_str("xget").unwrap(), Proxy::Xget);
159        assert_eq!(Proxy::from_str("jsdelivr").unwrap(), Proxy::Jsdelivr);
160        assert_eq!(Proxy::from_str("XGET").unwrap(), Proxy::Xget);
161        assert!(Proxy::from_str("invalid").is_err());
162    }
163
164    #[test]
165    fn test_nested_file_path() {
166        let resource = GitHubResource::file(
167            "owner".to_string(),
168            "repo".to_string(),
169            "main".to_string(),
170            "src/lib/file.rs".to_string(),
171        );
172        let url = resource.url(&Proxy::Xget);
173        assert_eq!(
174            url,
175            "https://xget.xi-xu.me/gh/owner/repo/raw/main/src/lib/file.rs"
176        );
177    }
178
179    #[test]
180    fn test_branch_with_refs() {
181        let resource = GitHubResource::file(
182            "owner".to_string(),
183            "repo".to_string(),
184            "refs/heads/main".to_string(),
185            "file.sh".to_string(),
186        );
187        let url = resource.url(&Proxy::GitHub);
188        assert_eq!(
189            url,
190            "https://github.com/owner/repo/raw/refs/heads/main/file.sh"
191        );
192    }
193}