crates/flotilla-resources/src/checkout.rs
Line | Count | Source |
1 | | use serde::{Deserialize, Serialize}; |
2 | | |
3 | | use crate::{resource::define_resource, status_patch::StatusPatch}; |
4 | | |
5 | | define_resource!(Checkout, "checkouts", CheckoutSpec, CheckoutStatus, CheckoutStatusPatch); |
6 | | |
7 | | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
8 | | pub struct CheckoutSpec { |
9 | | pub env_ref: String, |
10 | | #[serde(rename = "ref")] |
11 | | pub r#ref: String, |
12 | | pub target_path: String, |
13 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
14 | | pub worktree: Option<CheckoutWorktreeSpec>, |
15 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
16 | | pub fresh_clone: Option<FreshCloneCheckoutSpec>, |
17 | | } |
18 | | |
19 | | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
20 | | pub struct CheckoutWorktreeSpec { |
21 | | pub clone_ref: String, |
22 | | } |
23 | | |
24 | | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
25 | | pub struct FreshCloneCheckoutSpec { |
26 | | pub url: String, |
27 | | } |
28 | | |
29 | | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] |
30 | | pub enum CheckoutPhase { |
31 | | #[default] |
32 | | Pending, |
33 | | Preparing, |
34 | | Ready, |
35 | | Terminating, |
36 | | Failed, |
37 | | } |
38 | | |
39 | | #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] |
40 | | pub struct CheckoutStatus { |
41 | | pub phase: CheckoutPhase, |
42 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
43 | | pub path: Option<String>, |
44 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
45 | | pub commit: Option<String>, |
46 | | #[serde(default, skip_serializing_if = "Option::is_none")] |
47 | | pub message: Option<String>, |
48 | | } |
49 | | |
50 | | #[derive(Debug, Clone, PartialEq, Eq)] |
51 | | pub enum CheckoutStatusPatch { |
52 | | MarkPreparing, |
53 | | MarkReady { path: String, commit: Option<String> }, |
54 | | MarkTerminating, |
55 | | MarkFailed { message: String }, |
56 | | } |
57 | | |
58 | | impl StatusPatch<CheckoutStatus> for CheckoutStatusPatch { |
59 | 6 | fn apply(&self, status: &mut CheckoutStatus) { |
60 | 6 | match self { |
61 | 1 | Self::MarkPreparing => { |
62 | 1 | status.phase = CheckoutPhase::Preparing; |
63 | 1 | status.message = None; |
64 | 1 | } |
65 | 4 | Self::MarkReady { path, commit } => { |
66 | 4 | status.phase = CheckoutPhase::Ready; |
67 | 4 | status.path = Some(path.clone()); |
68 | 4 | status.commit = commit.clone(); |
69 | 4 | status.message = None; |
70 | 4 | } |
71 | 0 | Self::MarkTerminating => { |
72 | 0 | status.phase = CheckoutPhase::Terminating; |
73 | 0 | } |
74 | 1 | Self::MarkFailed { message } => { |
75 | 1 | status.phase = CheckoutPhase::Failed; |
76 | 1 | status.message = Some(message.clone()); |
77 | 1 | } |
78 | | } |
79 | 6 | } |
80 | | } |