Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make FetchRepoDataOptions clonable #321

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions crates/rattler-bin/src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,13 +588,11 @@ async fn fetch_repo_data_records_with_progress(
channel.platform_url(platform),
client,
repodata_cache,
FetchRepoDataOptions {
download_progress: Some(Box::new(move |DownloadProgress { total, bytes }| {
download_progress_progress_bar.set_length(total.unwrap_or(bytes));
download_progress_progress_bar.set_position(bytes);
})),
..Default::default()
},
FetchRepoDataOptions::default(),
Some(Box::new(move |DownloadProgress { total, bytes }| {
download_progress_progress_bar.set_length(total.unwrap_or(bytes));
download_progress_progress_bar.set_position(bytes);
})),
)
.await;

Expand Down
37 changes: 24 additions & 13 deletions crates/rattler_repodata_gateway/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use url::Url;
mod cache;
pub mod jlap;

/// Type alias for function to report progress while downloading repodata
pub type ProgressFunc = Box<dyn FnMut(DownloadProgress) + Send + Sync>;

/// RepoData could not be found for given channel and platform
#[derive(Debug, thiserror::Error)]
pub enum RepoDataNotFoundError {
Expand Down Expand Up @@ -136,15 +139,12 @@ impl Variant {
}

/// Additional knobs that allow you to tweak the behavior of [`fetch_repo_data`].
#[derive(Default)]
#[derive(Default, Clone)]
pub struct FetchRepoDataOptions {
/// How to use the cache. By default it will cache and reuse downloaded repodata.json (if the
/// server allows it).
pub cache_action: CacheAction,

/// A function that is called during downloading of the repodata.json to report progress.
pub download_progress: Option<Box<dyn FnMut(DownloadProgress) + Send>>,

/// Determines which variant to download. See [`Variant`] for more information.
pub variant: Variant,
}
Expand Down Expand Up @@ -272,6 +272,7 @@ pub async fn fetch_repo_data(
client: AuthenticatedClient,
cache_path: &Path,
options: FetchRepoDataOptions,
progress: Option<ProgressFunc>,
) -> Result<CachedRepoData, FetchRepoDataError> {
let subdir_url = normalize_subdir_url(subdir_url);

Expand Down Expand Up @@ -517,7 +518,7 @@ pub async fn fetch_repo_data(
Encoding::Passthrough
},
cache_path,
options.download_progress,
progress,
)
.await?;

Expand Down Expand Up @@ -578,16 +579,16 @@ async fn stream_and_decode_to_file(
response: Response,
content_encoding: Encoding,
temp_dir: &Path,
mut progress: Option<Box<dyn FnMut(DownloadProgress) + Send>>,
mut progress_func: Option<ProgressFunc>,
) -> Result<(NamedTempFile, blake2::digest::Output<Blake2b256>), FetchRepoDataError> {
// Determine the length of the response in bytes and notify the listener that a download is
// starting. The response may be compressed. Decompression happens below.
let content_size = response.content_length();
if let Some(progress) = progress.as_mut() {
progress(DownloadProgress {
if let Some(progress_func) = progress_func.as_mut() {
progress_func(DownloadProgress {
bytes: 0,
total: content_size,
})
});
}

// Determine the encoding of the response
Expand All @@ -605,11 +606,11 @@ async fn stream_and_decode_to_file(
let total_bytes_mut = &mut total_bytes;
let bytes_stream = bytes_stream.inspect_ok(move |bytes| {
*total_bytes_mut += bytes.len() as u64;
if let Some(progress) = progress.as_mut() {
progress(DownloadProgress {
if let Some(progress_func) = progress_func.as_mut() {
progress_func(DownloadProgress {
bytes: *total_bytes_mut,
total: content_size,
})
});
}
});

Expand Down Expand Up @@ -1102,6 +1103,7 @@ mod test {
AuthenticatedClient::default(),
cache_dir.path(),
Default::default(),
None,
)
.await
.unwrap();
Expand Down Expand Up @@ -1131,6 +1133,7 @@ mod test {
AuthenticatedClient::default(),
cache_dir.path(),
Default::default(),
None,
)
.await
.unwrap();
Expand All @@ -1143,6 +1146,7 @@ mod test {
AuthenticatedClient::default(),
cache_dir.path(),
Default::default(),
None,
)
.await
.unwrap();
Expand All @@ -1166,6 +1170,7 @@ mod test {
AuthenticatedClient::default(),
cache_dir.path(),
Default::default(),
None,
)
.await
.unwrap();
Expand Down Expand Up @@ -1194,6 +1199,7 @@ mod test {
AuthenticatedClient::default(),
cache_dir.path(),
Default::default(),
None,
)
.await
.unwrap();
Expand Down Expand Up @@ -1235,6 +1241,7 @@ mod test {
AuthenticatedClient::default(),
cache_dir.path(),
Default::default(),
None,
)
.await
.unwrap();
Expand Down Expand Up @@ -1283,6 +1290,7 @@ mod test {
AuthenticatedClient::default(),
cache_dir.path(),
Default::default(),
None,
)
.await
.unwrap();
Expand Down Expand Up @@ -1335,6 +1343,7 @@ mod test {
authenticated_client,
cache_dir.path(),
Default::default(),
None,
)
.await
.unwrap();
Expand Down Expand Up @@ -1367,9 +1376,9 @@ mod test {
AuthenticatedClient::default(),
cache_dir.path(),
FetchRepoDataOptions {
download_progress: Some(Box::new(download_progress)),
..Default::default()
},
Some(Box::new(download_progress)),
)
.await
.unwrap();
Expand All @@ -1394,6 +1403,7 @@ mod test {
FetchRepoDataOptions {
..Default::default()
},
None,
)
.await;

Expand All @@ -1417,6 +1427,7 @@ mod test {
FetchRepoDataOptions {
..Default::default()
},
None,
)
.await;

Expand Down
3 changes: 2 additions & 1 deletion crates/rattler_repodata_gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
//! repodata_url,
//! client,
//! cache,
//! fetch::FetchRepoDataOptions { ..Default::default() }
//! fetch::FetchRepoDataOptions { ..Default::default() },
//! None,
//! ).await;
//!
//! let result = match result {
Expand Down