coodev-runner 0.1.42

A simple runner for coodev
Documentation
use crate::error;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct RepositoryVolume {
  pub key: String,
  pub path: String,
  pub repository: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct OrganizationVolume {
  pub key: String,
  pub path: String,
  pub organization: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct GlobalVolume {
  pub key: String,
  pub path: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum Volume {
  Repository(RepositoryVolume),
  Organization(OrganizationVolume),
  Global(GlobalVolume),
}

pub struct VolumeBuilder {
  pub key: String,
  pub owner: Option<String>,
  pub repository: Option<String>,
  pub path: Option<String>,
}

impl VolumeBuilder {
  pub fn repository(&mut self, repository: impl Into<String>) -> &mut Self {
    self.repository = Some(repository.into());

    self
  }

  pub fn owner(&mut self, owner: impl Into<String>) -> &mut Self {
    self.owner = Some(owner.into());

    self
  }

  pub fn path(&mut self, path: impl Into<String>) -> &mut Self {
    self.path = Some(path.into());

    self
  }

  pub fn build(&self) -> crate::Result<Volume> {
    let key = self.key.clone();
    let path = self.path.clone().ok_or({
      error::Error::workflow_config_error(
        "Volume path is required. Please set volume path by Volume::new(name).path(path)",
      )
    })?;

    match (self.owner.clone(), self.repository.clone()) {
      (Some(owner), Some(repository)) => Ok(Volume::Repository(RepositoryVolume {
        key,
        path,
        repository: format!("{}/{}", owner, repository),
      })),
      (Some(owner), None) => Ok(Volume::Organization(OrganizationVolume {
        key,
        path,
        organization: owner,
      })),
      (None, None) => Ok(Volume::Global(GlobalVolume { key, path })),
      (None, Some(_)) => Err(
        error::Error::workflow_config_error(
          "Repository owner is required. Please set repository owner by Volume::new(name).owner(owner)"
      ),
      ),
    }
  }
}

impl Volume {
  pub fn new(key: impl Into<String>) -> VolumeBuilder {
    VolumeBuilder {
      key: key.into(),
      owner: None,
      repository: None,
      path: None,
    }
  }
}