Skip to content
Merged
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
43 changes: 43 additions & 0 deletions crates/composefs-oci/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub mod oci_image;
pub mod skopeo;
pub mod tar;

// Re-export the composefs crate for consumers who only need composefs-oci
pub use composefs;

use std::{collections::HashMap, sync::Arc};

use anyhow::{bail, ensure, Context, Result};
Expand Down Expand Up @@ -55,6 +58,11 @@ pub struct ImportStats {
}

impl ImportStats {
/// Total number of objects processed (new + already present).
pub fn total_objects(&self) -> u64 {
self.objects_copied + self.objects_already_present
}

/// Merge another `ImportStats` into this one.
pub fn merge(&mut self, other: &ImportStats) {
self.objects_copied += other.objects_copied;
Expand Down Expand Up @@ -84,6 +92,19 @@ impl ImportStats {
}
}

impl std::fmt::Display for ImportStats {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} new + {} already present objects; {} stored, {} inlined",
self.objects_copied,
self.objects_already_present,
indicatif::HumanBytes(self.bytes_copied),
indicatif::HumanBytes(self.bytes_inlined),
)
}
}

/// Result of a pull operation.
#[derive(Debug)]
pub struct PullResult<ObjectID> {
Expand Down Expand Up @@ -469,4 +490,26 @@ mod test {
let result = open_config::<Sha256HashValue>(&repo, &config_digest, None);
assert!(result.is_ok());
}

#[test]
fn test_import_stats_display() {
let stats = ImportStats {
objects_copied: 42,
objects_already_present: 100,
bytes_copied: 1_500_000,
bytes_inlined: 800,
};
assert_eq!(
stats.to_string(),
"42 new + 100 already present objects; 1.43 MiB stored, 800 B inlined"
);

let empty = ImportStats::default();
assert_eq!(
empty.to_string(),
"0 new + 0 already present objects; 0 B stored, 0 B inlined"
);
assert_eq!(empty.total_objects(), 0);
assert_eq!(stats.total_objects(), 142);
}
}
Loading