coodev_runner/runner/
volumes.rs1use crate::error;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
5pub struct RepositoryVolume {
6 pub key: String,
7 pub path: String,
8 pub repository: String,
9}
10
11#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
12pub struct OrganizationVolume {
13 pub key: String,
14 pub path: String,
15 pub organization: String,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
19pub struct GlobalVolume {
20 pub key: String,
21 pub path: String,
22}
23
24#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
25pub enum Volume {
26 Repository(RepositoryVolume),
27 Organization(OrganizationVolume),
28 Global(GlobalVolume),
29}
30
31pub struct VolumeBuilder {
32 pub key: String,
33 pub owner: Option<String>,
34 pub repository: Option<String>,
35 pub path: Option<String>,
36}
37
38impl VolumeBuilder {
39 pub fn repository(&mut self, repository: impl Into<String>) -> &mut Self {
40 self.repository = Some(repository.into());
41
42 self
43 }
44
45 pub fn owner(&mut self, owner: impl Into<String>) -> &mut Self {
46 self.owner = Some(owner.into());
47
48 self
49 }
50
51 pub fn path(&mut self, path: impl Into<String>) -> &mut Self {
52 self.path = Some(path.into());
53
54 self
55 }
56
57 pub fn build(&self) -> crate::Result<Volume> {
58 let key = self.key.clone();
59 let path = self.path.clone().ok_or({
60 error::Error::workflow_config_error(
61 "Volume path is required. Please set volume path by Volume::new(name).path(path)",
62 )
63 })?;
64
65 match (self.owner.clone(), self.repository.clone()) {
66 (Some(owner), Some(repository)) => Ok(Volume::Repository(RepositoryVolume {
67 key,
68 path,
69 repository: format!("{}/{}", owner, repository),
70 })),
71 (Some(owner), None) => Ok(Volume::Organization(OrganizationVolume {
72 key,
73 path,
74 organization: owner,
75 })),
76 (None, None) => Ok(Volume::Global(GlobalVolume { key, path })),
77 (None, Some(_)) => Err(
78 error::Error::workflow_config_error(
79 "Repository owner is required. Please set repository owner by Volume::new(name).owner(owner)"
80 ),
81 ),
82 }
83 }
84}
85
86impl Volume {
87 pub fn new(key: impl Into<String>) -> VolumeBuilder {
88 VolumeBuilder {
89 key: key.into(),
90 owner: None,
91 repository: None,
92 path: None,
93 }
94 }
95}