1use std::fmt::{self, Display};
7use std::ops;
8use std::str::FromStr;
9use std::time::Duration;
10
11pub mod checksum;
12mod checksum_strategy;
13pub use checksum_strategy::ChecksumStrategy;
14
15pub use crate::model::builders;
16pub use crate::model::{
17 ArchiveStatus, ChecksumAlgorithm, ChecksumMode, ChecksumType, ChunkMetadata, DownloadInput,
18 Object, ObjectCannedAcl, ObjectLockLegalHoldStatus, ObjectLockMode, ObjectMetadata,
19 ObjectStorageClass, Owner, ReplicationStatus, RequestCharged, RequestPayer, RestoreStatus,
20 ServerSideEncryption, StorageClass, UploadInput, UploadOutput,
21};
22
23#[non_exhaustive]
27#[derive(Debug, Clone, Default)]
28pub enum FailedMultipartUploadPolicy {
29 #[default]
31 AbortUpload,
32 Retain,
42}
43
44#[derive(Debug, Default)]
46pub struct AbortedUpload {
47 pub(crate) upload_id: Option<String>,
48 pub(crate) request_charged: Option<crate::model::RequestCharged>,
49 pub(crate) outcome: AbortOutcome,
50 pub(crate) output: Option<crate::operation::upload::UploadOutput>,
51}
52
53#[non_exhaustive]
55#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
56pub enum AbortOutcome {
57 #[default]
65 Cancelled,
66 AlreadyCompleted,
69 Retained,
72}
73
74impl AbortedUpload {
75 pub fn outcome(&self) -> AbortOutcome {
78 self.outcome
79 }
80
81 pub fn output(&self) -> Option<&crate::operation::upload::UploadOutput> {
84 self.output.as_ref()
85 }
86 pub fn upload_id(&self) -> Option<&str> {
91 self.upload_id.as_deref()
92 }
93
94 pub fn request_charged(&self) -> &Option<crate::model::RequestCharged> {
99 &self.request_charged
100 }
101}
102
103#[non_exhaustive]
108#[derive(Debug, Clone, Default, PartialEq)]
109pub enum FailedTransferPolicy {
110 #[default]
112 Abort,
113 Continue,
116}
117
118#[non_exhaustive]
120#[derive(Debug)]
121pub struct FailedDownload {
122 pub(crate) input: crate::operation::download::DownloadInput,
124
125 pub(crate) error: crate::error::Error,
127}
128
129impl FailedDownload {
130 pub fn input(&self) -> &crate::operation::download::DownloadInput {
132 &self.input
133 }
134
135 pub fn error(&self) -> &crate::error::Error {
137 &self.error
138 }
139}
140
141#[non_exhaustive]
143#[derive(Debug)]
144pub struct FailedUpload {
145 pub(crate) input: Option<crate::operation::upload::UploadInput>,
146 pub(crate) error: crate::error::Error,
147 pub(crate) source_path: Option<std::path::PathBuf>,
149}
150
151impl FailedUpload {
152 pub fn input(&self) -> Option<&crate::operation::upload::UploadInput> {
154 self.input.as_ref()
155 }
156
157 pub fn error(&self) -> &crate::error::Error {
159 &self.error
160 }
161
162 pub fn source_path(&self) -> Option<&std::path::Path> {
164 self.source_path.as_deref()
165 }
166}
167
168#[derive(Debug, Clone, Copy, PartialEq, Eq)]
170#[non_exhaustive]
171pub enum TransferStatus {
172 Active,
174 Completed,
176 Failed,
178 Cancelled,
180}
181
182impl TransferStatus {
183 pub fn is_terminal(&self) -> bool {
185 !matches!(self, Self::Active)
186 }
187}
188
189#[derive(Debug, Clone, Copy, PartialEq)]
191#[non_exhaustive]
192pub(crate) enum BucketType {
193 Standard,
194 Express,
195}
196
197impl BucketType {
198 pub(crate) fn from_bucket_name(bucket_name: &str) -> Self {
199 if bucket_name.ends_with("--x-s3") {
200 BucketType::Express
201 } else {
202 BucketType::Standard
203 }
204 }
205}
206
207#[derive(Debug, Clone)]
209pub(crate) struct Bucket {
210 name: String,
211 kind: BucketType,
212}
213
214impl Bucket {
215 pub(crate) fn new(name: String) -> Self {
216 let kind = BucketType::from_bucket_name(&name);
217 Self { name, kind }
218 }
219
220 pub(crate) fn name(&self) -> &str {
221 &self.name
222 }
223
224 pub(crate) fn kind(&self) -> BucketType {
225 self.kind
226 }
227}
228
229#[non_exhaustive]
249#[derive(Debug, Clone)]
250pub struct IntegrityChecks {
251 checksum_crc32: Option<String>,
252 checksum_crc32c: Option<String>,
253 checksum_crc64_nvme: Option<String>,
254 checksum_sha1: Option<String>,
255 checksum_sha256: Option<String>,
256 checksum_sha512: Option<String>,
257 checksum_md5: Option<String>,
258 checksum_xxhash64: Option<String>,
259 checksum_xxhash3: Option<String>,
260 checksum_xxhash128: Option<String>,
261 checksum_type: Option<crate::model::ChecksumType>,
262 checksum_validation: ChecksumValidation,
263}
264
265impl IntegrityChecks {
266 pub fn checksum_validation(&self) -> &ChecksumValidation {
268 &self.checksum_validation
269 }
270
271 pub fn checksum_crc32(&self) -> Option<&str> {
273 self.checksum_crc32.as_deref()
274 }
275
276 pub fn checksum_crc32c(&self) -> Option<&str> {
278 self.checksum_crc32c.as_deref()
279 }
280
281 pub fn checksum_crc64_nvme(&self) -> Option<&str> {
283 self.checksum_crc64_nvme.as_deref()
284 }
285
286 pub fn checksum_sha1(&self) -> Option<&str> {
288 self.checksum_sha1.as_deref()
289 }
290
291 pub fn checksum_sha256(&self) -> Option<&str> {
293 self.checksum_sha256.as_deref()
294 }
295
296 pub fn checksum_sha512(&self) -> Option<&str> {
298 self.checksum_sha512.as_deref()
299 }
300
301 pub fn checksum_md5(&self) -> Option<&str> {
303 self.checksum_md5.as_deref()
304 }
305
306 pub fn checksum_xxhash64(&self) -> Option<&str> {
308 self.checksum_xxhash64.as_deref()
309 }
310
311 pub fn checksum_xxhash3(&self) -> Option<&str> {
313 self.checksum_xxhash3.as_deref()
314 }
315
316 pub fn checksum_xxhash128(&self) -> Option<&str> {
318 self.checksum_xxhash128.as_deref()
319 }
320
321 pub fn checksum_type(&self) -> Option<&crate::model::ChecksumType> {
323 self.checksum_type.as_ref()
324 }
325}
326
327#[non_exhaustive]
339#[derive(Debug, Clone, PartialEq, Eq)]
340pub enum ChecksumValidation {
341 #[non_exhaustive]
343 Validated {
344 algorithm: crate::model::ChecksumAlgorithm,
347 },
348 #[non_exhaustive]
350 NotValidated {
351 reason: NotValidatedReason,
353 },
354}
355
356#[non_exhaustive]
362#[derive(Debug, Clone, Copy, PartialEq, Eq)]
363pub enum NotValidatedReason {
364 Disabled,
367 CompositeChecksum,
371 PartialCoverage,
373 Unavailable,
376 UnsupportedAlgorithm,
379}
380
381impl IntegrityChecks {
382 pub(crate) fn new(
385 values: Option<&crate::model::ObjectMetadata>,
386 checksum_type: Option<crate::model::ChecksumType>,
387 checksum_validation: ChecksumValidation,
388 ) -> Self {
389 let value = |f: fn(&crate::model::ObjectMetadata) -> &Option<String>| {
390 values.and_then(|m| f(m).clone())
391 };
392 Self {
393 checksum_crc32: value(|m| &m.checksum_crc32),
394 checksum_crc32c: value(|m| &m.checksum_crc32_c),
395 checksum_crc64_nvme: value(|m| &m.checksum_crc64_nvme),
396 checksum_sha1: value(|m| &m.checksum_sha1),
397 checksum_sha256: value(|m| &m.checksum_sha256),
398 checksum_sha512: value(|m| &m.checksum_sha512),
399 checksum_md5: value(|m| &m.checksum_md5),
400 checksum_xxhash64: value(|m| &m.checksum_xxhash64),
401 checksum_xxhash3: value(|m| &m.checksum_xxhash3),
402 checksum_xxhash128: value(|m| &m.checksum_xxhash128),
403 checksum_type,
404 checksum_validation,
405 }
406 }
407}
408
409#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
424pub struct ByteSize(u64);
425
426impl ByteSize {
427 pub const fn new(bytes: u64) -> Self {
429 Self(bytes)
430 }
431
432 pub const fn kib(n: u64) -> Self {
439 Self(mul(n, ByteUnit::Kibibyte.as_bytes_u64()))
440 }
441
442 pub const fn mib(n: u64) -> Self {
449 Self(mul(n, ByteUnit::Mebibyte.as_bytes_u64()))
450 }
451
452 pub const fn gib(n: u64) -> Self {
459 Self(mul(n, ByteUnit::Gibibyte.as_bytes_u64()))
460 }
461
462 pub const fn as_u64(self) -> u64 {
464 self.0
465 }
466
467 pub const fn as_usize(self) -> usize {
475 assert!(self.0 <= usize::MAX as u64, "byte count exceeds usize::MAX");
476 self.0 as usize
477 }
478
479 pub fn as_unit(self, unit: ByteUnit) -> f64 {
481 unit.convert(self.0)
482 }
483
484 pub fn display_as(self, unit: ByteUnit) -> ByteSizeDisplayContext {
486 ByteSizeDisplayContext {
487 total_bytes: self.0,
488 unit,
489 }
490 }
491}
492
493const fn mul(n: u64, unit_bytes: u64) -> u64 {
494 match n.checked_mul(unit_bytes) {
495 Some(bytes) => bytes,
496 None => panic!("byte count exceeds u64::MAX"),
497 }
498}
499
500impl fmt::Display for ByteSize {
501 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
503 let units = &[ByteUnit::Gibibyte, ByteUnit::Mebibyte, ByteUnit::Kibibyte];
504 let mut unit = ByteUnit::Byte;
505 for u in units {
506 if self.0 >= u.as_bytes_u64() {
507 unit = *u;
508 break;
509 }
510 }
511 fmt::Display::fmt(&self.display_as(unit), f)
512 }
513}
514
515#[non_exhaustive]
520#[derive(Debug, Clone, Copy, PartialEq, Eq)]
521pub enum ByteUnit {
522 Byte,
524 Kilobit,
526 Kibibyte,
528 Megabit,
530 Mebibyte,
532 Gigabit,
534 Gibibyte,
536}
537
538impl ByteUnit {
539 pub(crate) fn convert(&self, bytes: u64) -> f64 {
541 bytes as f64 / self.as_bytes_u64() as f64
542 }
543
544 pub(crate) const fn as_bytes_u64(&self) -> u64 {
546 match self {
547 ByteUnit::Byte => 1,
548 ByteUnit::Kilobit => 125,
549 ByteUnit::Kibibyte => 1 << 10,
550 ByteUnit::Megabit => 125_000,
551 ByteUnit::Mebibyte => 1 << 20,
552 ByteUnit::Gigabit => 125_000_000,
553 ByteUnit::Gibibyte => 1 << 30,
554 }
555 }
556
557 pub(crate) const fn as_str(&self) -> &'static str {
558 match self {
559 ByteUnit::Byte => "B",
560 ByteUnit::Kilobit => "Kb",
561 ByteUnit::Kibibyte => "KiB",
562 ByteUnit::Megabit => "Mb",
563 ByteUnit::Mebibyte => "MiB",
564 ByteUnit::Gigabit => "Gb",
565 ByteUnit::Gibibyte => "GiB",
566 }
567 }
568}
569
570impl AsRef<str> for ByteUnit {
571 fn as_ref(&self) -> &str {
572 self.as_str()
573 }
574}
575
576impl FromStr for ByteUnit {
577 type Err = crate::error::Error;
578
579 fn from_str(s: &str) -> Result<Self, Self::Err> {
580 let unit = match s {
581 "B" => ByteUnit::Byte,
582 "Kb" => ByteUnit::Kilobit,
583 "KiB" => ByteUnit::Kibibyte,
584 "Mb" => ByteUnit::Megabit,
585 "MiB" => ByteUnit::Mebibyte,
586 "Gb" => ByteUnit::Gigabit,
587 "GiB" => ByteUnit::Gibibyte,
588 _ => {
589 return Err(crate::error::invalid_input(format!(
590 "unknown byte unit '{}'",
591 s
592 )))
593 }
594 };
595
596 Ok(unit)
597 }
598}
599
600#[derive(Debug)]
606pub struct ByteSizeDisplayContext {
607 total_bytes: u64,
608 unit: ByteUnit,
609}
610
611impl fmt::Display for ByteSizeDisplayContext {
612 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
613 if self.total_bytes.is_multiple_of(self.unit.as_bytes_u64()) {
614 let converted = self.total_bytes / self.unit.as_bytes_u64();
615 return write!(f, "{converted} {}", self.unit.as_str());
616 }
617 let precision = f.precision().unwrap_or(3);
618 write!(
619 f,
620 "{1:.*} {2:}",
621 precision,
622 self.unit.convert(self.total_bytes),
623 self.unit.as_str()
624 )
625 }
626}
627
628#[derive(Debug, Clone, Copy)]
630pub struct Throughput {
631 bytes_transferred: u64,
632 elapsed: Duration,
633}
634
635impl Throughput {
636 pub const fn new(bytes_transferred: u64, elapsed: Duration) -> Throughput {
638 Throughput {
639 bytes_transferred,
640 elapsed,
641 }
642 }
643
644 pub const fn new_bytes_per_sec(bytes_transferred: u64) -> Throughput {
658 Self::new(bytes_transferred, Duration::from_secs(1))
659 }
660
661 pub fn as_unit_per_sec(&self, unit: ByteUnit) -> f64 {
663 (self.bytes_transferred as f64 / unit.as_bytes_u64() as f64) / self.elapsed.as_secs_f64()
664 }
665
666 pub fn as_bytes_per_sec(&self) -> f64 {
668 self.as_unit_per_sec(ByteUnit::Byte)
669 }
670
671 pub const fn bytes_transferred(&self) -> u64 {
673 self.bytes_transferred
674 }
675
676 pub fn display_as(&self, unit: ByteUnit) -> ThroughputDisplayContext<'_> {
678 ThroughputDisplayContext {
679 throughput: self,
680 unit,
681 }
682 }
683}
684
685impl PartialEq for Throughput {
686 fn eq(&self, other: &Self) -> bool {
687 self.as_bytes_per_sec() == other.as_bytes_per_sec()
688 }
689}
690
691impl PartialOrd for Throughput {
692 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
693 self.as_bytes_per_sec()
694 .partial_cmp(&other.as_bytes_per_sec())
695 }
696}
697
698impl ops::Add for Throughput {
700 type Output = Self;
701
702 fn add(self, rhs: Self) -> Self::Output {
703 let bps = self.as_bytes_per_sec() + rhs.as_bytes_per_sec();
704
705 Throughput::new_bytes_per_sec(bps.round() as u64)
706 }
707}
708
709impl ops::Sub for Throughput {
711 type Output = Self;
712
713 fn sub(self, rhs: Self) -> Self::Output {
714 let bps = self.as_bytes_per_sec() - rhs.as_bytes_per_sec();
715 Throughput::new_bytes_per_sec(bps.round() as u64)
716 }
717}
718
719impl ops::Mul<u64> for Throughput {
721 type Output = Self;
722
723 fn mul(self, rhs: u64) -> Self::Output {
724 Throughput::new(self.bytes_transferred * rhs, self.elapsed)
725 }
726}
727
728impl ops::Div<u64> for Throughput {
730 type Output = Self;
731
732 fn div(self, rhs: u64) -> Self::Output {
733 Throughput::new(self.bytes_transferred / rhs, self.elapsed)
734 }
735}
736
737impl fmt::Display for Throughput {
738 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
739 let context = ThroughputDisplayContext {
740 throughput: self,
741 unit: ByteUnit::Megabit,
742 };
743 Display::fmt(&context, f)
744 }
745}
746
747#[derive(Debug)]
753pub struct ThroughputDisplayContext<'a> {
754 throughput: &'a Throughput,
755 unit: ByteUnit,
756}
757
758impl fmt::Display for ThroughputDisplayContext<'_> {
759 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
760 if let Some(precision) = f.precision() {
761 write!(
762 f,
763 "{1:.*} {2:}/s",
764 precision,
765 self.throughput.as_unit_per_sec(self.unit),
766 self.unit.as_str()
767 )
768 } else {
769 write!(
770 f,
771 "{} {}/s",
772 self.throughput.as_unit_per_sec(self.unit),
773 self.unit.as_str()
774 )
775 }
776 }
777}
778
779#[cfg(test)]
780mod tests {
781 use crate::config::TargetThroughput;
782
783 #[test]
784 fn target_throughput_accepts_finite_positive_gbps() {
785 for gbps in [0.5, 1.0, 200.0] {
786 let target = TargetThroughput::gbps(gbps).unwrap();
787 assert_eq!(target.as_gbps(), gbps);
788 }
789 }
790
791 #[test]
792 fn target_throughput_rejects_values_that_are_not_a_rate() {
793 for gbps in [0.0, -1.0, f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
794 assert!(TargetThroughput::gbps(gbps).is_err(), "gbps {gbps}");
795 }
796 }
797}
798
799#[cfg(test)]
800mod value_type_tests {
801 use super::{ByteSize, ByteUnit, Throughput};
802 use std::str::FromStr;
803 use std::time::Duration;
804
805 #[test]
809 fn byte_unit_accessors_agree() {
810 let units = [
811 (ByteUnit::Byte, 1u64),
812 (ByteUnit::Kilobit, 125),
813 (ByteUnit::Kibibyte, 1024),
814 (ByteUnit::Megabit, 125_000),
815 (ByteUnit::Mebibyte, 1024 * 1024),
816 (ByteUnit::Gigabit, 125_000_000),
817 (ByteUnit::Gibibyte, 1024 * 1024 * 1024),
818 ];
819 for (unit, bytes) in units {
820 assert_eq!(unit.as_bytes_u64(), bytes, "{unit:?} bytes");
821 }
822 }
823
824 #[test]
825 fn byte_size_constructors_and_display() {
826 assert_eq!(ByteSize::kib(2).as_u64(), 2 * 1024);
827 assert_eq!(ByteSize::mib(8).as_u64(), 8 * 1024 * 1024);
828 assert_eq!(ByteSize::gib(3).as_u64(), 3 * 1024 * 1024 * 1024);
829
830 assert_eq!(ByteSize::new(512).to_string(), "512 B");
832 assert_eq!(ByteSize::kib(4).to_string(), "4 KiB");
833 assert_eq!(ByteSize::mib(8).to_string(), "8 MiB");
834 assert_eq!(ByteSize::new(1_610_612_736).to_string(), "1.500 GiB");
835
836 assert_eq!(
838 ByteSize::gib(1).display_as(ByteUnit::Mebibyte).to_string(),
839 "1024 MiB"
840 );
841 assert_eq!(ByteSize::mib(1).as_unit(ByteUnit::Kibibyte), 1024.0);
842 }
843
844 #[test]
845 #[should_panic(expected = "byte count exceeds u64::MAX")]
846 fn byte_size_overflow_panics() {
847 let _ = ByteSize::gib(u64::MAX);
848 }
849
850 #[test]
851 fn test_throughput_display() {
852 assert_eq!(
854 "1 Mb/s",
855 format!("{}", Throughput::new(125_000, Duration::from_secs(1)))
856 );
857
858 let t = Throughput::new(1_000_000, Duration::from_secs(1));
859
860 assert_eq!("1000000 B/s", format!("{}", t.display_as(ByteUnit::Byte)));
862 assert_eq!("8000 Kb/s", format!("{}", t.display_as(ByteUnit::Kilobit)));
863 assert_eq!(
864 "976.5625 KiB/s",
865 format!("{}", t.display_as(ByteUnit::Kibibyte))
866 );
867 assert_eq!("8 Mb/s", format!("{}", t.display_as(ByteUnit::Megabit)));
868 assert_eq!(
869 "0.954 MiB/s",
870 format!("{:.3}", t.display_as(ByteUnit::Mebibyte))
871 );
872 assert_eq!("0.008 Gb/s", format!("{}", t.display_as(ByteUnit::Gigabit)));
873 assert_eq!(
874 "0.00093 GiB/s",
875 format!("{:.5}", t.display_as(ByteUnit::Gibibyte))
876 );
877 }
878
879 #[test]
880 fn test_from_str() {
881 let units = &[
882 ByteUnit::Byte,
883 ByteUnit::Kilobit,
884 ByteUnit::Kibibyte,
885 ByteUnit::Megabit,
886 ByteUnit::Mebibyte,
887 ByteUnit::Gigabit,
888 ByteUnit::Gibibyte,
889 ];
890
891 for u in units {
892 let u2 = ByteUnit::from_str(u.as_str()).unwrap();
893 assert_eq!(*u, u2);
894 }
895
896 assert!(ByteUnit::from_str("kb").is_err());
897 }
898
899 #[test]
900 fn test_ops() {
901 let t = Throughput::new_bytes_per_sec(ByteSize::mib(10).as_u64());
902 let t2 = Throughput::new_bytes_per_sec(ByteSize::mib(5).as_u64());
903
904 assert_eq!(
905 t + t2,
906 Throughput::new_bytes_per_sec(t.bytes_transferred() + t2.bytes_transferred())
907 );
908 assert_eq!(t - t2, t2);
909 assert_eq!(
910 t * 2,
911 Throughput::new_bytes_per_sec(t.bytes_transferred() * 2)
912 );
913 assert_eq!(t / 2, t2);
914 }
915
916 #[test]
917 fn test_byte_display_context() {
918 assert_eq!("1 KiB", format!("{}", ByteSize::new(1024)));
919 assert_eq!("1 MiB", format!("{}", ByteSize::new(1024 * 1024)));
920 assert_eq!("1 GiB", format!("{}", ByteSize::new(1024 * 1024 * 1024)));
921
922 assert_eq!("727 B", format!("{}", ByteSize::new(727)));
923 assert_eq!(
924 "0.710 KiB",
925 format!("{}", ByteSize::new(727).display_as(ByteUnit::Kibibyte))
926 );
927 assert_eq!("3.420 KiB", format!("{}", ByteSize::new(3502)));
928 assert_eq!("3.41992 KiB", format!("{:.5}", ByteSize::new(3502)));
929
930 assert_eq!("7.201 MiB", format!("{}", ByteSize::new(7550498)));
931 assert_eq!(
932 "0.007 GiB",
933 format!("{}", ByteSize::new(7550498).display_as(ByteUnit::Gibibyte))
934 );
935
936 assert_eq!("1.016 GiB", format!("{}", ByteSize::new(1091242563)));
937 assert_eq!(
938 "1040.690 MiB",
939 format!(
940 "{}",
941 ByteSize::new(1091242563).display_as(ByteUnit::Mebibyte)
942 )
943 );
944 }
945}