camel_api/
dynamic_router.rs1use std::time::Duration;
2
3use crate::filter::TargetSource;
4
5#[derive(Clone)]
6pub struct DynamicRouterConfig {
7 pub expression: TargetSource,
8 pub uri_delimiter: String,
9 pub cache_size: i32,
10 pub ignore_invalid_endpoints: bool,
11 pub max_iterations: usize,
12 pub timeout: Option<Duration>,
13}
14
15impl DynamicRouterConfig {
16 pub fn new(expression: impl Into<TargetSource>) -> Self {
17 Self {
18 expression: expression.into(),
19 uri_delimiter: ",".to_string(),
20 cache_size: 1000,
21 ignore_invalid_endpoints: false,
22 max_iterations: 1000,
23 timeout: Some(Duration::from_secs(60)),
24 }
25 }
26
27 pub fn uri_delimiter(mut self, d: impl Into<String>) -> Self {
28 self.uri_delimiter = d.into();
29 self
30 }
31
32 pub fn cache_size(mut self, n: i32) -> Self {
33 self.cache_size = n;
34 self
35 }
36
37 pub fn ignore_invalid_endpoints(mut self, ignore: bool) -> Self {
38 self.ignore_invalid_endpoints = ignore;
39 self
40 }
41
42 pub fn max_iterations(mut self, n: usize) -> Self {
43 self.max_iterations = n;
44 self
45 }
46
47 pub fn timeout(mut self, d: Duration) -> Self {
48 self.timeout = Some(d);
49 self
50 }
51
52 pub fn no_timeout(mut self) -> Self {
53 self.timeout = None;
54 self
55 }
56}
57
58impl std::fmt::Debug for DynamicRouterConfig {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 f.debug_struct("DynamicRouterConfig")
61 .field("uri_delimiter", &self.uri_delimiter)
62 .field("cache_size", &self.cache_size)
63 .field("ignore_invalid_endpoints", &self.ignore_invalid_endpoints)
64 .field("max_iterations", &self.max_iterations)
65 .field("timeout", &self.timeout)
66 .finish()
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use std::sync::Arc;
73 use std::time::Duration;
74
75 use super::*;
76
77 fn noop_expr() -> TargetSource {
78 TargetSource::Sync(Arc::new(|_| None))
79 }
80
81 #[test]
82 fn new_has_defaults() {
83 let cfg = DynamicRouterConfig::new(noop_expr());
84 assert_eq!(cfg.uri_delimiter, ",");
85 assert_eq!(cfg.cache_size, 1000);
86 assert!(!cfg.ignore_invalid_endpoints);
87 assert_eq!(cfg.max_iterations, 1000);
88 assert_eq!(cfg.timeout, Some(Duration::from_secs(60)));
89 }
90
91 #[test]
92 fn builder_chaining() {
93 let cfg = DynamicRouterConfig::new(noop_expr())
94 .uri_delimiter(";")
95 .cache_size(100)
96 .ignore_invalid_endpoints(true)
97 .max_iterations(50)
98 .timeout(Duration::from_secs(10));
99 assert_eq!(cfg.uri_delimiter, ";");
100 assert_eq!(cfg.cache_size, 100);
101 assert!(cfg.ignore_invalid_endpoints);
102 assert_eq!(cfg.max_iterations, 50);
103 assert_eq!(cfg.timeout, Some(Duration::from_secs(10)));
104 }
105
106 #[test]
107 fn no_timeout_clears() {
108 let cfg = DynamicRouterConfig::new(noop_expr()).no_timeout();
109 assert!(cfg.timeout.is_none());
110 }
111
112 #[test]
113 fn debug_format_contains_fields() {
114 let cfg = DynamicRouterConfig::new(noop_expr());
115 let debug = format!("{cfg:?}");
116 assert!(debug.contains("DynamicRouterConfig"));
117 assert!(debug.contains("uri_delimiter"));
118 assert!(debug.contains("cache_size"));
119 }
120
121 #[test]
122 fn clone_preserves_values() {
123 let cfg = DynamicRouterConfig::new(noop_expr())
124 .uri_delimiter("|")
125 .cache_size(42);
126 let cloned = cfg.clone();
127 assert_eq!(cloned.uri_delimiter, "|");
128 assert_eq!(cloned.cache_size, 42);
129 }
130}