1 use {
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 /// How the project's files are delivered to the server.
11 ///
12 /// `Git` — the classic smbCloud flow: push to a remote git repo, the server
13 /// builds and restarts the process.
14 /// `Rsync` — files are transferred directly with rsync over SSH; no build step
15 /// runs on the server. Ideal for pre-built static sites or assets.
16 #[derive(Deserialize_repr, Serialize_repr, Debug, Clone, Copy, PartialEq, Eq, Default)]
17 #[repr(u8)]
18 #[tsync]
19 pub enum DeploymentMethod {
20 #[default]
21 Git = 0,
22 Rsync = 1,
23 }
24
25 impl 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)]
35 pub struct Config {
36 pub current_project: Option<Project>,
37 pub current_auth_app: Option<AuthApp>,
38 }
39
40 #[derive(Deserialize, Serialize, Debug, Clone)]
41 #[tsync]
42 pub struct Project {
43 pub id: i32,
44 pub name: String,
45 pub runner: Runner,
46 /// Defaults to `Git` when absent (older API responses won't include the field).
47 #[serde(default)]
48 pub deployment_method: DeploymentMethod,
49 pub path: Option<String>,
50 pub repository: Option<String>,
51 pub description: Option<String>,
52 pub created_at: DateTime<Utc>,
53 pub updated_at: DateTime<Utc>,
54 /// Deployment kind, e.g. "vite-spa". Absent for server-side runners.
55 pub kind: Option<String>,
56 /// Local source directory to build from, e.g. "frontend/connected-devices".
57 /// Used by vite-spa deploys as the working directory for the build step.
58 /// Distinct from `path`, which is the remote destination on the server.
59 pub source: Option<String>,
60 /// Build output directory relative to `source`, e.g. "dist".
61 pub output: Option<String>,
62 /// Package manager to use for the build step, e.g. "pnpm".
63 pub package_manager: Option<String>,
64 /// PM2 process name to restart after a nextjs-ssr deploy, e.g. "my-app".
65 /// Matches the name passed to `pm2 start` on the server.
66 pub pm2_app: Option<String>,
67 /// Path to a shared lib directory to rsync to the server before deploying,
68 /// e.g. "lib". Used by Rails apps that depend on native gems built from
69 /// monorepo-level source. Relative to the repo root.
70 pub shared_lib: Option<String>,
71 /// SSH command to run on the server after rsyncing the shared lib,
72 /// e.g. "cd ~/lib/gems/gem_error_codes && rbenv local 3.4.2 && bundle install && bundle exec rake compile".
73 pub compile_cmd: Option<String>,
74 }
75
76 impl Display for Project {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 write!(f, "ID: {}, Name: {}", self.id, self.name,)
79 }
80 }
81 #[derive(Serialize, Debug, Deserialize, Clone)]
82 #[tsync]
83 pub struct ProjectCreate {
84 pub name: String,
85 pub repository: String,
86 pub description: String,
87 pub runner: Runner,
88 #[serde(default)]
89 pub deployment_method: DeploymentMethod,
90 }
91
92 #[derive(Deserialize, Serialize, Debug)]
93 #[tsync]
94 pub struct Deployment {
95 pub id: i32,
96 pub project_id: i32,
97 pub commit_hash: String,
98 pub status: DeploymentStatus,
99 #[serde(with = "ar_date_format")]
100 pub created_at: DateTime<Utc>,
101 #[serde(with = "ar_date_format")]
102 pub updated_at: DateTime<Utc>,
103 }
104
105 #[derive(Deserialize, Serialize, Debug)]
106 pub struct DeploymentPayload {
107 pub commit_hash: String,
108 pub status: DeploymentStatus,
109 }
110
111 #[derive(Deserialize_repr, Serialize_repr, Debug, Clone, Copy)] // Added Clone, Copy
112 #[repr(u8)]
113 #[tsync]
114 pub enum DeploymentStatus {
115 Started = 0,
116 Failed,
117 Done,
118 }
119
120 impl Display for DeploymentStatus {
121 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122 match self {
123 DeploymentStatus::Started => write!(f, "🚀"),
124 DeploymentStatus::Failed => write!(f, "❌"),
125 DeploymentStatus::Done => write!(f, "✅"),
126 }
127 }
128 }
129
130 #[cfg(test)]
131 mod tests {
132 use super::*;
133 use serde_json::json;
134 #[test]
135 fn test_project_create() {
136 let project_create = ProjectCreate {
137 name: "test".to_owned(),
138 repository: "test".to_owned(),
139 description: "test".to_owned(),
140 runner: Runner::NodeJs,
141 deployment_method: DeploymentMethod::Git,
142 };
143 let json = json!({
144 "name": "test",
145 "repository": "test",
146 "description": "test",
147 "runner": 0,
148 "deployment_method": 0
149 });
150 assert_eq!(serde_json::to_value(project_create).unwrap(), json);
151 }
152
153 #[test]
154 fn test_deployment_status_display() {
155 assert_eq!(format!("{}", DeploymentStatus::Started), "🚀");
156 assert_eq!(DeploymentStatus::Started.to_string(), "🚀");
157
158 assert_eq!(format!("{}", DeploymentStatus::Failed), "❌");
159 assert_eq!(DeploymentStatus::Failed.to_string(), "❌");
160
161 assert_eq!(format!("{}", DeploymentStatus::Done), "✅");
162 assert_eq!(DeploymentStatus::Done.to_string(), "✅");
163 }
164 }