Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
feat(src/run.rs): represent metadata about a CI run
Lars Wirzenius committed 2 years ago
commit b096d9fb6f6322aae92070c0c8d6c6faad325a18
parent 13e2c2488e2ccbdcfefd46d76ad6fc42040184c1
1 file changed +83 -0
added 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())
+
    }
+
}