use serde::Serialize; /// Describes a node container in the compose stack. #[derive(Clone, Debug, Serialize)] pub struct NodeDescriptor { name: String, image: String, entrypoint: String, volumes: Vec, extra_hosts: Vec, ports: Vec, #[serde(skip)] container_ports: Vec, environment: Vec, #[serde(skip_serializing_if = "Option::is_none")] platform: Option, } /// Environment variable entry for docker-compose templating. #[derive(Clone, Debug, Serialize, PartialEq, Eq)] pub struct EnvEntry { key: String, value: String, } impl EnvEntry { pub fn new(key: impl Into, value: impl Into) -> Self { Self { key: key.into(), value: value.into(), } } #[cfg(test)] pub fn key(&self) -> &str { &self.key } #[cfg(test)] pub fn value(&self) -> &str { &self.value } } impl NodeDescriptor { #[allow(clippy::too_many_arguments)] pub fn new( name: impl Into, image: impl Into, entrypoint: impl Into, volumes: Vec, extra_hosts: Vec, ports: Vec, container_ports: Vec, environment: Vec, platform: Option, ) -> Self { Self { name: name.into(), image: image.into(), entrypoint: entrypoint.into(), volumes, extra_hosts, ports, container_ports, environment, platform, } } #[allow(clippy::too_many_arguments)] pub fn with_loopback_ports( name: impl Into, image: impl Into, entrypoint: impl Into, volumes: Vec, extra_hosts: Vec, container_ports: Vec, environment: Vec, platform: Option, ) -> Self { Self::new( name, image, entrypoint, volumes, extra_hosts, container_ports .iter() .copied() .map(Self::loopback_port_binding) .collect(), container_ports, environment, platform, ) } #[must_use] pub fn loopback_port_binding(port: u16) -> String { format!("127.0.0.1::{port}") } pub fn ports(&self) -> &[String] { &self.ports } pub fn container_ports(&self) -> &[u16] { &self.container_ports } pub fn image(&self) -> &str { &self.image } pub fn platform(&self) -> Option<&str> { self.platform.as_deref() } #[cfg(test)] pub fn test_ports(&self) -> &[String] { self.ports() } #[cfg(test)] pub fn environment(&self) -> &[EnvEntry] { &self.environment } }