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
10fn default_datetime() -> DateTime<Utc> {
11 DateTime::UNIX_EPOCH
12}
13
14#[derive(Deserialize_repr, Serialize_repr, Debug, Clone, Copy, PartialEq, Eq, Default)]
21#[repr(u8)]
22#[tsync]
23pub enum DeploymentMethod {
24 #[default]
25 Git = 0,
26 Rsync = 1,
27}
28
29impl Display for DeploymentMethod {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 match self {
32 DeploymentMethod::Git => write!(f, "Git"),
33 DeploymentMethod::Rsync => write!(f, "Rsync"),
34 }
35 }
36}
37
38#[derive(Deserialize, Debug, Serialize, Clone)]
39pub struct Config {
40 pub current_project: Option<Project>,
42 #[serde(default)]
44 pub current_frontend_app: Option<crate::frontend_app::FrontendApp>,
45 pub current_auth_app: Option<AuthApp>,
46}
47
48#[derive(Deserialize, Serialize, Debug, Clone)]
49#[tsync]
50pub struct Project {
51 pub id: i32,
53 #[serde(default)]
54 pub name: String,
55 #[serde(default)]
56 pub runner: Runner,
57 #[serde(default)]
59 pub deployment_method: DeploymentMethod,
60 pub path: Option<String>,
61 pub repository: Option<String>,
62 pub description: Option<String>,
63 pub frontend_app_id: Option<String>,
66 pub deploy_repo_id: Option<i64>,
69 pub source_path: Option<String>,
71 #[serde(default = "default_datetime")]
72 pub created_at: DateTime<Utc>,
73 #[serde(default = "default_datetime")]
74 pub updated_at: DateTime<Utc>,
75 pub kind: Option<String>,
77 pub source: Option<String>,
82 pub output: Option<String>,
84 pub package_manager: Option<String>,
86 pub pm2_app: Option<String>,
89 #[serde(default, skip_serializing_if = "Option::is_none")]
92 pub pm2_env: Option<std::collections::HashMap<String, serde_json::Value>>,
93 #[serde(default)]
95 pub port: Option<u16>,
96 pub shared_lib: Option<String>,
100 pub compile_cmd: Option<String>,
103 #[serde(default)]
105 pub install_command: Option<String>,
106 pub binary_name: Option<String>,
109 pub rust_target: Option<String>,
112 pub swift_sdk: Option<String>,
117 pub swift_toolchain: Option<String>,
124}
125
126impl Display for Project {
127 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
128 write!(f, "ID: {}, Name: {}", self.id, self.name,)
129 }
130}
131#[derive(Serialize, Debug, Deserialize, Clone)]
134#[tsync]
135pub struct ProjectCreate {
136 pub name: String,
137 pub description: String,
138}
139
140#[derive(Deserialize, Serialize, Debug)]
141#[tsync]
142pub struct Deployment {
143 pub id: i32,
144 pub project_id: i32,
145 pub frontend_app_id: Option<String>,
146 pub frontend_app_name: Option<String>,
147 pub commit_hash: String,
148 pub status: DeploymentStatus,
149 #[serde(with = "ar_date_format")]
150 pub created_at: DateTime<Utc>,
151 #[serde(with = "ar_date_format")]
152 pub updated_at: DateTime<Utc>,
153}
154
155#[derive(Deserialize, Serialize, Debug)]
156pub struct DeploymentPayload {
157 pub commit_hash: String,
158 pub status: DeploymentStatus,
159 pub frontend_app_id: Option<String>,
160}
161
162#[derive(Deserialize_repr, Serialize_repr, Debug, Clone, Copy)] #[repr(u8)]
164#[tsync]
165pub enum DeploymentStatus {
166 Started = 0,
167 Failed,
168 Done,
169}
170
171impl Display for DeploymentStatus {
172 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
173 match self {
174 DeploymentStatus::Started => write!(f, "🚀"),
175 DeploymentStatus::Failed => write!(f, "❌"),
176 DeploymentStatus::Done => write!(f, "✅"),
177 }
178 }
179}
180
181#[cfg(test)]
182mod tests {
183 use super::*;
184 use serde_json::json;
185 #[test]
186 fn test_project_create() {
187 let project_create = ProjectCreate {
188 name: "test".to_owned(),
189 description: "test".to_owned(),
190 };
191 let json = json!({
192 "name": "test",
193 "description": "test"
194 });
195 assert_eq!(serde_json::to_value(project_create).unwrap(), json);
196 }
197
198 #[test]
199 fn test_deployment_status_display() {
200 assert_eq!(format!("{}", DeploymentStatus::Started), "🚀");
201 assert_eq!(DeploymentStatus::Started.to_string(), "🚀");
202
203 assert_eq!(format!("{}", DeploymentStatus::Failed), "❌");
204 assert_eq!(DeploymentStatus::Failed.to_string(), "❌");
205
206 assert_eq!(format!("{}", DeploymentStatus::Done), "✅");
207 assert_eq!(DeploymentStatus::Done.to_string(), "✅");
208 }
209}