datex_core/runtime/
config.rs1use crate::{
2 collections::HashMap,
3 datex_proxy::{
4 DatexValueProxyInfallibleSerialize, DatexValueProxySerialize,
5 },
6 network::com_hub::InterfacePriority,
7 prelude::*,
8 values::{core_values::endpoint::Endpoint, value::Value},
9};
10use datex_macros_internal::Datex;
11
12pub fn is_priority_none(v: &InterfacePriority) -> bool {
13 matches!(v, InterfacePriority::None)
14}
15
16#[derive(Datex, Debug, Clone, PartialEq, Eq)]
17pub struct RuntimeConfigInterface {
19 #[datex(rename = "type")]
20 pub interface_type: String,
21 pub config: Value,
22 pub priority: InterfacePriority,
23}
24
25impl RuntimeConfigInterface {
26 pub fn new<T: DatexValueProxySerialize>(
27 interface_type: &str,
28 setup_data: T,
29 ) -> Result<RuntimeConfigInterface, String> {
30 Ok(RuntimeConfigInterface {
31 interface_type: interface_type.to_string(),
32 priority: InterfacePriority::default(),
33 config: setup_data.try_to_value().map_err(|e| {
34 format!(
35 "Failed to convert setup_data to ValueContainer: {:?}",
36 e
37 )
38 })?,
39 })
40 }
41
42 pub fn new_from_map(
43 interface_type: &str,
44 config: Value,
45 ) -> RuntimeConfigInterface {
46 RuntimeConfigInterface {
47 priority: InterfacePriority::default(),
48 interface_type: interface_type.to_string(),
49 config,
50 }
51 }
52}
53
54#[derive(Datex, Debug, Default, Clone)]
55pub struct RuntimeConfig {
56 pub endpoint: Endpoint,
57 pub interfaces: Option<Vec<RuntimeConfigInterface>>,
58 pub env: Option<HashMap<String, String>>,
59}
60
61impl RuntimeConfig {
62 pub fn new_with_endpoint(endpoint: Endpoint) -> Self {
63 RuntimeConfig {
64 endpoint,
65 interfaces: None,
66 env: None,
67 }
68 }
69
70 pub fn add_interface<T: DatexValueProxyInfallibleSerialize>(
71 &mut self,
72 interface_type: String,
73 config: T,
74 priority: InterfacePriority,
75 ) {
76 let config = config.to_value();
77 let interface = RuntimeConfigInterface {
78 interface_type,
79 config,
80 priority,
81 };
82 if let Some(interfaces) = &mut self.interfaces {
83 interfaces.push(interface);
84 } else {
85 self.interfaces = Some(vec![interface]);
86 }
87 }
88
89 pub fn add_env_var(&mut self, key: String, value: String) {
91 self.env.get_or_insert_with(HashMap::new).insert(key, value);
92 }
93
94 pub fn add_env_vars(&mut self, vars: HashMap<String, String>) {
96 self.env.get_or_insert_with(HashMap::new).extend(vars);
97 }
98
99 #[cfg(feature = "target_native")]
100 pub fn load_host_env_vars(&mut self) {
102 for (key, value) in std::env::vars() {
104 self.env.get_or_insert_with(HashMap::new).insert(key, value);
105 }
106 }
107
108 #[cfg(feature = "target_native")]
109 pub fn add_env_vars_from_file(
111 &mut self,
112 path: &std::path::PathBuf,
113 ) -> Result<(), dotenvy::Error> {
114 let loader1 = dotenvy::from_path_iter(path)?;
115 for item in loader1 {
116 let (key, val) = item?;
117 self.env.get_or_insert_with(HashMap::new).insert(key, val);
118 }
119 Ok(())
120 }
121}
122
123#[cfg(test)]
124pub mod tests {
125 use datex_macros_internal::Datex;
126
127 use crate::{
128 datex_proxy::{
129 DatexValueContainerProxyDeserialize,
130 DatexValueContainerProxyInfallibleSerialize,
131 },
132 prelude::*,
133 runtime::{RuntimeConfig, RuntimeConfigInterface},
134 values::core_values::{endpoint::Endpoint, map::Map},
135 };
136
137 #[derive(Datex)]
138 struct MySetupData {
139 field1: String,
140 field2: i32,
141 }
142
143 #[test]
144 fn add_env_var() {
145 let mut config = RuntimeConfig::default();
146 config.add_env_var("KEY1".to_string(), "VALUE1".to_string());
147 let env_vars = config.env.unwrap();
148 assert_eq!(env_vars.get("KEY1"), Some(&"VALUE1".to_string()));
149 }
150
151 #[test]
152 fn datex_proxy_runtime_config_interface() {
153 let config_interface = RuntimeConfigInterface::new(
154 "test",
155 MySetupData {
156 field1: "value".to_string(),
157 field2: 42,
158 },
159 )
160 .unwrap();
161 assert_eq!(config_interface.interface_type, "test");
162 let setup_data = config_interface.config.clone();
163 let map: Map = setup_data.try_into_value().unwrap();
164 assert_eq!(
165 map.get("field1")
166 .unwrap()
167 .clone()
168 .try_into_value::<String>()
169 .unwrap(),
170 "value".to_string()
171 );
172 assert_eq!(
173 map.get("field2")
174 .unwrap()
175 .clone()
176 .try_into_value::<i32>()
177 .unwrap(),
178 42
179 );
180
181 let value_container = config_interface.to_value_container();
182 let parsed_config_interface: RuntimeConfigInterface =
183 value_container.try_into().unwrap();
184 assert_eq!(parsed_config_interface.interface_type, "test");
185 }
186
187 #[test]
188 fn datex_proxy_runtime_config() {
189 let config = RuntimeConfig::new_with_endpoint(Endpoint::new("@test"));
190 let value_container = config.to_value_container();
191 let parsed_config: RuntimeConfig =
192 RuntimeConfig::try_from_value_container(value_container).unwrap();
193 assert_eq!(parsed_config.endpoint, Endpoint::new("@test"));
194 }
195}