Skip to content

Commit 38a191d

Browse files
authored
refactor: make Config mutable, excepting the backend configurations. (#29)
* refactor: make `Config` mutable, excepting the backend configurations. This commit makes `Config` mutable for everything except for the inner backend configurations while still keeping it cheaply cloneable; the backend configuration may have long strings for authentication. With these changes, a `Config` can be cloned and a setting like `overwrite` can be changed as needed depending on the context. * chore: update CHANGELOG. * chore: code review feedback.
1 parent 14757b7 commit 38a191d

File tree

2 files changed

+102
-45
lines changed

2 files changed

+102
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717

1818
#### Changed
1919

20+
* `Config` now has mutable fields ([#29](https://github.com/stjude-rust-labs/cloud-copy/pull/29)).
2021
* `Config` now implements a builder pattern for setting configuration options ([#26](https://github.com/stjude-rust-labs/cloud-copy/pull/26)).
2122

2223
## 0.4.0 - 10-13-2025

src/config.rs

Lines changed: 101 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -330,27 +330,9 @@ impl GoogleConfig {
330330
}
331331
}
332332

333-
/// Stores the inner configuration for [`Config`].
333+
/// Stores the inner backends configuration for [`Config`].
334334
#[derive(Debug, Default, Deserialize)]
335-
struct ConfigInner {
336-
/// The hash algorithm to use for calculating content digests.
337-
#[serde(default)]
338-
algorithm: HashAlgorithm,
339-
/// Stores whether or not we're linking to cache entries.
340-
#[serde(default)]
341-
link_to_cache: bool,
342-
/// Stores whether or not the destination should be overwritten.
343-
#[serde(default)]
344-
overwrite: bool,
345-
/// Stores the block size to use for file transfers.
346-
#[serde(default)]
347-
block_size: Option<u64>,
348-
/// Stores the parallelism level for network operations.
349-
#[serde(default)]
350-
parallelism: Option<usize>,
351-
/// Stores the number of retries to attempt for network operations.
352-
#[serde(default)]
353-
retries: Option<usize>,
335+
struct BackendsConfig {
354336
/// Stores the Azure Storage configuration.
355337
#[serde(default)]
356338
azure: AzureConfig,
@@ -364,13 +346,28 @@ struct ConfigInner {
364346

365347
/// Used to build a [`Config`].
366348
#[derive(Debug, Default)]
367-
pub struct ConfigBuilder(ConfigInner);
349+
pub struct ConfigBuilder {
350+
/// The hash algorithm to use for calculating content digests.
351+
algorithm: HashAlgorithm,
352+
/// Stores whether or not we're linking to cache entries.
353+
link_to_cache: bool,
354+
/// Stores whether or not the destination should be overwritten.
355+
overwrite: bool,
356+
/// Stores the block size to use for file transfers.
357+
block_size: Option<u64>,
358+
/// Stores the parallelism level for network operations.
359+
parallelism: Option<usize>,
360+
/// Stores the number of retries to attempt for network operations.
361+
retries: Option<usize>,
362+
/// The backends configuration for the `Config`.
363+
backends: BackendsConfig,
364+
}
368365

369366
impl ConfigBuilder {
370367
/// Sets the hash algorithm to use for calculating content digests of
371368
/// uploads.
372369
pub fn with_hash_algorithm(mut self, algorithm: HashAlgorithm) -> Self {
373-
self.0.algorithm = algorithm;
370+
self.algorithm = algorithm;
374371
self
375372
}
376373

@@ -391,7 +388,7 @@ impl ConfigBuilder {
391388
///
392389
/// When `false`, a copy to the destination is always performed.
393390
pub fn with_link_to_cache(mut self, link_to_cache: bool) -> Self {
394-
self.0.link_to_cache = link_to_cache;
391+
self.link_to_cache = link_to_cache;
395392
self
396393
}
397394

@@ -404,15 +401,15 @@ impl ConfigBuilder {
404401
/// be made for the URL; if the request succeeds, the copy operation will
405402
/// fail.
406403
pub fn with_overwrite(mut self, overwrite: bool) -> Self {
407-
self.0.overwrite = overwrite;
404+
self.overwrite = overwrite;
408405
self
409406
}
410407

411408
/// Sets the block size to use for file transfers.
412409
///
413410
/// The default block size depends on the cloud storage service.
414411
pub fn with_block_size(mut self, block_size: u64) -> Self {
415-
self.0.block_size = Some(block_size);
412+
self.block_size = Some(block_size);
416413
self
417414
}
418415

@@ -422,7 +419,7 @@ impl ConfigBuilder {
422419
///
423420
/// The default block size depends on the cloud storage service.
424421
pub fn with_maybe_block_size(mut self, block_size: Option<u64>) -> Self {
425-
self.0.block_size = block_size;
422+
self.block_size = block_size;
426423
self
427424
}
428425

@@ -437,7 +434,7 @@ impl ConfigBuilder {
437434
/// Defaults to the host's available parallelism (or 1 if it cannot be
438435
/// determined).
439436
pub fn with_parallelism(mut self, parallelism: usize) -> Self {
440-
self.0.parallelism = Some(parallelism);
437+
self.parallelism = Some(parallelism);
441438
self
442439
}
443440

@@ -454,15 +451,15 @@ impl ConfigBuilder {
454451
/// Defaults to the host's available parallelism (or 1 if it cannot be
455452
/// determined).
456453
pub fn with_maybe_parallelism(mut self, parallelism: Option<usize>) -> Self {
457-
self.0.parallelism = parallelism;
454+
self.parallelism = parallelism;
458455
self
459456
}
460457

461458
/// Sets the number of retries to attempt for network operations.
462459
///
463460
/// Defaults to `5`.
464461
pub fn with_retries(mut self, retries: usize) -> Self {
465-
self.0.retries = Some(retries);
462+
self.retries = Some(retries);
466463
self
467464
}
468465

@@ -472,39 +469,69 @@ impl ConfigBuilder {
472469
///
473470
/// Defaults to `5`.
474471
pub fn with_maybe_retries(mut self, retries: Option<usize>) -> Self {
475-
self.0.retries = retries;
472+
self.retries = retries;
476473
self
477474
}
478475

479476
/// Sets the Azure Storage configuration to use.
480477
pub fn with_azure(mut self, azure: AzureConfig) -> Self {
481-
self.0.azure = azure;
478+
self.backends.azure = azure;
482479
self
483480
}
484481

485482
/// Sets the Amazon S3 configuration to use.
486483
pub fn with_s3(mut self, s3: S3Config) -> Self {
487-
self.0.s3 = s3;
484+
self.backends.s3 = s3;
488485
self
489486
}
490487

491488
/// Sets the Google Cloud Storage configuration to use.
492489
pub fn with_google(mut self, google: GoogleConfig) -> Self {
493-
self.0.google = google;
490+
self.backends.google = google;
494491
self
495492
}
496493

497494
/// Consumes the builder and returns the [`Config`].
498495
pub fn build(self) -> Config {
499-
Config(Arc::new(self.0))
496+
Config {
497+
algorithm: self.algorithm,
498+
link_to_cache: self.link_to_cache,
499+
overwrite: self.overwrite,
500+
block_size: self.block_size,
501+
parallelism: self.parallelism,
502+
retries: self.retries,
503+
backends: Arc::new(self.backends),
504+
}
500505
}
501506
}
502507

503508
/// Configuration used in a cloud copy operation.
504509
///
505510
/// A [`Config`] is cheaply cloned.
506511
#[derive(Debug, Clone, Default, Deserialize)]
507-
pub struct Config(Arc<ConfigInner>);
512+
pub struct Config {
513+
/// The hash algorithm to use for calculating content digests.
514+
#[serde(default)]
515+
algorithm: HashAlgorithm,
516+
/// Stores whether or not we're linking to cache entries.
517+
#[serde(default)]
518+
link_to_cache: bool,
519+
/// Stores whether or not the destination should be overwritten.
520+
#[serde(default)]
521+
overwrite: bool,
522+
/// Stores the block size to use for file transfers.
523+
#[serde(default)]
524+
block_size: Option<u64>,
525+
/// Stores the parallelism level for network operations.
526+
#[serde(default)]
527+
parallelism: Option<usize>,
528+
/// Stores the number of retries to attempt for network operations.
529+
#[serde(default)]
530+
retries: Option<usize>,
531+
/// Stores the backends configuration.
532+
#[serde(default)]
533+
backends: Arc<BackendsConfig>,
534+
}
508535

509536
impl Config {
510537
/// Gets a [`ConfigBuilder`] for building a new [`Config`].
@@ -514,7 +541,12 @@ impl Config {
514541

515542
/// Gets the hash algorithm used for calculating content digests of uploads.
516543
pub fn hash_algorithm(&self) -> HashAlgorithm {
517-
self.0.algorithm
544+
self.algorithm
545+
}
546+
547+
/// Sets the hash algorithm used for calculating content digests of uploads.
548+
pub fn set_hash_algorithm(&mut self, algorithm: HashAlgorithm) {
549+
self.algorithm = algorithm;
518550
}
519551

520552
/// Gets whether or not cache entries should be linked.
@@ -534,7 +566,12 @@ impl Config {
534566
///
535567
/// When `false`, a copy to the destination is always performed.
536568
pub fn link_to_cache(&self) -> bool {
537-
self.0.link_to_cache
569+
self.link_to_cache
570+
}
571+
572+
/// Sets whether or not cache entries should be linked.
573+
pub fn set_link_to_cache(&mut self, link_to_cache: bool) {
574+
self.link_to_cache = link_to_cache;
538575
}
539576

540577
/// Gets whether or not the destination should be overwritten.
@@ -546,14 +583,24 @@ impl Config {
546583
/// be made for the URL; if the request succeeds, the copy operation will
547584
/// fail.
548585
pub fn overwrite(&self) -> bool {
549-
self.0.overwrite
586+
self.overwrite
587+
}
588+
589+
/// Sets whether or not the destination should be overwritten.
590+
pub fn set_overwrite(&mut self, overwrite: bool) {
591+
self.overwrite = overwrite;
550592
}
551593

552594
/// Gets the block size to use for file transfers.
553595
///
554596
/// The default block size depends on the cloud storage service.
555597
pub fn block_size(&self) -> Option<u64> {
556-
self.0.block_size
598+
self.block_size
599+
}
600+
601+
/// Sets the block size fot use for file transfers.
602+
pub fn set_block_size(&mut self, block_size: u64) {
603+
self.block_size = Some(block_size);
557604
}
558605

559606
/// Gets the parallelism supported for uploads and downloads.
@@ -567,31 +614,40 @@ impl Config {
567614
/// Defaults to the host's available parallelism (or 1 if it cannot be
568615
/// determined).
569616
pub fn parallelism(&self) -> usize {
570-
self.0
571-
.parallelism
617+
self.parallelism
572618
.unwrap_or_else(|| available_parallelism().map(NonZero::get).unwrap_or(1))
573619
}
574620

621+
/// Sets the parallelism supported for uploads and downloads.
622+
pub fn set_parallelism(&mut self, parallelism: usize) {
623+
self.parallelism = Some(parallelism);
624+
}
625+
575626
/// Gets the number of retries to attempt for network operations.
576627
///
577628
/// Defaults to `5`.
578629
pub fn retries(&self) -> usize {
579-
self.0.retries.unwrap_or(DEFAULT_RETRIES)
630+
self.retries.unwrap_or(DEFAULT_RETRIES)
631+
}
632+
633+
/// Sets the number of retries to attempt for network operations.
634+
pub fn set_retries(&mut self, retries: usize) {
635+
self.retries = Some(retries);
580636
}
581637

582638
/// Gets the Azure Storage configuration.
583639
pub fn azure(&self) -> &AzureConfig {
584-
&self.0.azure
640+
&self.backends.azure
585641
}
586642

587643
/// Gets the Amazon S3 configuration.
588644
pub fn s3(&self) -> &S3Config {
589-
&self.0.s3
645+
&self.backends.s3
590646
}
591647

592648
/// Gets the Google Cloud Storage configuration.
593649
pub fn google(&self) -> &GoogleConfig {
594-
&self.0.google
650+
&self.backends.google
595651
}
596652

597653
/// Gets an iterator over the retry durations for network operations.

0 commit comments

Comments
 (0)