diff --git a/crates/ecr/src/lib.rs b/crates/ecr/src/lib.rs index 0292d66..50dfc2d 100644 --- a/crates/ecr/src/lib.rs +++ b/crates/ecr/src/lib.rs @@ -20,6 +20,7 @@ pub mod extract; pub mod kernel; pub mod mount; pub mod namespace; +pub mod progress; pub mod qemu; pub mod qemu_vm; pub mod rootfs; @@ -38,4 +39,5 @@ macro_rules! veprintln { pub use exec::{exec, ExecOptions}; pub use mount::BindTarget; +pub use progress::{NoProgress, Progress, Unit}; pub use rootfs::{PrepareRequest, PreparedRootfs, RootfsCache}; diff --git a/crates/ecr/src/progress.rs b/crates/ecr/src/progress.rs new file mode 100644 index 0000000..58b3496 --- /dev/null +++ b/crates/ecr/src/progress.rs @@ -0,0 +1,42 @@ +//! Progress reporting for long-running operations. +//! +//! The library never touches the terminal: downloads, extraction and +//! initramfs creation report their progress through the [`Progress`] +//! sink supplied by the caller. Consumers choose what to render — +//! progress bars, spinners, structured events — or pass [`NoProgress`] +//! for silence. + +/// What a progress operation counts. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Unit { + /// Bytes transferred (downloads). + Bytes, + /// Files processed (extraction, archive creation). + Files, +} + +/// Sink for progress of long-running library operations. +/// +/// Operations are sequential: `begin` starts the next operation (any +/// previous one is implicitly over), `advance` counts completed units, +/// `finish` marks its end. Sinks must be callable from any thread. +pub trait Progress: Send + Sync { + /// An operation labelled `label` starts, counting `unit`s; `total` + /// is the unit count when known upfront. + fn begin(&self, label: &str, unit: Unit, total: Option); + + /// `n` more units of the current operation completed. + fn advance(&self, n: u64); + + /// The current operation ended. + fn finish(&self); +} + +/// A [`Progress`] sink that discards every event. +pub struct NoProgress; + +impl Progress for NoProgress { + fn begin(&self, _: &str, _: Unit, _: Option) {} + fn advance(&self, _: u64) {} + fn finish(&self) {} +}