Skip to main content

nanite_docker/instruction/
expose.rs

1use core::fmt::{Display, Formatter};
2
3/// Represents an `EXPOSE` instruction.
4/// ```rust
5/// use nanite_docker::{Expose, ExposeProtocol};
6///
7/// let expose = Expose {
8///     port: 8080,
9///     protocol: Some(ExposeProtocol::tcp),
10/// };
11/// let expose_built = format!("{expose}");
12/// assert_eq!(expose_built, "EXPOSE 8080/tcp");
13/// ```
14#[derive(Clone, Debug)]
15pub struct Expose {
16    pub port: u16,
17    pub protocol: Option<ExposeProtocol>,
18}
19
20impl Display for Expose {
21    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
22        match &self.protocol {
23            Some(p) => match p {
24                ExposeProtocol::tcp => write!(f, "EXPOSE {}/tcp", self.port),
25                ExposeProtocol::udp => write!(f, "EXPOSE {}/udp", self.port),
26            },
27            None => write!(f, "EXPOSE {}", self.port),
28        }
29    }
30}
31
32/// Defines different port types for the EXPOSE Instruction
33#[allow(
34    nonstandard_style,
35    reason = "tcp and udp are standards that should be lowercase for legibility"
36)]
37#[derive(Clone, Debug)]
38pub enum ExposeProtocol {
39    tcp,
40    udp,
41}