winbrew_models\install/
installed.rs1use core::str::FromStr;
2use serde::{Deserialize, Serialize};
3
4use super::engine::{EngineKind, EngineMetadata};
5use super::installer::InstallerType;
6use crate::shared::{DeploymentKind, ModelError};
7
8#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy)]
10#[serde(rename_all = "lowercase")]
11pub enum PackageStatus {
12 Installing,
14 Ok,
16 Updating,
18 Failed,
20}
21
22impl PackageStatus {
23 pub fn as_str(&self) -> &'static str {
25 match self {
26 Self::Installing => "installing",
27 Self::Ok => "ok",
28 Self::Updating => "updating",
29 Self::Failed => "failed",
30 }
31 }
32}
33
34impl FromStr for PackageStatus {
35 type Err = ModelError;
36
37 fn from_str(status: &str) -> Result<Self, Self::Err> {
38 match status.trim().to_ascii_lowercase().as_str() {
39 "installing" => Ok(Self::Installing),
40 "ok" => Ok(Self::Ok),
41 "updating" => Ok(Self::Updating),
42 "failed" => Ok(Self::Failed),
43 other => Err(ModelError::invalid_enum_value("package.status", other)),
44 }
45 }
46}
47
48impl std::fmt::Display for PackageStatus {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 f.write_str(self.as_str())
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::PackageStatus;
57 use core::str::FromStr;
58
59 #[test]
60 fn package_status_rejects_unknown_value() {
61 let err = PackageStatus::from_str("mystery").expect_err("unknown status should fail");
62
63 assert!(err.to_string().contains("invalid package.status: mystery"));
64 }
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct InstalledPackage {
70 pub name: String,
72 pub version: String,
74 pub kind: InstallerType,
76 #[serde(default = "default_deployment_kind")]
78 pub deployment_kind: DeploymentKind,
79 pub engine_kind: EngineKind,
81 pub engine_metadata: Option<EngineMetadata>,
83 pub install_dir: String,
85 pub dependencies: Vec<String>,
87 pub status: PackageStatus,
89 pub installed_at: String,
91}
92
93fn default_deployment_kind() -> DeploymentKind {
94 DeploymentKind::Installed
95}