nullnet_libdatastore/
config.rs

1/// Configuration structure for the datastore.
2///
3/// This struct encapsulates the configuration details required to connect to the datastore,
4/// including the host address, port, and whether TLS is enabled.
5#[derive(Debug, Clone)]
6pub struct DatastoreConfig {
7    /// Hostname or IP address of the datastore.
8    pub host: String,
9    /// Port number used to connect to the datastore.
10    pub port: u16,
11    /// Whether TLS is enabled for the datastore connection.
12    pub tls: bool,
13}
14
15impl DatastoreConfig {
16    /// Creates a new `DatastoreConfig` instance by reading values from environment variables.
17    ///
18    /// If the environment variables are not set or cannot be parsed, default values are used:
19    /// - `DATASTORE_HOST` defaults to `"127.0.0.1"`.
20    /// - `DATASTORE_PORT` defaults to `6000`.
21    /// - `DATASTORE_TLS` defaults to `false`.
22    ///
23    /// # Returns
24    /// A `DatastoreConfig` instance with values derived from the environment or defaults.
25    #[must_use]
26    pub fn from_env() -> Self {
27        Self {
28            host: read_host_value_from_env(String::from("127.0.0.1")),
29            port: read_port_value_from_env(6000),
30            tls: real_tls_value_from_env(false),
31        }
32    }
33
34    /// Creates a new `DatastoreConfig` instance with the provided values.
35    ///
36    /// # Arguments
37    /// * `host` - A `String` representing the hostname or IP address of the datastore.
38    /// * `port` - A `u16` representing the port number for connecting to the datastore.
39    /// * `tls` - A `bool` indicating whether to use TLS for the connection (`true` for TLS, `false` otherwise).
40    ///
41    /// # Returns
42    /// A `DatastoreConfig` instance initialized with the specified host, port, and TLS settings.
43    #[must_use]
44    pub fn new(host: String, port: u16, tls: bool) -> Self {
45        Self { host, port, tls }
46    }
47}
48
49/// Reads the `DATASTORE_HOST` environment variable.
50///
51/// If the variable is not set or an error occurs, the specified default value is returned.
52///
53/// # Parameters
54/// - `default`: Default hostname to use if the environment variable is not set.
55///
56/// # Returns
57/// The value of `DATASTORE_HOST` or the default value if the variable is not set.
58fn read_host_value_from_env(default: String) -> String {
59    std::env::var("DATASTORE_HOST").unwrap_or_else(|err| {
60        eprintln!("Failed to read 'DATASTORE_HOST' env var: {err}. Using default value ...");
61        default
62    })
63}
64
65/// Reads the `DATASTORE_PORT` environment variable and parses it as a `u16`.
66///
67/// If the variable is not set, cannot be parsed, or an error occurs, the specified default value is used.
68///
69/// # Parameters
70/// - `default`: Default port to use if the environment variable is not set or invalid.
71///
72/// # Returns
73/// The value of `DATASTORE_PORT` parsed as a `u16` or the default value if the variable is not set or invalid.
74fn read_port_value_from_env(default: u16) -> u16 {
75    match std::env::var("DATASTORE_PORT") {
76        Ok(value) => value.parse::<u16>().unwrap_or_else(|err| {
77            eprintln!(
78                "Failed to parse 'DATASTORE_PORT' ({value}) as u16: {err}. Using default value ..."
79            );
80            default
81        }),
82        Err(err) => {
83            eprintln!("Failed to read 'DATASTORE_PORT' env var: {err}. Using default value ...");
84            default
85        }
86    }
87}
88
89/// Reads the `DATASTORE_TLS` environment variable and interprets it as a boolean.
90///
91/// If the variable is set to `"true"` (case insensitive), it returns `true`. For any other value or
92/// if the variable is not set, the specified default value is used.
93///
94/// # Parameters
95/// - `default`: Default value to use if the environment variable is not set.
96///
97/// # Returns
98/// `true` if the environment variable is set to `"true"`, otherwise the default value.
99fn real_tls_value_from_env(default: bool) -> bool {
100    match std::env::var("DATASTORE_TLS") {
101        Ok(value) => value.to_lowercase() == "true",
102        Err(err) => {
103            eprintln!("Failed to read 'DATASTORE_TLS' env var: {err}. Using default value ...");
104            default
105        }
106    }
107}