yozefu_app/configuration/
yozefu_config.rs

1//! module defining the configuration of the yozefu application
2
3use super::{Configuration, SchemaRegistryConfig};
4use crate::{APPLICATION_NAME, configuration::ClusterConfig};
5use std::{collections::HashMap, path::PathBuf};
6
7/// composed of kafka properties and
8/// an optional user-specific configuration.
9#[derive(Debug, Clone)]
10pub struct YozefuConfig {
11    cluster: String,
12    cluster_config: ClusterConfig,
13    pub log_file: Option<PathBuf>,
14    pub export_directory: Option<PathBuf>,
15}
16
17impl YozefuConfig {
18    pub(super) fn new(cluster: &str, cluster_config: ClusterConfig) -> Self {
19        Self {
20            cluster: cluster.to_string(),
21            cluster_config,
22            log_file: None,
23            export_directory: None,
24        }
25    }
26
27    pub fn cluster(&self) -> &str {
28        &self.cluster
29    }
30
31    pub fn url_template(&self) -> Option<String> {
32        self.cluster_config.url_template.clone()
33    }
34
35    pub fn config(&self) -> &ClusterConfig {
36        &self.cluster_config
37    }
38
39    pub fn schema_registry(&self) -> Option<SchemaRegistryConfig> {
40        self.cluster_config.schema_registry.clone()
41    }
42
43    pub fn with_exported_directory(self, exported_directory: PathBuf) -> Self {
44        Self {
45            cluster: self.cluster,
46            cluster_config: self.cluster_config,
47            log_file: self.log_file,
48            export_directory: Some(exported_directory),
49        }
50    }
51
52    pub fn with_logs_file(self, logs_file: PathBuf) -> Self {
53        Self {
54            cluster: self.cluster,
55            cluster_config: self.cluster_config,
56            log_file: Some(logs_file),
57            export_directory: self.export_directory,
58        }
59    }
60
61    pub fn set_kafka_property(&mut self, key: &str, value: &str) {
62        self.cluster_config.set_kafka_property(key, value);
63    }
64
65    /// Overrides the kafka properties with the properties provided by the user
66    pub fn update_kafka_properties(self, kafka_properties: HashMap<String, String>) -> Self {
67        Self {
68            cluster: self.cluster,
69            cluster_config: self.cluster_config.with_kafka_properties(kafka_properties),
70            log_file: self.log_file,
71            export_directory: self.export_directory,
72        }
73    }
74}
75
76impl Configuration for YozefuConfig {
77    /// Returns the kafka properties
78    fn kafka_config_map(&self) -> HashMap<String, String> {
79        let mut config_map = self.cluster_config.kafka_config_map();
80
81        // Default properties
82        for (key, value) in [
83            ("group.id", APPLICATION_NAME),
84            ("enable.auto.commit", "false"),
85        ] {
86            if !config_map.contains_key(key) {
87                config_map.insert(key.into(), value.into());
88            }
89        }
90        config_map
91    }
92}