use crate::io::walk::S3Walker;
use crate::types::FailedTransferPolicy;
use aws_smithy_types::error::operation::BuildError;
use std::{
fmt,
path::{Path, PathBuf},
};
#[non_exhaustive]
#[derive(Clone)]
pub struct DownloadObjectsInput {
pub bucket: Option<String>,
pub destination: Option<PathBuf>,
pub delimiter: Option<String>,
pub failure_policy: FailedTransferPolicy,
pub walker: Option<S3Walker>,
pub max_concurrent_downloads: Option<usize>,
}
impl DownloadObjectsInput {
pub fn builder() -> DownloadObjectsInputBuilder {
DownloadObjectsInputBuilder::default()
}
pub fn bucket(&self) -> Option<&str> {
self.bucket.as_deref()
}
pub fn destination(&self) -> Option<&Path> {
self.destination.as_deref()
}
pub fn delimiter(&self) -> Option<&str> {
self.delimiter.as_deref()
}
pub fn failure_policy(&self) -> &FailedTransferPolicy {
&self.failure_policy
}
pub fn walker(&self) -> Option<&S3Walker> {
self.walker.as_ref()
}
pub fn max_concurrent_downloads(&self) -> Option<usize> {
self.max_concurrent_downloads
}
}
impl fmt::Debug for DownloadObjectsInput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DownloadObjectsInput")
.field("bucket", &self.bucket)
.field("destination", &self.destination)
.field("delimiter", &self.delimiter)
.field("failure_policy", &self.failure_policy)
.field("walker", &self.walker.is_some())
.field("max_concurrent_downloads", &self.max_concurrent_downloads)
.finish()
}
}
#[non_exhaustive]
#[derive(Clone, Default)]
pub struct DownloadObjectsInputBuilder {
pub(crate) bucket: Option<String>,
pub(crate) destination: Option<PathBuf>,
pub(crate) delimiter: Option<String>,
pub(crate) failure_policy: FailedTransferPolicy,
pub(crate) walker: Option<S3Walker>,
pub(crate) max_concurrent_downloads: Option<usize>,
}
impl DownloadObjectsInputBuilder {
pub fn bucket(mut self, input: impl Into<String>) -> Self {
self.bucket = Some(input.into());
self
}
pub fn set_bucket(mut self, input: Option<String>) -> Self {
self.bucket = input;
self
}
pub fn get_bucket(&self) -> Option<&str> {
self.bucket.as_deref()
}
pub fn destination(mut self, input: impl Into<PathBuf>) -> Self {
self.destination = Some(input.into());
self
}
pub fn set_destination(mut self, input: Option<PathBuf>) -> Self {
self.destination = input;
self
}
pub fn get_destination(&self) -> Option<&Path> {
self.destination.as_deref()
}
pub fn delimiter(mut self, input: impl Into<String>) -> Self {
self.delimiter = Some(input.into());
self
}
pub fn set_delimiter(mut self, input: Option<String>) -> Self {
self.delimiter = input;
self
}
pub fn get_delimiter(&self) -> Option<&str> {
self.delimiter.as_deref()
}
pub fn failure_policy(mut self, input: FailedTransferPolicy) -> Self {
self.failure_policy = input;
self
}
pub fn set_failure_policy(mut self, input: Option<FailedTransferPolicy>) -> Self {
self.failure_policy = input.unwrap_or_default();
self
}
pub fn get_failure_policy(&self) -> &FailedTransferPolicy {
&self.failure_policy
}
pub fn walker(mut self, input: S3Walker) -> Self {
self.walker = Some(input);
self
}
pub fn set_walker(mut self, input: Option<S3Walker>) -> Self {
self.walker = input;
self
}
pub fn get_walker(&self) -> Option<&S3Walker> {
self.walker.as_ref()
}
pub fn max_concurrent_downloads(mut self, input: usize) -> Self {
self.max_concurrent_downloads = Some(input);
self
}
pub fn set_max_concurrent_downloads(mut self, input: Option<usize>) -> Self {
self.max_concurrent_downloads = input;
self
}
pub fn get_max_concurrent_downloads(&self) -> Option<usize> {
self.max_concurrent_downloads
}
pub fn build(self) -> Result<DownloadObjectsInput, BuildError> {
if self.bucket.is_none() {
return Err(BuildError::missing_field("bucket", "A bucket is required"));
}
if self.destination.is_none() {
return Err(BuildError::missing_field(
"destination",
"Destination directory is required",
));
}
Ok(DownloadObjectsInput {
bucket: self.bucket,
destination: self.destination,
delimiter: self.delimiter,
failure_policy: self.failure_policy,
walker: self.walker,
max_concurrent_downloads: self.max_concurrent_downloads,
})
}
}
impl fmt::Debug for DownloadObjectsInputBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DownloadObjectsInputBuilder")
.field("bucket", &self.bucket)
.field("destination", &self.destination)
.field("delimiter", &self.delimiter)
.field("failure_policy", &self.failure_policy)
.field("walker", &self.walker.is_some())
.field("max_concurrent_downloads", &self.max_concurrent_downloads)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::DownloadObjectsInput;
use crate::types::FailedTransferPolicy;
#[test]
fn set_failure_policy_replaces_and_none_restores_default() {
let builder = DownloadObjectsInput::builder()
.bucket("test-bucket")
.destination("/tmp/test")
.set_failure_policy(Some(FailedTransferPolicy::Continue));
assert_eq!(
builder.get_failure_policy(),
&FailedTransferPolicy::Continue
);
let input = builder
.set_failure_policy(None)
.build()
.expect("valid input");
assert_eq!(input.failure_policy(), &FailedTransferPolicy::Abort);
}
#[test]
fn test_no_destination_error() {
let err = DownloadObjectsInput::builder()
.bucket("test-bucket")
.build()
.unwrap_err();
assert!(err
.to_string()
.contains("Destination directory is required"));
}
#[test]
fn test_no_bucket_error() {
let err = DownloadObjectsInput::builder()
.destination("/tmp/test")
.build()
.unwrap_err();
assert!(err.to_string().contains("A bucket is required"));
}
#[test]
fn max_concurrent_downloads_zero_clamped_to_one() {
let input = DownloadObjectsInput::builder()
.bucket("test-bucket")
.destination("/tmp/test")
.max_concurrent_downloads(0)
.build()
.expect("valid input");
let resolved = input
.max_concurrent_downloads()
.map(|n| n.max(1))
.unwrap_or(crate::operation::DEFAULT_MAX_CONCURRENT_CHILDREN);
assert_eq!(
1, resolved,
"an explicit 0 must be clamped to 1 to avoid wedging the transfer"
);
}
#[test]
fn max_concurrent_downloads_none_uses_default() {
let input = DownloadObjectsInput::builder()
.bucket("test-bucket")
.destination("/tmp/test")
.build()
.expect("valid input");
let resolved = input
.max_concurrent_downloads()
.map(|n| n.max(1))
.unwrap_or(crate::operation::DEFAULT_MAX_CONCURRENT_CHILDREN);
assert_eq!(crate::operation::DEFAULT_MAX_CONCURRENT_CHILDREN, resolved);
}
#[test]
fn max_concurrent_downloads_nonzero_preserved() {
let input = DownloadObjectsInput::builder()
.bucket("test-bucket")
.destination("/tmp/test")
.max_concurrent_downloads(4)
.build()
.expect("valid input");
let resolved = input
.max_concurrent_downloads()
.map(|n| n.max(1))
.unwrap_or(crate::operation::DEFAULT_MAX_CONCURRENT_CHILDREN);
assert_eq!(4, resolved);
}
}