Skip to main content

mediatek_optical/
operation.rs

1//! Presentation-independent lifecycle observations shared by optical hosts.
2
3/// A host-facing error category; detailed causes remain in the typed library error.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum IssueKind {
6    /// The selected bytes cannot form a valid firmware update.
7    InvalidFirmware,
8    /// This firmware's update format is not implemented.
9    UnsupportedFirmware,
10    /// Installed evidence no longer matches the connected receiver.
11    ReceiverMismatch,
12    /// The selected firmware targets different hardware.
13    HardwareMismatch,
14    /// A required command or device read could not complete.
15    Connection,
16    /// Readback or target startup was not verified.
17    Verification,
18    /// An update command failed; transport evidence determines consequences.
19    Update,
20    /// The host needs to retain diagnostics for a new or unexpected condition.
21    Unexpected,
22}
23
24/// A protocol-neutral operation phase.
25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub enum Phase {
27    /// Acquiring bytes from the installed firmware.
28    Reading,
29    /// Validating and assembling an executable update.
30    Preparing,
31    /// Sending firmware to the drive.
32    Writing,
33    /// Waiting for programming and restart to finish.
34    Restarting,
35    /// Comparing bytes read from the drive with the prepared target.
36    Verifying,
37}
38
39/// A region label; hosts may translate these without knowing the protocol.
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub enum Component {
42    /// A complete firmware image.
43    Firmware,
44    /// The boot and update component.
45    Kernel,
46    /// The application component.
47    Normal,
48}
49
50/// Observed work in one phase; absent counts mean indeterminate activity.
51#[derive(Clone, Copy, Debug, PartialEq, Eq)]
52pub struct Progress {
53    /// What the library is doing.
54    pub phase: Phase,
55    /// Region whose bytes are being processed.
56    pub component: Component,
57    /// Completed bytes, never inferred from elapsed time.
58    pub done: Option<usize>,
59    /// Total bytes, when measurable.
60    pub total: Option<usize>,
61}
62
63impl Progress {
64    /// Measured progress in a region.
65    pub const fn bytes(phase: Phase, component: Component, done: usize, total: usize) -> Self {
66        Self {
67            phase,
68            component,
69            done: Some(done),
70            total: Some(total),
71        }
72    }
73
74    /// A phase that cannot honestly report a percentage.
75    pub const fn waiting(phase: Phase, component: Component) -> Self {
76        Self {
77            phase,
78            component,
79            done: None,
80            total: None,
81        }
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn waiting_does_not_invent_a_percentage() {
91        let waiting = Progress::waiting(Phase::Restarting, Component::Firmware);
92        assert_eq!((waiting.done, waiting.total), (None, None));
93        let measured = Progress::bytes(Phase::Verifying, Component::Firmware, 512, 1024);
94        assert_eq!((measured.done, measured.total), (Some(512), Some(1024)));
95    }
96}