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
283
284
use std::{path::PathBuf, sync::Arc};
use crate::{
download::Download,
error::DownloadError,
filter::Filter,
traits::{Asset as _, Platform, Release as _},
types::{OverwriteMode, Progress},
};
pub struct ReleaseDownload<P: Platform> {
project: String,
tag: Option<String>,
filter: Filter,
output: Option<String>,
overwrite: OverwriteMode,
extract: bool,
extract_to: Option<PathBuf>,
on_progress: Option<Arc<dyn Fn(Progress) + Send + Sync>>,
_platform: std::marker::PhantomData<P>,
}
impl<P: Platform> ReleaseDownload<P> {
/// Creates a new `ReleaseDownload` configured for the given project with sensible defaults.
///
/// The returned builder is initialized with:
/// - `tag = None`
/// - a default `Filter`
/// - no explicit output path
/// - `overwrite = OverwriteMode::Prompt`
/// - extraction disabled
/// - no extraction path
/// - no progress callback
///
/// # Examples
///
/// ```
/// use soar_dl::release::ReleaseDownload;
/// use soar_dl::github::Github;
///
/// let dl = ReleaseDownload::<Github>::new("owner/repo");
/// // You can then chain further configuration:
/// // let dl = dl.tag("v1.2.3").output("downloads/").extract(true);
/// ```
pub fn new(project: impl Into<String>) -> Self {
Self {
project: project.into(),
tag: None,
filter: Filter::default(),
output: None,
overwrite: OverwriteMode::Prompt,
extract: false,
extract_to: None,
on_progress: None,
_platform: std::marker::PhantomData,
}
}
/// Sets the release tag to target when selecting a release.
///
/// The provided tag will be used by `execute` to find a release with a matching tag.
/// Returns the updated builder to allow method chaining.
///
/// # Examples
///
/// ```
/// use soar_dl::release::ReleaseDownload;
/// use soar_dl::github::Github;
///
/// let builder = ReleaseDownload::<Github>::new("owner/repo").tag("v1.2.3");
/// ```
pub fn tag(mut self, tag: impl Into<String>) -> Self {
self.tag = Some(tag.into());
self
}
/// Sets the asset filter used to select which release assets will be downloaded.
///
/// The provided `filter` will be used to match asset names when executing the download.
///
/// # Examples
///
/// ```
/// use soar_dl::release::ReleaseDownload;
/// use soar_dl::filter::Filter;
/// use soar_dl::github::Github;
///
/// let _rd = ReleaseDownload::<Github>::new("owner/repo").filter(Filter::default());
/// ```
pub fn filter(mut self, filter: Filter) -> Self {
self.filter = filter;
self
}
/// Sets the base output path for downloaded assets.
///
/// The provided path will be used as the destination directory or base file path when downloads are written.
///
/// # Returns
///
/// The modified `ReleaseDownload` builder with the output path set.
///
/// # Examples
///
/// ```
/// use soar_dl::release::ReleaseDownload;
/// use soar_dl::github::Github;
///
/// let dl = ReleaseDownload::<Github>::new("owner/repo").output("downloads");
/// ```
pub fn output(mut self, path: impl Into<String>) -> Self {
self.output = Some(path.into());
self
}
/// Set the overwrite behavior for downloaded files.
///
/// `mode` determines how existing files are handled when downloading (for example, overwrite, skip, or prompt).
///
/// # Examples
///
/// ```
/// use soar_dl::release::ReleaseDownload;
/// use soar_dl::types::OverwriteMode;
/// use soar_dl::github::Github;
///
/// let dl = ReleaseDownload::<Github>::new("owner/repo").overwrite(OverwriteMode::Force);
/// ```
pub fn overwrite(mut self, mode: OverwriteMode) -> Self {
self.overwrite = mode;
self
}
/// Enables or disables extraction of downloaded assets.
///
/// When set to `true`, assets that are archives will be extracted after they are downloaded.
///
/// # Examples
///
/// ```
/// use soar_dl::release::ReleaseDownload;
/// use soar_dl::github::Github;
///
/// let rd = ReleaseDownload::<Github>::new("owner/repo").extract(true);
/// ```
pub fn extract(mut self, extract: bool) -> Self {
self.extract = extract;
self
}
/// Sets the destination directory where downloaded archives will be extracted.
///
/// # Arguments
///
/// * `path` - Destination path to extract downloaded assets into.
///
/// # Examples
///
/// ```
/// use soar_dl::release::ReleaseDownload;
/// use soar_dl::github::Github;
///
/// let rd = ReleaseDownload::<Github>::new("owner/repo").extract_to("out/artifacts");
/// ```
pub fn extract_to(mut self, path: impl Into<PathBuf>) -> Self {
self.extract_to = Some(path.into());
self
}
/// Registers a callback that will be invoked with progress updates for each download.
///
/// The provided callback is stored and called with `Progress` events as assets are downloaded.
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
/// use soar_dl::release::ReleaseDownload;
/// use soar_dl::types::Progress;
/// use soar_dl::github::Github;
///
/// let _rd = ReleaseDownload::<Github>::new("owner/repo")
/// .progress(|progress: Progress| {
/// // handle progress (e.g., log or update UI)
/// println!("{:?}", progress);
/// });
/// ```
pub fn progress<F>(mut self, f: F) -> Self
where
F: Fn(Progress) + Send + Sync + 'static,
{
self.on_progress = Some(Arc::new(f));
self
}
/// Downloads matched assets for a project's release and returns their local file paths.
///
/// Selects a release by the configured tag if provided; otherwise prefers the first non-prerelease
/// release or falls back to the first release.
///
/// Filters the release's assets using the configured `Filter`, downloads each matching asset with the configured
/// output, overwrite, and extraction options, and returns a vector of the resulting local `PathBuf`s.
///
/// Returns an error if no release is found or if no assets match the filter.
///
/// # Returns
///
/// A `Vec<PathBuf>` containing the local paths of the downloaded assets on success, or a
/// `DownloadError` on failure.
///
/// # Examples
///
/// ```no_run
/// use std::path::PathBuf;
/// use soar_dl::release::ReleaseDownload;
/// use soar_dl::github::Github;
/// use soar_dl::filter::Filter;
///
/// let paths: Vec<PathBuf> = ReleaseDownload::<Github>::new("owner/repo")
/// .tag("v1.0")
/// .filter(Filter::default())
/// .output("downloads")
/// .execute()
/// .unwrap();
///
/// assert!(!paths.is_empty());
/// ```
pub fn execute(self) -> Result<Vec<PathBuf>, DownloadError> {
let releases = P::fetch_releases(&self.project, self.tag.as_deref())?;
let release = if let Some(ref tag) = self.tag {
releases.iter().find(|r| r.tag() == tag)
} else {
releases
.iter()
.find(|r| !r.is_prerelease())
.or_else(|| releases.first())
};
let release = release.ok_or_else(|| DownloadError::InvalidResponse)?;
let assets: Vec<_> = release
.assets()
.iter()
.filter(|a| self.filter.matches(a.name()))
.collect();
if assets.is_empty() {
return Err(DownloadError::NoMatch {
available: release
.assets()
.iter()
.map(|a| a.name().to_string())
.collect(),
});
}
let mut paths = Vec::new();
for asset in assets {
let mut dl = Download::new(asset.url())
.overwrite(self.overwrite)
.extract(self.extract);
if let Some(ref output) = self.output {
dl = dl.output(output);
}
if let Some(ref extract_to) = self.extract_to {
dl = dl.extract_to(extract_to);
}
if let Some(ref cb) = self.on_progress {
let cb = cb.clone();
dl = dl.progress(move |p| cb(p));
}
let path = dl.execute()?;
paths.push(path);
}
Ok(paths)
}
}