docker_utils/
container_config.rs

1/*
2 * SPDX-License-Identifier: MIT
3 * Copyright (c) "2025" . The buildutils Authors and Contributors. All Rights Reserved.
4 */
5
6use std::fmt::Display;
7use wait_utils::WaitStrategy;
8// https://github.com/elastio/bon
9use bon::Builder;
10
11/// Create a new instance of the `ContainerConfig` struct with the given parameters.
12///
13/// Examples
14///
15/// Creating a new configuration using the derived builder:
16/// ```rust
17///  use docker_utils::*;
18///
19///     let config = ContainerConfig::builder()
20///         .name("test_container")
21///         .image("test_image")
22///         .tag("latest")
23///         .url("0.0.0.0")
24///         .connection_port(8080)
25///         .reuse_container(true)
26///         .keep_configuration(true)
27///         .wait_strategy(WaitStrategy::NoWait)
28///         .build();
29/// ```
30///
31#[derive(Builder, Debug, Default, Clone, Eq, PartialOrd, Ord, PartialEq, Hash)]
32pub struct ContainerConfig<'l> {
33    name: &'l str,
34    image: &'l str,
35    tag: &'l str,
36    url: &'l str,
37    connection_port: u16,
38    reuse_container: bool,
39    keep_configuration: bool,
40    additional_ports: Option<&'l [u16]>,
41    additional_env_vars: Option<&'l [&'l str]>,
42    platform: Option<&'l str>,
43    wait_strategy: WaitStrategy,
44}
45
46impl<'l> ContainerConfig<'l> {
47    /// Create a new instance of the `ContainerConfig` struct with the given parameters.
48    ///
49    /// # Arguments
50    ///
51    /// * `name` - The name of the container.
52    /// * `image` - The image to use for the container.
53    /// * `tag` - The tag of the image.
54    /// * `url` - The default URL of the container. Usually 0.0.0.0
55    /// * `connection_port` - The port number for the main connection i.e. 80 for a webserver.
56    /// * `additional_ports` - An optional array of additional ports to publish.
57    /// * `platform` - An optional platform string in case the container image is not multi-arch.
58    /// * `reuse_container` - A boolean flag indicating whether to reuse an existing container if found.
59    /// * `keep_configuration` -  A boolean flag indication whether to keep the configuration upon
60    ///    every environment setup. If set to true, the same configuration will be used across all
61    ///    environment setups. If false, each setup will re-create all tables and import data.,
62    /// * `wait_strategy` - The wait strategy to use for the container.
63    ///
64    /// Examples
65    ///
66    /// Creating a new configuration using the constructor:
67    /// ```rust
68    /// use docker_utils::*;
69    ///
70    ///     let config =  ContainerConfig::new(
71    ///         "test_container",
72    ///         "test_image",
73    ///         "latest",
74    ///         "0.0.0.0",
75    ///         8080,
76    ///         Some(&[8081, 8082]),
77    ///         Some(&["ENV_VAR=VALUE", "DEBUG=true"]),
78    ///         Some("linux/amd64"),
79    ///         true,
80    ///         false,
81    ///         WaitStrategy::default(), // NoWait is the default wait strategy
82    ///     );
83    /// ```
84    ///
85    /// # Returns
86    ///
87    /// Returns a new instance of the `ContainerConfig` struct.
88    ///
89    #[allow(clippy::too_many_arguments)]
90    pub const fn new(
91        name: &'l str,
92        image: &'l str,
93        tag: &'l str,
94        url: &'l str,
95        connection_port: u16,
96        additional_ports: Option<&'l [u16]>,
97        additional_env_vars: Option<&'l [&'l str]>,
98        platform: Option<&'l str>,
99        reuse_container: bool,
100        keep_configuration: bool,
101        wait_strategy: WaitStrategy,
102    ) -> Self {
103        Self {
104            name,
105            image,
106            tag,
107            url,
108            connection_port,
109            reuse_container,
110            keep_configuration,
111            additional_ports,
112            additional_env_vars,
113            platform,
114            wait_strategy,
115        }
116    }
117}
118
119impl<'l> ContainerConfig<'l> {
120    #[inline]
121    pub const fn name(&self) -> &'l str {
122        self.name
123    }
124    #[inline]
125    pub fn container_image(&self) -> String {
126        format!("{}:{}", self.image, self.tag)
127    }
128    #[inline]
129    pub fn container_name(&self) -> String {
130        format!("{}-{}", self.name, self.connection_port)
131    }
132    #[inline]
133    pub const fn url(&self) -> &'l str {
134        self.url
135    }
136    #[inline]
137    pub const fn connection_port(&self) -> u16 {
138        self.connection_port
139    }
140    #[inline]
141    pub const fn additional_ports(&self) -> Option<&'l [u16]> {
142        self.additional_ports
143    }
144    #[inline]
145    pub const fn additional_env_vars(&self) -> Option<&'l [&'l str]> {
146        self.additional_env_vars
147    }
148    #[inline]
149    pub const fn platform(&self) -> Option<&'l str> {
150        self.platform
151    }
152    #[inline]
153    pub const fn reuse_container(&self) -> bool {
154        self.reuse_container
155    }
156    #[inline]
157    pub const fn keep_configuration(&self) -> bool {
158        self.keep_configuration
159    }
160    #[inline]
161    pub const fn wait_strategy(&self) -> &WaitStrategy {
162        &self.wait_strategy
163    }
164    #[inline]
165    pub const fn image(&self) -> &'l str {
166        self.image
167    }
168    #[inline]
169    pub const fn tag(&self) -> &'l str {
170        self.tag
171    }
172}
173
174impl Display for ContainerConfig<'_> {
175    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
176        write!(
177            f,
178            "name: {}, image: {}:{}, url: {} connection_port: {}, additional_ports: {:?}, \
179            additional_env_vars: {:?}, platform: {:?},  reuse_container: {}, keep_configuration: {}, wait_strategy: {}",
180            self.name,
181            self.image,
182            self.tag,
183            self.url,
184            self.connection_port,
185            self.additional_ports,
186            self.additional_env_vars,
187            self.platform,
188            self.reuse_container,
189            self.keep_configuration,
190            self.wait_strategy,
191        )
192    }
193}