Skip to main content

nanite_docker/instruction/
healthcheck.rs

1use alloc::{string::String, vec::Vec};
2
3use crate::Cmd;
4use core::fmt::{Display, Formatter};
5
6/// Represents a `HEALTHCHECK` instruction.
7/// ```rust
8/// use nanite_docker::{HealthCheck, HealthCheckOpt, Cmd};
9///
10/// let healthcheck = HealthCheck::Some {
11///     opts: vec![
12///         HealthCheckOpt::Interval("30s".into()),
13///         HealthCheckOpt::Retries(5),
14///     ],
15///     cmd: Cmd{ argv: vec!["echo".into(), "hello".into()] },
16/// };
17///
18/// let healthcheck_built = format!("{healthcheck}");
19/// assert_eq!(healthcheck_built, r#"HEALTHCHECK --interval=30s --retries=5 CMD ["echo", "hello"]"#)
20/// ```
21#[derive(Clone, Debug)]
22pub enum HealthCheck {
23    Some { opts: Vec<HealthCheckOpt>, cmd: Cmd },
24    None,
25}
26
27impl Display for HealthCheck {
28    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
29        match self {
30            Self::Some { opts, cmd } => {
31                write!(f, "HEALTHCHECK ")?;
32
33                for opt in opts {
34                    write!(f, "{opt} ")?;
35                }
36
37                write!(f, "{}", cmd)
38            }
39            Self::None => write!(f, "HEALTHCHECK NONE"),
40        }
41    }
42}
43
44#[derive(Clone, Debug)]
45pub enum HealthCheckOpt {
46    Interval(String),
47    Timeout(String),
48    StartPeriod(String),
49    StartInterval(String),
50    Retries(i32),
51}
52
53impl Display for HealthCheckOpt {
54    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
55        match self {
56            Self::Interval(d) => write!(f, "--interval={d}"),
57            Self::Timeout(d) => write!(f, "--timeout={d}"),
58            Self::StartPeriod(d) => write!(f, "--start-period={d}"),
59            Self::StartInterval(d) => write!(f, "--start-interval={d}"),
60            Self::Retries(n) => write!(f, "--retries={n}"),
61        }
62    }
63}