e2e_test_model/
step.rs

1use serde::{Deserialize, Serialize};
2#[cfg(feature = "sqlx")]
3use sqlx::{FromRow, Type};
4use time::OffsetDateTime;
5use uuid::Uuid;
6
7use crate::{CreateStepRequest, ExecutionResult};
8
9#[derive(Debug, Deserialize, Serialize)]
10#[cfg_attr(feature = "sqlx", derive(FromRow))]
11pub struct Step {
12    pub execution_uuid: Uuid,
13    pub state: StepState,
14    pub result: StepResult,
15    #[serde(with = "time::serde::rfc3339")]
16    pub created_at: OffsetDateTime,
17}
18
19impl Default for Step {
20    fn default() -> Self {
21        Self {
22            execution_uuid: Default::default(),
23            state: StepState::Setup,
24            result: StepResult::Skipped,
25            created_at: OffsetDateTime::now_utc(),
26        }
27    }
28}
29
30impl Step {
31    pub fn execution_result(&self) -> Option<ExecutionResult> {
32        match (&self.state, &self.result) {
33            (_, StepResult::Failed) => Some(ExecutionResult::Failed),
34            (StepState::Teardown, StepResult::Passed | StepResult::Skipped) => {
35                Some(ExecutionResult::Passed)
36            }
37            (StepState::Setup | StepState::Run, StepResult::Passed | StepResult::Skipped) => None,
38        }
39    }
40}
41
42#[cfg_attr(feature = "sqlx", derive(Type))]
43#[cfg_attr(
44    feature = "sqlx",
45    sqlx(type_name = "step_state", rename_all = "snake_case")
46)]
47#[derive(Debug, Deserialize, Serialize)]
48#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
49pub enum StepState {
50    Setup,
51    Run,
52    Teardown,
53}
54
55#[cfg_attr(feature = "sqlx", derive(Type))]
56#[cfg_attr(
57    feature = "sqlx",
58    sqlx(type_name = "step_result", rename_all = "snake_case")
59)]
60#[derive(Debug, Deserialize, Serialize)]
61#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
62pub enum StepResult {
63    Passed,
64    Failed,
65    Skipped,
66}
67
68pub enum ExhaustiveSteps {
69    SetupPassed,
70    SetupFailed,
71    SetupSkipped,
72    RunPassed,
73    RunFailed,
74    RunSkipped,
75    TeardownPassed,
76    TeardownFailed,
77    TeardownSkipped,
78}
79
80impl From<ExhaustiveSteps> for CreateStepRequest {
81    fn from(exhaustive_step: ExhaustiveSteps) -> Self {
82        let (state, result) = match exhaustive_step {
83            ExhaustiveSteps::SetupPassed => (StepState::Setup, StepResult::Passed),
84            ExhaustiveSteps::SetupFailed => (StepState::Setup, StepResult::Failed),
85            ExhaustiveSteps::SetupSkipped => (StepState::Setup, StepResult::Skipped),
86            ExhaustiveSteps::RunPassed => (StepState::Run, StepResult::Passed),
87            ExhaustiveSteps::RunFailed => (StepState::Run, StepResult::Failed),
88            ExhaustiveSteps::RunSkipped => (StepState::Run, StepResult::Skipped),
89            ExhaustiveSteps::TeardownPassed => (StepState::Teardown, StepResult::Passed),
90            ExhaustiveSteps::TeardownFailed => (StepState::Teardown, StepResult::Failed),
91            ExhaustiveSteps::TeardownSkipped => (StepState::Teardown, StepResult::Skipped),
92        };
93
94        Self { state, result }
95    }
96}