use std::fmt::{self, Display};
use std::ops;
use std::str::FromStr;
use std::time::Duration;
pub mod checksum;
mod checksum_strategy;
pub use checksum_strategy::ChecksumStrategy;
pub use crate::model::builders;
pub use crate::model::{
ArchiveStatus, ChecksumAlgorithm, ChecksumMode, ChecksumType, ChunkMetadata, DownloadInput,
Object, ObjectCannedAcl, ObjectLockLegalHoldStatus, ObjectLockMode, ObjectMetadata,
ObjectStorageClass, Owner, ReplicationStatus, RequestCharged, RequestPayer, RestoreStatus,
ServerSideEncryption, StorageClass, UploadInput, UploadOutput,
};
#[non_exhaustive]
#[derive(Debug, Clone, Default)]
pub enum FailedMultipartUploadPolicy {
#[default]
AbortUpload,
Retain,
}
#[derive(Debug, Default)]
pub struct AbortedUpload {
pub(crate) upload_id: Option<String>,
pub(crate) request_charged: Option<crate::model::RequestCharged>,
pub(crate) outcome: AbortOutcome,
pub(crate) output: Option<crate::operation::upload::UploadOutput>,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum AbortOutcome {
#[default]
Cancelled,
AlreadyCompleted,
Retained,
}
impl AbortedUpload {
pub fn outcome(&self) -> AbortOutcome {
self.outcome
}
pub fn output(&self) -> Option<&crate::operation::upload::UploadOutput> {
self.output.as_ref()
}
pub fn upload_id(&self) -> Option<&str> {
self.upload_id.as_deref()
}
pub fn request_charged(&self) -> &Option<crate::model::RequestCharged> {
&self.request_charged
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq)]
pub enum FailedTransferPolicy {
#[default]
Abort,
Continue,
}
#[non_exhaustive]
#[derive(Debug)]
pub struct FailedDownload {
pub(crate) input: crate::operation::download::DownloadInput,
pub(crate) error: crate::error::Error,
}
impl FailedDownload {
pub fn input(&self) -> &crate::operation::download::DownloadInput {
&self.input
}
pub fn error(&self) -> &crate::error::Error {
&self.error
}
}
#[non_exhaustive]
#[derive(Debug)]
pub struct FailedUpload {
pub(crate) input: Option<crate::operation::upload::UploadInput>,
pub(crate) error: crate::error::Error,
pub(crate) source_path: Option<std::path::PathBuf>,
}
impl FailedUpload {
pub fn input(&self) -> Option<&crate::operation::upload::UploadInput> {
self.input.as_ref()
}
pub fn error(&self) -> &crate::error::Error {
&self.error
}
pub fn source_path(&self) -> Option<&std::path::Path> {
self.source_path.as_deref()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TransferStatus {
Active,
Completed,
Failed,
Cancelled,
}
impl TransferStatus {
pub fn is_terminal(&self) -> bool {
!matches!(self, Self::Active)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub(crate) enum BucketType {
Standard,
Express,
}
impl BucketType {
pub(crate) fn from_bucket_name(bucket_name: &str) -> Self {
if bucket_name.ends_with("--x-s3") {
BucketType::Express
} else {
BucketType::Standard
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct Bucket {
name: String,
kind: BucketType,
}
impl Bucket {
pub(crate) fn new(name: String) -> Self {
let kind = BucketType::from_bucket_name(&name);
Self { name, kind }
}
pub(crate) fn name(&self) -> &str {
&self.name
}
pub(crate) fn kind(&self) -> BucketType {
self.kind
}
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct IntegrityChecks {
checksum_crc32: Option<String>,
checksum_crc32c: Option<String>,
checksum_crc64_nvme: Option<String>,
checksum_sha1: Option<String>,
checksum_sha256: Option<String>,
checksum_sha512: Option<String>,
checksum_md5: Option<String>,
checksum_xxhash64: Option<String>,
checksum_xxhash3: Option<String>,
checksum_xxhash128: Option<String>,
checksum_type: Option<crate::model::ChecksumType>,
checksum_validation: ChecksumValidation,
}
impl IntegrityChecks {
pub fn checksum_validation(&self) -> &ChecksumValidation {
&self.checksum_validation
}
pub fn checksum_crc32(&self) -> Option<&str> {
self.checksum_crc32.as_deref()
}
pub fn checksum_crc32c(&self) -> Option<&str> {
self.checksum_crc32c.as_deref()
}
pub fn checksum_crc64_nvme(&self) -> Option<&str> {
self.checksum_crc64_nvme.as_deref()
}
pub fn checksum_sha1(&self) -> Option<&str> {
self.checksum_sha1.as_deref()
}
pub fn checksum_sha256(&self) -> Option<&str> {
self.checksum_sha256.as_deref()
}
pub fn checksum_sha512(&self) -> Option<&str> {
self.checksum_sha512.as_deref()
}
pub fn checksum_md5(&self) -> Option<&str> {
self.checksum_md5.as_deref()
}
pub fn checksum_xxhash64(&self) -> Option<&str> {
self.checksum_xxhash64.as_deref()
}
pub fn checksum_xxhash3(&self) -> Option<&str> {
self.checksum_xxhash3.as_deref()
}
pub fn checksum_xxhash128(&self) -> Option<&str> {
self.checksum_xxhash128.as_deref()
}
pub fn checksum_type(&self) -> Option<&crate::model::ChecksumType> {
self.checksum_type.as_ref()
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChecksumValidation {
#[non_exhaustive]
Validated {
algorithm: crate::model::ChecksumAlgorithm,
},
#[non_exhaustive]
NotValidated {
reason: NotValidatedReason,
},
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NotValidatedReason {
Disabled,
CompositeChecksum,
PartialCoverage,
Unavailable,
UnsupportedAlgorithm,
}
impl IntegrityChecks {
pub(crate) fn new(
values: Option<&crate::model::ObjectMetadata>,
checksum_type: Option<crate::model::ChecksumType>,
checksum_validation: ChecksumValidation,
) -> Self {
let value = |f: fn(&crate::model::ObjectMetadata) -> &Option<String>| {
values.and_then(|m| f(m).clone())
};
Self {
checksum_crc32: value(|m| &m.checksum_crc32),
checksum_crc32c: value(|m| &m.checksum_crc32_c),
checksum_crc64_nvme: value(|m| &m.checksum_crc64_nvme),
checksum_sha1: value(|m| &m.checksum_sha1),
checksum_sha256: value(|m| &m.checksum_sha256),
checksum_sha512: value(|m| &m.checksum_sha512),
checksum_md5: value(|m| &m.checksum_md5),
checksum_xxhash64: value(|m| &m.checksum_xxhash64),
checksum_xxhash3: value(|m| &m.checksum_xxhash3),
checksum_xxhash128: value(|m| &m.checksum_xxhash128),
checksum_type,
checksum_validation,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ByteSize(u64);
impl ByteSize {
pub const fn new(bytes: u64) -> Self {
Self(bytes)
}
pub const fn kib(n: u64) -> Self {
Self(mul(n, ByteUnit::Kibibyte.as_bytes_u64()))
}
pub const fn mib(n: u64) -> Self {
Self(mul(n, ByteUnit::Mebibyte.as_bytes_u64()))
}
pub const fn gib(n: u64) -> Self {
Self(mul(n, ByteUnit::Gibibyte.as_bytes_u64()))
}
pub const fn as_u64(self) -> u64 {
self.0
}
pub const fn as_usize(self) -> usize {
assert!(self.0 <= usize::MAX as u64, "byte count exceeds usize::MAX");
self.0 as usize
}
pub fn as_unit(self, unit: ByteUnit) -> f64 {
unit.convert(self.0)
}
pub fn display_as(self, unit: ByteUnit) -> ByteSizeDisplayContext {
ByteSizeDisplayContext {
total_bytes: self.0,
unit,
}
}
}
const fn mul(n: u64, unit_bytes: u64) -> u64 {
match n.checked_mul(unit_bytes) {
Some(bytes) => bytes,
None => panic!("byte count exceeds u64::MAX"),
}
}
impl fmt::Display for ByteSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let units = &[ByteUnit::Gibibyte, ByteUnit::Mebibyte, ByteUnit::Kibibyte];
let mut unit = ByteUnit::Byte;
for u in units {
if self.0 >= u.as_bytes_u64() {
unit = *u;
break;
}
}
fmt::Display::fmt(&self.display_as(unit), f)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ByteUnit {
Byte,
Kilobit,
Kibibyte,
Megabit,
Mebibyte,
Gigabit,
Gibibyte,
}
impl ByteUnit {
pub(crate) fn convert(&self, bytes: u64) -> f64 {
bytes as f64 / self.as_bytes_u64() as f64
}
pub(crate) const fn as_bytes_u64(&self) -> u64 {
match self {
ByteUnit::Byte => 1,
ByteUnit::Kilobit => 125,
ByteUnit::Kibibyte => 1 << 10,
ByteUnit::Megabit => 125_000,
ByteUnit::Mebibyte => 1 << 20,
ByteUnit::Gigabit => 125_000_000,
ByteUnit::Gibibyte => 1 << 30,
}
}
pub(crate) const fn as_str(&self) -> &'static str {
match self {
ByteUnit::Byte => "B",
ByteUnit::Kilobit => "Kb",
ByteUnit::Kibibyte => "KiB",
ByteUnit::Megabit => "Mb",
ByteUnit::Mebibyte => "MiB",
ByteUnit::Gigabit => "Gb",
ByteUnit::Gibibyte => "GiB",
}
}
}
impl AsRef<str> for ByteUnit {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl FromStr for ByteUnit {
type Err = crate::error::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let unit = match s {
"B" => ByteUnit::Byte,
"Kb" => ByteUnit::Kilobit,
"KiB" => ByteUnit::Kibibyte,
"Mb" => ByteUnit::Megabit,
"MiB" => ByteUnit::Mebibyte,
"Gb" => ByteUnit::Gigabit,
"GiB" => ByteUnit::Gibibyte,
_ => {
return Err(crate::error::invalid_input(format!(
"unknown byte unit '{}'",
s
)))
}
};
Ok(unit)
}
}
#[derive(Debug)]
pub struct ByteSizeDisplayContext {
total_bytes: u64,
unit: ByteUnit,
}
impl fmt::Display for ByteSizeDisplayContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.total_bytes.is_multiple_of(self.unit.as_bytes_u64()) {
let converted = self.total_bytes / self.unit.as_bytes_u64();
return write!(f, "{converted} {}", self.unit.as_str());
}
let precision = f.precision().unwrap_or(3);
write!(
f,
"{1:.*} {2:}",
precision,
self.unit.convert(self.total_bytes),
self.unit.as_str()
)
}
}
#[derive(Debug, Clone, Copy)]
pub struct Throughput {
bytes_transferred: u64,
elapsed: Duration,
}
impl Throughput {
pub const fn new(bytes_transferred: u64, elapsed: Duration) -> Throughput {
Throughput {
bytes_transferred,
elapsed,
}
}
pub const fn new_bytes_per_sec(bytes_transferred: u64) -> Throughput {
Self::new(bytes_transferred, Duration::from_secs(1))
}
pub fn as_unit_per_sec(&self, unit: ByteUnit) -> f64 {
(self.bytes_transferred as f64 / unit.as_bytes_u64() as f64) / self.elapsed.as_secs_f64()
}
pub fn as_bytes_per_sec(&self) -> f64 {
self.as_unit_per_sec(ByteUnit::Byte)
}
pub const fn bytes_transferred(&self) -> u64 {
self.bytes_transferred
}
pub fn display_as(&self, unit: ByteUnit) -> ThroughputDisplayContext<'_> {
ThroughputDisplayContext {
throughput: self,
unit,
}
}
}
impl PartialEq for Throughput {
fn eq(&self, other: &Self) -> bool {
self.as_bytes_per_sec() == other.as_bytes_per_sec()
}
}
impl PartialOrd for Throughput {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.as_bytes_per_sec()
.partial_cmp(&other.as_bytes_per_sec())
}
}
impl ops::Add for Throughput {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let bps = self.as_bytes_per_sec() + rhs.as_bytes_per_sec();
Throughput::new_bytes_per_sec(bps.round() as u64)
}
}
impl ops::Sub for Throughput {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
let bps = self.as_bytes_per_sec() - rhs.as_bytes_per_sec();
Throughput::new_bytes_per_sec(bps.round() as u64)
}
}
impl ops::Mul<u64> for Throughput {
type Output = Self;
fn mul(self, rhs: u64) -> Self::Output {
Throughput::new(self.bytes_transferred * rhs, self.elapsed)
}
}
impl ops::Div<u64> for Throughput {
type Output = Self;
fn div(self, rhs: u64) -> Self::Output {
Throughput::new(self.bytes_transferred / rhs, self.elapsed)
}
}
impl fmt::Display for Throughput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let context = ThroughputDisplayContext {
throughput: self,
unit: ByteUnit::Megabit,
};
Display::fmt(&context, f)
}
}
#[derive(Debug)]
pub struct ThroughputDisplayContext<'a> {
throughput: &'a Throughput,
unit: ByteUnit,
}
impl fmt::Display for ThroughputDisplayContext<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(precision) = f.precision() {
write!(
f,
"{1:.*} {2:}/s",
precision,
self.throughput.as_unit_per_sec(self.unit),
self.unit.as_str()
)
} else {
write!(
f,
"{} {}/s",
self.throughput.as_unit_per_sec(self.unit),
self.unit.as_str()
)
}
}
}
#[cfg(test)]
mod tests {
use crate::config::TargetThroughput;
#[test]
fn target_throughput_accepts_finite_positive_gbps() {
for gbps in [0.5, 1.0, 200.0] {
let target = TargetThroughput::gbps(gbps).unwrap();
assert_eq!(target.as_gbps(), gbps);
}
}
#[test]
fn target_throughput_rejects_values_that_are_not_a_rate() {
for gbps in [0.0, -1.0, f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
assert!(TargetThroughput::gbps(gbps).is_err(), "gbps {gbps}");
}
}
}
#[cfg(test)]
mod value_type_tests {
use super::{ByteSize, ByteUnit, Throughput};
use std::str::FromStr;
use std::time::Duration;
#[test]
fn byte_unit_accessors_agree() {
let units = [
(ByteUnit::Byte, 1u64),
(ByteUnit::Kilobit, 125),
(ByteUnit::Kibibyte, 1024),
(ByteUnit::Megabit, 125_000),
(ByteUnit::Mebibyte, 1024 * 1024),
(ByteUnit::Gigabit, 125_000_000),
(ByteUnit::Gibibyte, 1024 * 1024 * 1024),
];
for (unit, bytes) in units {
assert_eq!(unit.as_bytes_u64(), bytes, "{unit:?} bytes");
}
}
#[test]
fn byte_size_constructors_and_display() {
assert_eq!(ByteSize::kib(2).as_u64(), 2 * 1024);
assert_eq!(ByteSize::mib(8).as_u64(), 8 * 1024 * 1024);
assert_eq!(ByteSize::gib(3).as_u64(), 3 * 1024 * 1024 * 1024);
assert_eq!(ByteSize::new(512).to_string(), "512 B");
assert_eq!(ByteSize::kib(4).to_string(), "4 KiB");
assert_eq!(ByteSize::mib(8).to_string(), "8 MiB");
assert_eq!(ByteSize::new(1_610_612_736).to_string(), "1.500 GiB");
assert_eq!(
ByteSize::gib(1).display_as(ByteUnit::Mebibyte).to_string(),
"1024 MiB"
);
assert_eq!(ByteSize::mib(1).as_unit(ByteUnit::Kibibyte), 1024.0);
}
#[test]
#[should_panic(expected = "byte count exceeds u64::MAX")]
fn byte_size_overflow_panics() {
let _ = ByteSize::gib(u64::MAX);
}
#[test]
fn test_throughput_display() {
assert_eq!(
"1 Mb/s",
format!("{}", Throughput::new(125_000, Duration::from_secs(1)))
);
let t = Throughput::new(1_000_000, Duration::from_secs(1));
assert_eq!("1000000 B/s", format!("{}", t.display_as(ByteUnit::Byte)));
assert_eq!("8000 Kb/s", format!("{}", t.display_as(ByteUnit::Kilobit)));
assert_eq!(
"976.5625 KiB/s",
format!("{}", t.display_as(ByteUnit::Kibibyte))
);
assert_eq!("8 Mb/s", format!("{}", t.display_as(ByteUnit::Megabit)));
assert_eq!(
"0.954 MiB/s",
format!("{:.3}", t.display_as(ByteUnit::Mebibyte))
);
assert_eq!("0.008 Gb/s", format!("{}", t.display_as(ByteUnit::Gigabit)));
assert_eq!(
"0.00093 GiB/s",
format!("{:.5}", t.display_as(ByteUnit::Gibibyte))
);
}
#[test]
fn test_from_str() {
let units = &[
ByteUnit::Byte,
ByteUnit::Kilobit,
ByteUnit::Kibibyte,
ByteUnit::Megabit,
ByteUnit::Mebibyte,
ByteUnit::Gigabit,
ByteUnit::Gibibyte,
];
for u in units {
let u2 = ByteUnit::from_str(u.as_str()).unwrap();
assert_eq!(*u, u2);
}
assert!(ByteUnit::from_str("kb").is_err());
}
#[test]
fn test_ops() {
let t = Throughput::new_bytes_per_sec(ByteSize::mib(10).as_u64());
let t2 = Throughput::new_bytes_per_sec(ByteSize::mib(5).as_u64());
assert_eq!(
t + t2,
Throughput::new_bytes_per_sec(t.bytes_transferred() + t2.bytes_transferred())
);
assert_eq!(t - t2, t2);
assert_eq!(
t * 2,
Throughput::new_bytes_per_sec(t.bytes_transferred() * 2)
);
assert_eq!(t / 2, t2);
}
#[test]
fn test_byte_display_context() {
assert_eq!("1 KiB", format!("{}", ByteSize::new(1024)));
assert_eq!("1 MiB", format!("{}", ByteSize::new(1024 * 1024)));
assert_eq!("1 GiB", format!("{}", ByteSize::new(1024 * 1024 * 1024)));
assert_eq!("727 B", format!("{}", ByteSize::new(727)));
assert_eq!(
"0.710 KiB",
format!("{}", ByteSize::new(727).display_as(ByteUnit::Kibibyte))
);
assert_eq!("3.420 KiB", format!("{}", ByteSize::new(3502)));
assert_eq!("3.41992 KiB", format!("{:.5}", ByteSize::new(3502)));
assert_eq!("7.201 MiB", format!("{}", ByteSize::new(7550498)));
assert_eq!(
"0.007 GiB",
format!("{}", ByteSize::new(7550498).display_as(ByteUnit::Gibibyte))
);
assert_eq!("1.016 GiB", format!("{}", ByteSize::new(1091242563)));
assert_eq!(
"1040.690 MiB",
format!(
"{}",
ByteSize::new(1091242563).display_as(ByteUnit::Mebibyte)
)
);
}
}