gh_workflow/
container.rs

1//! Container configuration types for GitHub workflow jobs.
2
3use derive_setters::Setters;
4use serde::{Deserialize, Serialize};
5
6use crate::env::Env;
7
8/// Represents a container configuration for jobs.
9#[derive(Debug, Setters, Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
10#[serde(rename_all = "kebab-case")]
11#[setters(strip_option, into)]
12pub struct Container {
13    /// The image to use for the container.
14    pub image: String,
15
16    /// Credentials for accessing the container.
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub credentials: Option<Credentials>,
19
20    /// Environment variables for the container.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub env: Option<Env>,
23
24    /// Ports to expose from the container.
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub ports: Option<Vec<Port>>,
27
28    /// Volumes to mount in the container.
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub volumes: Option<Vec<Volume>>,
31
32    /// Additional options for the container.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub options: Option<String>,
35
36    /// Hostname for the container.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub hostname: Option<String>,
39}
40
41/// Represents credentials for accessing a container.
42#[derive(Debug, Setters, Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
43#[serde(rename_all = "kebab-case")]
44#[setters(strip_option, into)]
45pub struct Credentials {
46    /// The username for authentication.
47    pub username: String,
48
49    /// The password for authentication.
50    pub password: String,
51}
52
53/// Represents a network port.
54#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
55#[serde(rename_all = "kebab-case")]
56pub enum Port {
57    /// A port specified by its number.
58    Number(u16),
59
60    /// A port specified by its name.
61    Name(String),
62}
63
64/// Represents a volume configuration for containers.
65#[derive(Debug, Setters, Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
66#[serde(rename_all = "kebab-case")]
67#[setters(strip_option, into)]
68pub struct Volume {
69    /// The source path of the volume.
70    pub source: String,
71
72    /// The destination path of the volume.
73    pub destination: String,
74}
75
76impl Volume {
77    /// Creates a new `Volume` from a string representation.
78    pub fn new(volume_str: &str) -> Option<Self> {
79        let parts: Vec<&str> = volume_str.split(':').collect();
80        if parts.len() == 2 {
81            Some(Volume {
82                source: parts[0].to_string(),
83                destination: parts[1].to_string(),
84            })
85        } else {
86            None
87        }
88    }
89}