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