1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
mod step;

use std::fmt::Display;

use serde::{Deserialize, Serialize};
#[cfg(feature = "sqlx")]
use sqlx::{FromRow, Type};
use time::OffsetDateTime;
use uuid::Uuid;

pub use step::*;

#[derive(Debug, Deserialize, Serialize)]
pub struct CreateExecutionRequest {
    pub product: String,
    pub repository: String,
    pub ref_name: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct GetExecutionsRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub product: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repository: Option<String>,
}

#[cfg_attr(feature = "sqlx", derive(Type))]
#[cfg_attr(
    feature = "sqlx",
    sqlx(type_name = "execution_result", rename_all = "snake_case")
)]
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ExecutionResult {
    InProgress,
    Passed,
    Failed,
}

impl Display for ExecutionResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExecutionResult::InProgress => write!(f, "in_progress"),
            ExecutionResult::Passed => write!(f, "passed"),
            ExecutionResult::Failed => write!(f, "failed"),
        }
    }
}

// TODO I think this should work, but it's not currently
// #[cfg(feature = "sqlx")]
// impl PgHasArrayType for ExecutionResult {
//     fn array_type_info() -> sqlx::postgres::PgTypeInfo {
//         PgTypeInfo::with_name("_execution_result")
//     }
// }

impl Default for ExecutionResult {
    fn default() -> Self {
        Self::InProgress
    }
}

#[derive(Debug, Deserialize, Serialize)]
#[cfg_attr(feature = "sqlx", derive(FromRow))]
pub struct Execution {
    #[cfg_attr(feature = "sqlx", sqlx(flatten))]
    pub uuid: Uuid,
    pub product: String,
    pub repository: String,
    pub ref_name: String,
    pub result: ExecutionResult,
    #[serde(with = "time::serde::rfc3339")]
    pub created_at: OffsetDateTime,
}

impl Default for Execution {
    fn default() -> Self {
        let (uuid, product, repository, ref_name, result) = Default::default();

        Self {
            uuid,
            product,
            repository,
            ref_name,
            result,
            created_at: OffsetDateTime::now_utc(),
        }
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct CreateStepRequest {
    pub state: StepState,
    pub result: StepResult,
}