1use std::sync::Arc;
2
3use camel_component_api::{CamelError, ComponentBundle, ComponentRegistrar};
4
5use crate::{MasterComponent, config::MasterComponentConfig};
6
7pub struct MasterBundle {
8 config: MasterComponentConfig,
9}
10
11impl ComponentBundle for MasterBundle {
12 fn config_key() -> &'static str {
13 "master"
14 }
15
16 fn from_toml(value: toml::Value) -> Result<Self, CamelError> {
17 let drain_timeout_ms = value
18 .get("drain_timeout_ms")
19 .and_then(|v| v.as_integer())
20 .unwrap_or(5000) as u64;
21 let delegate_retry_max_attempts = value
22 .get("delegate_retry_max_attempts")
23 .and_then(|v| v.as_integer())
24 .and_then(|v| u32::try_from(v).ok())
25 .map(|v| if v == 0 { None } else { Some(v) })
26 .unwrap_or(Some(30));
27
28 Ok(Self {
29 config: MasterComponentConfig {
30 drain_timeout_ms,
31 delegate_retry_max_attempts,
32 },
33 })
34 }
35
36 fn register_all(self, ctx: &mut dyn ComponentRegistrar) {
37 ctx.register_component_dyn(Arc::new(MasterComponent::new(self.config)));
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn from_toml_uses_defaults() {
47 let bundle = MasterBundle::from_toml(toml::Value::Table(toml::map::Map::new())).unwrap();
48 assert_eq!(bundle.config.drain_timeout_ms, 5000);
49 assert_eq!(bundle.config.delegate_retry_max_attempts, Some(30));
50 }
51
52 #[test]
53 fn from_toml_parses_delegate_retry_max_attempts() {
54 let mut table = toml::map::Map::new();
55 table.insert(
56 "delegate_retry_max_attempts".to_string(),
57 toml::Value::Integer(7),
58 );
59 let bundle = MasterBundle::from_toml(toml::Value::Table(table)).unwrap();
60 assert_eq!(bundle.config.delegate_retry_max_attempts, Some(7));
61 }
62
63 #[test]
64 fn from_toml_zero_means_unlimited_retries() {
65 let mut table = toml::map::Map::new();
66 table.insert(
67 "delegate_retry_max_attempts".to_string(),
68 toml::Value::Integer(0),
69 );
70 let bundle = MasterBundle::from_toml(toml::Value::Table(table)).unwrap();
71 assert_eq!(bundle.config.delegate_retry_max_attempts, None);
72 }
73}