Skip to main content

kellnr_db/
toolchain_target_status.rs

1use serde::{Deserialize, Serialize};
2use tracing::warn;
3
4/// Processing status of an uploaded toolchain target, stored as its
5/// lowercase name.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
7#[serde(rename_all = "lowercase")]
8pub enum ToolchainTargetStatus {
9    /// The archive is being split into its components.
10    Processing,
11    /// The target can be installed.
12    Ready,
13    /// Splitting the archive failed or was interrupted.
14    Failed,
15}
16
17impl ToolchainTargetStatus {
18    pub fn as_str(self) -> &'static str {
19        match self {
20            Self::Processing => "processing",
21            Self::Ready => "ready",
22            Self::Failed => "failed",
23        }
24    }
25
26    /// Reads a status stored in the database. Only the names of the variants
27    /// are ever stored; anything else is treated as failed, so the target is
28    /// never offered for installation.
29    pub(crate) fn from_db(status: &str) -> Self {
30        match status {
31            "processing" => Self::Processing,
32            "ready" => Self::Ready,
33            "failed" => Self::Failed,
34            other => {
35                warn!("Unknown toolchain target status {other:?}, treating it as failed");
36                Self::Failed
37            }
38        }
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    const ALL: [ToolchainTargetStatus; 3] = [
47        ToolchainTargetStatus::Processing,
48        ToolchainTargetStatus::Ready,
49        ToolchainTargetStatus::Failed,
50    ];
51
52    #[test]
53    fn db_string_round_trip() {
54        for status in ALL {
55            assert_eq!(ToolchainTargetStatus::from_db(status.as_str()), status);
56        }
57        assert_eq!(ToolchainTargetStatus::Processing.as_str(), "processing");
58        assert_eq!(ToolchainTargetStatus::Ready.as_str(), "ready");
59        assert_eq!(ToolchainTargetStatus::Failed.as_str(), "failed");
60    }
61
62    #[test]
63    fn json_is_the_db_string() {
64        for status in ALL {
65            assert_eq!(
66                serde_json::to_value(status).unwrap(),
67                serde_json::Value::String(status.as_str().to_string())
68            );
69        }
70    }
71
72    #[test]
73    fn unknown_db_string_is_failed() {
74        assert_eq!(
75            ToolchainTargetStatus::from_db("Ready"),
76            ToolchainTargetStatus::Failed
77        );
78    }
79}