1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use serde::Deserialize;
use crate::{
error::DownloadError,
platform::fetch_releases_json,
traits::{Asset, Platform, Release},
};
pub struct Github;
#[derive(Debug, Clone, Deserialize)]
pub struct GithubRelease {
pub name: Option<String>,
pub tag_name: String,
pub prerelease: bool,
pub published_at: String,
pub body: Option<String>,
pub assets: Vec<GithubAsset>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct GithubAsset {
pub name: String,
pub size: u64,
pub browser_download_url: String,
}
impl Platform for Github {
type Release = GithubRelease;
const API_BASE: &'static str = "https://api.github.com";
const TOKEN_ENV: [&str; 2] = ["GITHUB_TOKEN", "GH_TOKEN"];
/// Fetches releases for the given GitHub repository, optionally filtered by a specific tag.
///
/// If `tag` is provided, fetches the release that matches that tag; otherwise fetches the repository's releases (up to 100 per page).
///
/// # Arguments
///
/// * `project` — repository identifier in the form "owner/repo".
/// * `tag` — optional release tag to filter the results.
///
/// # Returns
///
/// `Ok` with a vector of releases on success, or `Err(DownloadError)` on failure.
///
/// # Examples
///
/// ```no_run
/// use soar_dl::github::Github;
/// use soar_dl::traits::{Platform, Release};
///
/// let releases = Github::fetch_releases("rust-lang/rust", None).unwrap();
/// assert!(releases.iter().all(|r| r.tag().len() > 0));
/// ```
fn fetch_releases(
project: &str,
tag: Option<&str>,
) -> Result<Vec<Self::Release>, DownloadError> {
let path = match tag {
Some(tag) => {
let encoded_tag =
url::form_urlencoded::byte_serialize(tag.as_bytes()).collect::<String>();
format!(
"/repos/{project}/releases/tags/{}?per_page=100",
encoded_tag
)
}
None => format!("/repos/{project}/releases?per_page=100"),
};
fetch_releases_json::<Self::Release>(&path, Self::API_BASE, Self::TOKEN_ENV)
}
}
impl Release for GithubRelease {
type Asset = GithubAsset;
/// The release's name, or an empty string if the release has no name.
///
/// # Examples
///
/// ```
/// use soar_dl::github::GithubRelease;
/// use soar_dl::traits::Release;
///
/// let r = GithubRelease {
/// name: Some("v1.0".into()),
/// tag_name: "v1.0".into(),
/// prerelease: false,
/// published_at: "".into(),
/// body: None,
/// assets: vec![],
/// };
/// assert_eq!(r.name(), "v1.0");
///
/// let unnamed = GithubRelease {
/// name: None,
/// tag_name: "v1.1".into(),
/// prerelease: false,
/// published_at: "".into(),
/// body: None,
/// assets: vec![],
/// };
/// assert_eq!(unnamed.name(), "");
/// ```
fn name(&self) -> &str {
self.name.as_deref().unwrap_or("")
}
/// Get the release tag as a string slice.
///
/// # Examples
///
/// ```
/// use soar_dl::github::GithubRelease;
/// use soar_dl::traits::Release;
///
/// let release = GithubRelease {
/// name: None,
/// tag_name: "v1.0.0".into(),
/// prerelease: false,
/// published_at: "".into(),
/// body: None,
/// assets: vec![],
/// };
/// assert_eq!(release.tag(), "v1.0.0");
/// ```
///
/// # Returns
///
/// `&str` containing the release tag.
fn tag(&self) -> &str {
&self.tag_name
}
/// Indicates whether the release is marked as a prerelease.
///
/// # Returns
///
/// `true` if the release is marked as a prerelease, `false` otherwise.
///
/// # Examples
///
/// ```
/// use soar_dl::github::GithubRelease;
/// use soar_dl::traits::Release;
///
/// let r = GithubRelease {
/// name: None,
/// tag_name: "v1.0.0".to_string(),
/// prerelease: true,
/// published_at: "".to_string(),
/// body: None,
/// assets: vec![],
/// };
/// assert!(r.is_prerelease());
/// ```
fn is_prerelease(&self) -> bool {
self.prerelease
}
/// Returns the release's publication timestamp as an RFC 3339 formatted string.
///
/// # Examples
///
/// ```
/// use soar_dl::github::GithubRelease;
/// use soar_dl::traits::Release;
///
/// let r = GithubRelease {
/// name: None,
/// tag_name: "v1.0.0".into(),
/// prerelease: false,
/// published_at: "2021-01-01T00:00:00Z".into(),
/// body: None,
/// assets: vec![],
/// };
/// assert_eq!(r.published_at(), "2021-01-01T00:00:00Z");
/// ```
fn published_at(&self) -> &str {
&self.published_at
}
/// Get a slice of assets associated with the release.
///
/// The slice contains the release's assets in declaration order.
///
/// # Examples
///
/// ```
/// use soar_dl::github::{GithubRelease, GithubAsset};
/// use soar_dl::traits::Release;
///
/// let asset = GithubAsset {
/// name: "example.zip".into(),
/// size: 1024,
/// browser_download_url: "https://example.com/example.zip".into(),
/// };
///
/// let release = GithubRelease {
/// name: Some("v1.0".into()),
/// tag_name: "v1.0".into(),
/// prerelease: false,
/// published_at: "2025-01-01T00:00:00Z".into(),
/// body: None,
/// assets: vec![asset],
/// };
///
/// assert_eq!(release.assets().len(), 1);
/// ```
fn assets(&self) -> &[Self::Asset] {
&self.assets
}
fn body(&self) -> Option<&str> {
self.body.as_deref()
}
}
impl Asset for GithubAsset {
/// Retrieves the asset's name.
///
/// # Examples
///
/// ```
/// use soar_dl::github::GithubAsset;
/// use soar_dl::traits::Asset;
///
/// let asset = GithubAsset {
/// name: "file.zip".to_string(),
/// size: 123,
/// browser_download_url: "https://example.com/file.zip".to_string(),
/// };
/// assert_eq!(asset.name(), "file.zip");
/// ```
///
/// # Returns
///
/// A `&str` containing the asset's name.
fn name(&self) -> &str {
&self.name
}
/// Asset size in bytes.
///
/// # Returns
///
/// `Some(size)` containing the asset size in bytes.
///
/// # Examples
///
/// ```
/// use soar_dl::github::GithubAsset;
/// use soar_dl::traits::Asset;
///
/// let asset = GithubAsset { name: "file".into(), size: 12345, browser_download_url: "https://example.com".into() };
/// assert_eq!(asset.size(), Some(12345));
/// ```
fn size(&self) -> Option<u64> {
Some(self.size)
}
/// Returns the asset's browser download URL.
///
/// # Examples
///
/// ```
/// use soar_dl::github::GithubAsset;
/// use soar_dl::traits::Asset;
///
/// let asset = GithubAsset {
/// name: "example".into(),
/// size: 123,
/// browser_download_url: "https://example.com/download".into(),
/// };
/// assert_eq!(asset.url(), "https://example.com/download");
/// ```
fn url(&self) -> &str {
&self.browser_download_url
}
}