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_repr, Serialize_repr, Debug, Clone, Copy, PartialEq, Eq, Default)]
17#[repr(u8)]
18#[tsync]
19pub enum DeploymentMethod {
20 #[default]
21 Git = 0,
22 Rsync = 1,
23}
24
25impl Display for DeploymentMethod {
26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 match self {
28 DeploymentMethod::Git => write!(f, "Git"),
29 DeploymentMethod::Rsync => write!(f, "Rsync"),
30 }
31 }
32}
33
34#[derive(Deserialize, Debug, Serialize, Clone)]
35pub struct Config {
36 pub current_project: Option<Project>,
38 #[serde(default)]
40 pub current_frontend_app: Option<crate::frontend_app::FrontendApp>,
41 pub current_auth_app: Option<AuthApp>,
42}
43
44#[derive(Deserialize, Serialize, Debug, Clone)]
45#[tsync]
46pub struct Project {
47 pub id: i32,
48 pub name: String,
49 pub runner: Runner,
50 #[serde(default)]
52 pub deployment_method: DeploymentMethod,
53 pub path: Option<String>,
54 pub repository: Option<String>,
55 pub description: Option<String>,
56 pub created_at: DateTime<Utc>,
57 pub updated_at: DateTime<Utc>,
58 pub kind: Option<String>,
60 pub source: Option<String>,
64 pub output: Option<String>,
66 pub package_manager: Option<String>,
68 pub pm2_app: Option<String>,
71 #[serde(default)]
73 pub port: Option<u16>,
74 pub shared_lib: Option<String>,
78 pub compile_cmd: Option<String>,
81}
82
83impl Display for Project {
84 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85 write!(f, "ID: {}, Name: {}", self.id, self.name,)
86 }
87}
88#[derive(Serialize, Debug, Deserialize, Clone)]
89#[tsync]
90pub struct ProjectCreate {
91 pub name: String,
92 pub repository: String,
93 pub description: String,
94 pub runner: Runner,
95 #[serde(default)]
96 pub deployment_method: DeploymentMethod,
97}
98
99#[derive(Deserialize, Serialize, Debug)]
100#[tsync]
101pub struct Deployment {
102 pub id: i32,
103 pub project_id: i32,
104 pub commit_hash: String,
105 pub status: DeploymentStatus,
106 #[serde(with = "ar_date_format")]
107 pub created_at: DateTime<Utc>,
108 #[serde(with = "ar_date_format")]
109 pub updated_at: DateTime<Utc>,
110}
111
112#[derive(Deserialize, Serialize, Debug)]
113pub struct DeploymentPayload {
114 pub commit_hash: String,
115 pub status: DeploymentStatus,
116}
117
118#[derive(Deserialize_repr, Serialize_repr, Debug, Clone, Copy)] #[repr(u8)]
120#[tsync]
121pub enum DeploymentStatus {
122 Started = 0,
123 Failed,
124 Done,
125}
126
127impl Display for DeploymentStatus {
128 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
129 match self {
130 DeploymentStatus::Started => write!(f, "🚀"),
131 DeploymentStatus::Failed => write!(f, "❌"),
132 DeploymentStatus::Done => write!(f, "✅"),
133 }
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use super::*;
140 use serde_json::json;
141 #[test]
142 fn test_project_create() {
143 let project_create = ProjectCreate {
144 name: "test".to_owned(),
145 repository: "test".to_owned(),
146 description: "test".to_owned(),
147 runner: Runner::NodeJs,
148 deployment_method: DeploymentMethod::Git,
149 };
150 let json = json!({
151 "name": "test",
152 "repository": "test",
153 "description": "test",
154 "runner": 0,
155 "deployment_method": 0
156 });
157 assert_eq!(serde_json::to_value(project_create).unwrap(), json);
158 }
159
160 #[test]
161 fn test_deployment_status_display() {
162 assert_eq!(format!("{}", DeploymentStatus::Started), "🚀");
163 assert_eq!(DeploymentStatus::Started.to_string(), "🚀");
164
165 assert_eq!(format!("{}", DeploymentStatus::Failed), "❌");
166 assert_eq!(DeploymentStatus::Failed.to_string(), "❌");
167
168 assert_eq!(format!("{}", DeploymentStatus::Done), "✅");
169 assert_eq!(DeploymentStatus::Done.to_string(), "✅");
170 }
171}