nullnet_libdatastore/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/// Configuration structure for the datastore.
///
/// This struct encapsulates the configuration details required to connect to the datastore,
/// including the host address, port, and whether TLS is enabled.
#[derive(Debug, Clone)]
pub struct DatastoreConfig {
    /// Hostname or IP address of the datastore.
    pub host: String,
    /// Port number used to connect to the datastore.
    pub port: u16,
    /// Whether TLS is enabled for the datastore connection.
    pub tls: bool,
}

impl DatastoreConfig {
    /// Creates a new `DatastoreConfig` instance by reading values from environment variables.
    ///
    /// If the environment variables are not set or cannot be parsed, default values are used:
    /// - `DATASTORE_HOST` defaults to `"127.0.0.1"`.
    /// - `DATASTORE_PORT` defaults to `6000`.
    /// - `DATASTORE_TLS` defaults to `false`.
    ///
    /// # Returns
    /// A `DatastoreConfig` instance with values derived from the environment or defaults.
    #[must_use]
    pub fn from_env() -> Self {
        Self {
            host: read_host_value_from_env(String::from("127.0.0.1")),
            port: read_port_value_from_env(6000),
            tls: real_tls_value_from_env(false),
        }
    }

    /// Creates a new `DatastoreConfig` instance with the provided values.
    ///
    /// # Arguments
    /// * `host` - A `String` representing the hostname or IP address of the datastore.
    /// * `port` - A `u16` representing the port number for connecting to the datastore.
    /// * `tls` - A `bool` indicating whether to use TLS for the connection (`true` for TLS, `false` otherwise).
    ///
    /// # Returns
    /// A `DatastoreConfig` instance initialized with the specified host, port, and TLS settings.
    #[must_use]
    pub fn new(host: String, port: u16, tls: bool) -> Self {
        Self { host, port, tls }
    }
}

/// Reads the `DATASTORE_HOST` environment variable.
///
/// If the variable is not set or an error occurs, the specified default value is returned.
///
/// # Parameters
/// - `default`: Default hostname to use if the environment variable is not set.
///
/// # Returns
/// The value of `DATASTORE_HOST` or the default value if the variable is not set.
fn read_host_value_from_env(default: String) -> String {
    std::env::var("DATASTORE_HOST").unwrap_or_else(|err| {
        eprintln!("Failed to read 'DATASTORE_HOST' env var: {err}. Using default value ...");
        default
    })
}

/// Reads the `DATASTORE_PORT` environment variable and parses it as a `u16`.
///
/// If the variable is not set, cannot be parsed, or an error occurs, the specified default value is used.
///
/// # Parameters
/// - `default`: Default port to use if the environment variable is not set or invalid.
///
/// # Returns
/// The value of `DATASTORE_PORT` parsed as a `u16` or the default value if the variable is not set or invalid.
fn read_port_value_from_env(default: u16) -> u16 {
    match std::env::var("DATASTORE_PORT") {
        Ok(value) => value.parse::<u16>().unwrap_or_else(|err| {
            eprintln!(
                "Failed to parse 'DATASTORE_PORT' ({value}) as u16: {err}. Using default value ..."
            );
            default
        }),
        Err(err) => {
            eprintln!("Failed to read 'DATASTORE_PORT' env var: {err}. Using default value ...");
            default
        }
    }
}

/// Reads the `DATASTORE_TLS` environment variable and interprets it as a boolean.
///
/// If the variable is set to `"true"` (case insensitive), it returns `true`. For any other value or
/// if the variable is not set, the specified default value is used.
///
/// # Parameters
/// - `default`: Default value to use if the environment variable is not set.
///
/// # Returns
/// `true` if the environment variable is set to `"true"`, otherwise the default value.
fn real_tls_value_from_env(default: bool) -> bool {
    match std::env::var("DATASTORE_TLS") {
        Ok(value) => value.to_lowercase() == "true",
        Err(err) => {
            eprintln!("Failed to read 'DATASTORE_TLS' env var: {err}. Using default value ...");
            default
        }
    }
}