service_utils/
service_config.rs

1/*
2 * SPDX-License-Identifier: MIT
3 * Copyright (c) "2025" . The buildutils Authors and Contributors. All Rights Reserved.
4 */
5
6// https://github.com/elastio/bon
7use bon::Builder;
8use std::fmt::{Display, Formatter};
9use wait_utils::WaitStrategy;
10
11/// Create a new instance of the `ServiceStartConfig` struct using the builder.
12///
13/// The `program` is the name of the program to start. The `wait_strategy`
14/// is the wait strategy to use to wait for the service to start. The
15/// `env_var` is an optional environment variable to set when starting the
16/// service.
17///
18/// # Examples
19///
20/// Basic configuration using the derived builder:
21/// ```rust
22/// use service_utils::*;
23///
24/// let config = ServiceStartConfig::builder()
25///     .program("program")
26///     .wait_strategy(WaitStrategy::NoWait)
27///     .build();
28/// ```
29///
30/// Configuration with optional environment variables using the builder:
31///
32/// ```rust
33/// use service_utils::*;
34///
35/// let config = ServiceStartConfig::builder()
36///     .program("program")
37///     .program_args(vec!["arg1", "arg2"])
38///     .wait_strategy(WaitStrategy::NoWait)
39///     .env_vars(vec![("KEY".into(), "VALUE".into())])
40///     .build();
41/// ```
42///
43/// # Returns
44///
45/// Returns a new `ServiceStartConfig` instance.
46///
47#[derive(Builder, Debug, Default, Clone, Eq, PartialOrd, Ord, PartialEq, Hash)]
48pub struct ServiceStartConfig {
49    program: &'static str,
50    wait_strategy: WaitStrategy,
51    program_args: Option<Vec< &'static str,>>,
52    env_vars: Option<Vec<(String, String)>>,
53}
54
55impl ServiceStartConfig {
56    /// Create a new instance of the `ServiceStartConfig` struct using the constructor.
57    ///
58    /// The `program` is the name of the program to start. The `wait_strategy`
59    /// is the wait strategy to use to wait for the service to start. The
60    /// `env_var` is an optional environment variable to set when starting the
61    /// service.
62    ///
63    /// # Examples
64    ///
65    /// Basic configuration using the constructor:
66    ///
67    /// ```rust
68    /// use service_utils::*;
69    ///
70    /// let config = ServiceStartConfig::new("program", WaitStrategy::NoWait, None, None);
71    /// ```
72    ///
73    /// # Returns
74    ///
75    /// Returns a new `ServiceStartConfig` instance.
76    ///
77    pub fn new(
78        program: &'static str,
79        wait_strategy: WaitStrategy,
80        program_args: Option<Vec< &'static str,>>,
81        env_vars: Option<Vec<(String, String)>>,
82    ) -> Self {
83        Self {
84            program,
85            wait_strategy,
86            program_args,
87            env_vars,
88        }
89    }
90}
91
92impl ServiceStartConfig {
93    #[inline]
94    pub const fn program(&self) -> &'static str {
95        self.program
96    }
97
98
99
100    #[inline]
101    pub const fn wait_strategy(&self) -> &WaitStrategy {
102        &self.wait_strategy
103    }
104
105    #[inline]
106    pub const fn env_vars(&self) -> &Option<Vec<(String, String)>>
107    {
108        &self.env_vars
109    }
110    #[inline]
111    pub fn program_args(&self) -> &Option<Vec<&'static str>> {
112        &self.program_args
113    }
114}
115
116impl Display for ServiceStartConfig {
117    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
118        write!(
119            f,
120            "ServiceStartConfig {{ program: {}, wait_strategy: {}, env_vars: {:?} }}",
121            self.program, self.wait_strategy, self.env_vars
122        )
123    }
124}