1use {
2 crate::{app_auth::AuthApp, ar_date_format, runner::Runner, tenant::Tenant},
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, Default)]
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 #[serde(default)]
50 pub current_tenant: Option<Tenant>,
51}
52
53#[derive(Deserialize, Serialize, Debug, Clone)]
54#[tsync]
55pub struct Project {
56 pub id: i32,
58 #[serde(default)]
61 pub tenant_id: Option<i64>,
62 #[serde(default)]
63 pub name: String,
64 #[serde(default)]
65 pub runner: Runner,
66 #[serde(default)]
68 pub deployment_method: DeploymentMethod,
69 pub path: Option<String>,
70 pub repository: Option<String>,
71 pub description: Option<String>,
72 pub frontend_app_id: Option<String>,
75 pub deploy_repo_id: Option<i64>,
78 pub source_path: Option<String>,
80 #[serde(default = "default_datetime")]
81 pub created_at: DateTime<Utc>,
82 #[serde(default = "default_datetime")]
83 pub updated_at: DateTime<Utc>,
84 pub kind: Option<String>,
86 pub source: Option<String>,
91 pub output: Option<String>,
93 pub package_manager: Option<String>,
95 pub pm2_app: Option<String>,
98 #[serde(default, skip_serializing_if = "Option::is_none")]
101 pub pm2_env: Option<std::collections::HashMap<String, serde_json::Value>>,
102 #[serde(default)]
104 pub port: Option<u16>,
105 pub shared_lib: Option<String>,
109 pub compile_cmd: Option<String>,
112 #[serde(default)]
114 pub install_command: Option<String>,
115 pub binary_name: Option<String>,
118 pub rust_target: Option<String>,
121 pub swift_sdk: Option<String>,
126 pub swift_toolchain: Option<String>,
133}
134
135impl Display for Project {
136 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137 write!(f, "ID: {}, Name: {}", self.id, self.name,)
138 }
139}
140#[derive(Serialize, Debug, Deserialize, Clone)]
143#[tsync]
144pub struct ProjectCreate {
145 pub name: String,
146 pub description: String,
147}
148
149#[derive(Deserialize, Serialize, Debug)]
150#[tsync]
151pub struct Deployment {
152 pub id: i32,
153 pub project_id: i32,
154 pub frontend_app_id: Option<String>,
155 pub frontend_app_name: Option<String>,
156 pub commit_hash: String,
157 pub status: DeploymentStatus,
158 #[serde(with = "ar_date_format")]
159 pub created_at: DateTime<Utc>,
160 #[serde(with = "ar_date_format")]
161 pub updated_at: DateTime<Utc>,
162}
163
164#[derive(Deserialize, Serialize, Debug)]
165pub struct DeploymentPayload {
166 pub commit_hash: String,
167 pub status: DeploymentStatus,
168 pub frontend_app_id: Option<String>,
169}
170
171#[derive(Deserialize_repr, Serialize_repr, Debug, Clone, Copy)] #[repr(u8)]
173#[tsync]
174pub enum DeploymentStatus {
175 Started = 0,
176 Failed,
177 Done,
178}
179
180impl Display for DeploymentStatus {
181 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
182 match self {
183 DeploymentStatus::Started => write!(f, "🚀"),
184 DeploymentStatus::Failed => write!(f, "❌"),
185 DeploymentStatus::Done => write!(f, "✅"),
186 }
187 }
188}
189
190#[cfg(test)]
191mod tests {
192 use super::*;
193 use serde_json::json;
194 #[test]
195 fn test_project_create() {
196 let project_create = ProjectCreate {
197 name: "test".to_owned(),
198 description: "test".to_owned(),
199 };
200 let json = json!({
201 "name": "test",
202 "description": "test"
203 });
204 assert_eq!(serde_json::to_value(project_create).unwrap(), json);
205 }
206
207 #[test]
208 fn test_deployment_status_display() {
209 assert_eq!(format!("{}", DeploymentStatus::Started), "🚀");
210 assert_eq!(DeploymentStatus::Started.to_string(), "🚀");
211
212 assert_eq!(format!("{}", DeploymentStatus::Failed), "❌");
213 assert_eq!(DeploymentStatus::Failed.to_string(), "❌");
214
215 assert_eq!(format!("{}", DeploymentStatus::Done), "✅");
216 assert_eq!(DeploymentStatus::Done.to_string(), "✅");
217 }
218}