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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use super::{utils::*, *};
use std::collections::HashMap;

impl From<astro_run::RunResult> for run_result::Result {
  fn from(value: astro_run::RunResult) -> Self {
    match value {
      astro_run::RunResult::Cancelled => run_result::Result::Cancelled(()),
      astro_run::RunResult::Succeeded => run_result::Result::Succeeded(()),
      astro_run::RunResult::Failed { exit_code } => run_result::Result::Failed(exit_code),
    }
  }
}

impl Into<astro_run::RunResult> for run_result::Result {
  fn into(self) -> astro_run::RunResult {
    match self {
      run_result::Result::Cancelled(_) => astro_run::RunResult::Cancelled,
      run_result::Result::Failed(exit_code) => astro_run::RunResult::Failed { exit_code },
      run_result::Result::Succeeded(_) => astro_run::RunResult::Succeeded,
    }
  }
}

impl TryInto<astro_run::StepRunResult> for StepRunResult {
  type Error = astro_run::Error;

  fn try_into(self) -> Result<astro_run::StepRunResult, Self::Error> {
    let started_at = convert_timestamp_to_datetime(&self.started_at)?;
    let completed_at = convert_timestamp_to_datetime(&self.completed_at)?;

    Ok(astro_run::StepRunResult {
      id: astro_run::StepId::try_from(self.id.as_str())?,
      state: WorkflowState::from_i32(self.state)
        .ok_or(astro_run::Error::internal_runtime_error(format!(
          "Invalid WorkflowState value: {}",
          self.state
        )))?
        .into(),
      exit_code: self.exit_code,
      started_at,
      completed_at,
    })
  }
}

impl TryFrom<astro_run::StepRunResult> for StepRunResult {
  type Error = astro_run::Error;

  fn try_from(value: astro_run::StepRunResult) -> Result<Self, Self::Error> {
    let started_at = convert_datetime_to_timestamp(&value.started_at)?;
    let completed_at = convert_datetime_to_timestamp(&value.completed_at)?;

    Ok(StepRunResult {
      id: value.id.to_string(),
      state: value.state as i32,
      exit_code: value.exit_code,
      started_at,
      completed_at,
    })
  }
}

impl TryInto<astro_run::JobRunResult> for JobRunResult {
  type Error = astro_run::Error;

  fn try_into(self) -> Result<astro_run::JobRunResult, Self::Error> {
    let started_at = convert_timestamp_to_datetime(&self.started_at)?;
    let completed_at = convert_timestamp_to_datetime(&self.completed_at)?;

    let mut steps = Vec::new();
    for step in self.steps {
      steps.push(step.try_into()?);
    }

    Ok(astro_run::JobRunResult {
      id: astro_run::JobId::try_from(self.id.as_str())?,
      state: WorkflowState::from_i32(self.state)
        .ok_or(astro_run::Error::internal_runtime_error(format!(
          "Invalid WorkflowState value: {}",
          self.state
        )))?
        .into(),
      started_at,
      completed_at,
      steps,
    })
  }
}

impl TryFrom<astro_run::JobRunResult> for JobRunResult {
  type Error = astro_run::Error;

  fn try_from(value: astro_run::JobRunResult) -> Result<Self, Self::Error> {
    let started_at = convert_datetime_to_timestamp(&value.started_at)?;
    let completed_at = convert_datetime_to_timestamp(&value.completed_at)?;

    let mut steps = Vec::new();
    for step in value.steps {
      steps.push(step.try_into()?);
    }

    Ok(JobRunResult {
      id: value.id.to_string(),
      state: value.state as i32,
      started_at,
      completed_at,
      steps,
    })
  }
}

impl TryInto<astro_run::WorkflowRunResult> for WorkflowRunResult {
  type Error = astro_run::Error;

  fn try_into(self) -> Result<astro_run::WorkflowRunResult, Self::Error> {
    let started_at = convert_timestamp_to_datetime(&self.started_at)?;
    let completed_at = convert_timestamp_to_datetime(&self.completed_at)?;

    let mut jobs = HashMap::new();
    for (key, job) in self.jobs {
      jobs.insert(key, job.try_into()?);
    }

    Ok(astro_run::WorkflowRunResult {
      id: astro_run::WorkflowId::try_from(self.id.as_str())?,
      state: WorkflowState::from_i32(self.state)
        .ok_or(astro_run::Error::internal_runtime_error(format!(
          "Invalid WorkflowState value: {}",
          self.state
        )))?
        .into(),
      started_at,
      completed_at,
      jobs,
    })
  }
}

impl TryFrom<astro_run::WorkflowRunResult> for WorkflowRunResult {
  type Error = astro_run::Error;

  fn try_from(value: astro_run::WorkflowRunResult) -> Result<Self, Self::Error> {
    let started_at = convert_datetime_to_timestamp(&value.started_at)?;
    let completed_at = convert_datetime_to_timestamp(&value.completed_at)?;

    let mut jobs = HashMap::new();
    for (key, job) in value.jobs {
      jobs.insert(key, job.try_into()?);
    }

    Ok(WorkflowRunResult {
      id: value.id.to_string(),
      state: value.state as i32,
      started_at,
      completed_at,
      jobs,
    })
  }
}