smbcloud_model/
project.rs

1use {
2    crate::{app_auth::AuthApp, ar_date_format, runner::Runner},
3    chrono::{DateTime, Utc},
4    serde::{Deserialize, Serialize},
5    serde_repr::{Deserialize_repr, Serialize_repr},
6    std::fmt::Display,
7    tsync::tsync,
8};
9
10#[derive(Deserialize, Debug, Serialize)]
11pub struct Config {
12    pub current_project: Option<Project>,
13    pub current_auth_app: Option<AuthApp>,
14}
15
16#[derive(Deserialize, Serialize, Debug, Clone)]
17#[tsync]
18pub struct Project {
19    pub id: i32,
20    pub name: String,
21    pub runner: Runner,
22    pub repository: Option<String>,
23    pub description: Option<String>,
24    pub created_at: DateTime<Utc>,
25    pub updated_at: DateTime<Utc>,
26}
27
28impl Display for Project {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        write!(f, "ID: {}, Name: {}", self.id, self.name,)
31    }
32}
33#[derive(Serialize, Debug, Deserialize, Clone)]
34#[tsync]
35pub struct ProjectCreate {
36    pub name: String,
37    pub repository: String,
38    pub description: String,
39    pub runner: Runner,
40}
41
42#[derive(Deserialize, Serialize, Debug)]
43#[tsync]
44pub struct Deployment {
45    pub id: i32,
46    pub project_id: i32,
47    pub commit_hash: String,
48    pub status: DeploymentStatus,
49    #[serde(with = "ar_date_format")]
50    pub created_at: DateTime<Utc>,
51    #[serde(with = "ar_date_format")]
52    pub updated_at: DateTime<Utc>,
53}
54
55#[derive(Deserialize, Serialize, Debug)]
56pub struct DeploymentPayload {
57    pub commit_hash: String,
58    pub status: DeploymentStatus,
59}
60
61#[derive(Deserialize_repr, Serialize_repr, Debug, Clone, Copy)] // Added Clone, Copy
62#[repr(u8)]
63#[tsync]
64pub enum DeploymentStatus {
65    Started = 0,
66    Failed,
67    Done,
68}
69
70impl Display for DeploymentStatus {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        match self {
73            DeploymentStatus::Started => write!(f, "🚀"),
74            DeploymentStatus::Failed => write!(f, "❌"),
75            DeploymentStatus::Done => write!(f, "✅"),
76        }
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83    use serde_json::json;
84    #[test]
85    fn test_project_create() {
86        let project_create = ProjectCreate {
87            name: "test".to_owned(),
88            repository: "test".to_owned(),
89            description: "test".to_owned(),
90            runner: Runner::NodeJs,
91        };
92        let json = json!({
93            "name": "test",
94            "repository": "test", // Corrected: repository should be included as per struct
95            "description": "test",
96            "runner": 0
97        });
98        assert_eq!(serde_json::to_value(project_create).unwrap(), json);
99    }
100
101    #[test]
102    fn test_deployment_status_display() {
103        assert_eq!(format!("{}", DeploymentStatus::Started), "🚀");
104        assert_eq!(DeploymentStatus::Started.to_string(), "🚀");
105
106        assert_eq!(format!("{}", DeploymentStatus::Failed), "❌");
107        assert_eq!(DeploymentStatus::Failed.to_string(), "❌");
108
109        assert_eq!(format!("{}", DeploymentStatus::Done), "✅");
110        assert_eq!(DeploymentStatus::Done.to_string(), "✅");
111    }
112}