diff options
author | Lars Wirzenius <liw@liw.fi> | 2024-02-02 17:32:41 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2024-02-02 17:40:37 +0200 |
commit | b096d9fb6f6322aae92070c0c8d6c6faad325a18 (patch) | |
tree | 64cecfa627450374a68b789152d1ce68eb4723d5 | |
parent | 13e2c2488e2ccbdcfefd46d76ad6fc42040184c1 (diff) | |
download | radicle-ci-broker-b096d9fb6f6322aae92070c0c8d6c6faad325a18.tar.gz |
feat(src/run.rs): represent metadata about a CI run
This captures all the metadata about a CI run that the CI broker needs
to know. It can also be updated piecemeal.
Signed-off-by: Lars Wirzenius <liw@liw.fi>
-rw-r--r-- | src/run.rs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/run.rs b/src/run.rs new file mode 100644 index 0000000..8029c95 --- /dev/null +++ b/src/run.rs @@ -0,0 +1,83 @@ +use std::fmt; + +use serde::{Deserialize, Serialize}; + +use crate::msg::{RunId, RunResult}; + +#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] +pub struct Run { + broker_run_id: RunId, + adapter_run_id: Option<RunId>, + state: RunState, + result: Option<RunResult>, +} + +impl Default for Run { + fn default() -> Self { + Self { + broker_run_id: RunId::default(), + adapter_run_id: None, + state: RunState::Triggered, + result: None, + } + } +} + +impl Run { + /// Return the run id assigned by the broker. This is set when the + /// run is created and can't be changed. + pub fn broker_run_id(&self) -> &RunId { + &self.broker_run_id + } + + /// Set the run id assigned by the adapter. + pub fn set_adapter_run_id(&mut self, run_id: RunId) { + self.adapter_run_id = Some(run_id); + } + + /// Return the run id assigned by the adapter, if any. + pub fn adapter_run_id(&self) -> Option<&RunId> { + self.adapter_run_id.as_ref() + } + + /// Return the state of the CI run. + pub fn state(&self) -> RunState { + self.state + } + + /// Set the state of the run. + pub fn set_state(&mut self, state: RunState) { + self.state = state; + } + + /// Set the result of the CI run. + pub fn set_result(&mut self, result: RunResult) { + self.result = Some(result); + } + + /// Return the result of the CI run. + pub fn result(&self) -> Option<&RunResult> { + self.result.as_ref() + } +} + +/// State of CI run. +#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] +#[serde(rename_all = "snake_case")] +pub enum RunState { + /// Run has been triggered, but has not yet been started. + Triggered, + + /// Run is currently running. + Running, + + /// Run has finished. It may or may not have succeeded. + Finished, +} + +impl fmt::Display for RunState { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(f, "{}", serde_json::to_string(self).unwrap()) + } +} |