Skip to main content

kernel/install/
event.rs

1//! The progress and event types an install emits.
2
3/// Download progress for an install: bytes so far, the total (when known), and
4/// which file is in flight.
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
6pub struct InstallProgress {
7    /// Bytes downloaded so far.
8    pub bytes_downloaded: i64,
9    /// The total to download, if known.
10    pub total_bytes: Option<i64>,
11    /// Whether `total_bytes` is a partial/growing estimate (so no fraction).
12    pub total_is_partial: bool,
13    /// The file currently downloading, if any.
14    pub current_file: Option<String>,
15}
16
17impl InstallProgress {
18    /// The completed fraction in `[0, 1]`, or `None` when the total is unknown,
19    /// zero, or only a partial estimate.
20    pub fn fraction(&self) -> Option<f64> {
21        let total = self.total_bytes?;
22        if total <= 0 || self.total_is_partial {
23            return None;
24        }
25        Some((self.bytes_downloaded as f64 / total as f64).clamp(0.0, 1.0))
26    }
27}
28
29/// A lifecycle event for an install job.
30#[derive(Debug, Clone, PartialEq, Eq, Hash)]
31pub enum InstallEvent {
32    /// Waiting to start.
33    Queued,
34    /// Resolving the plan before transfer.
35    Preparing,
36    /// A human-readable status line.
37    Status(String),
38    /// Download progress.
39    Progress(InstallProgress),
40    /// Completed successfully.
41    Done,
42    /// Failed, with a message.
43    Failed {
44        /// Why it failed.
45        message: String,
46    },
47    /// Cancelled by the user.
48    Cancelled,
49}
50
51impl InstallEvent {
52    /// Whether this event ends the job (done/failed/cancelled).
53    pub fn is_terminal(&self) -> bool {
54        matches!(
55            self,
56            InstallEvent::Done | InstallEvent::Failed { .. } | InstallEvent::Cancelled
57        )
58    }
59}
60
61/// What a provider's transfer emits while running (before the terminal event is
62/// synthesized by the install service).
63#[derive(Debug, Clone, PartialEq, Eq, Hash)]
64pub enum InstallStreamEvent {
65    /// A status line.
66    Status(String),
67    /// Download progress.
68    Progress(InstallProgress),
69}