Skip to main content

corundum_framework_update/
package.rs

1//! The state of a package this app updates itself, shared by the crates that show or drive it.
2
3use serde::{Deserialize, Serialize};
4
5/// Where a self-updating package stands: the running version and what the updater is doing.
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7pub struct PackageStatus {
8    /// The running version, `None` when it is unknown.
9    pub current: Option<semver::Version>,
10    /// What the updater is doing.
11    pub state: PackageState,
12}
13
14/// What a package's updater is doing.
15#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
16pub enum PackageState {
17    /// Nothing asked yet.
18    Idle,
19    /// Looking for a newer version.
20    Checking,
21    /// The running version is the newest.
22    UpToDate,
23    /// Fetching a newer version.
24    Downloading {
25        /// How much is fetched, 0 to 100.
26        percent: u8,
27    },
28    /// A newer version is staged; restarting applies it.
29    Ready {
30        /// The staged version.
31        version: semver::Version,
32    },
33    /// The last check or download failed, with the reason.
34    Failed(String),
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn the_state_round_trips_through_json() {
43        for state in [
44            PackageState::Idle,
45            PackageState::Downloading { percent: 40 },
46            PackageState::Ready {
47                version: semver::Version::new(1, 2, 3),
48            },
49            PackageState::Failed("offline".to_owned()),
50        ] {
51            let status = PackageStatus {
52                current: Some(semver::Version::new(1, 0, 0)),
53                state,
54            };
55            let json = serde_json::to_string(&status).unwrap();
56            assert_eq!(
57                serde_json::from_str::<PackageStatus>(&json).unwrap(),
58                status
59            );
60        }
61    }
62}