Skip to main content

camel_api/
recipient_list.rs

1use std::sync::Arc;
2
3use crate::Exchange;
4
5pub type RecipientListExpression = Arc<dyn Fn(&Exchange) -> String + Send + Sync>;
6
7#[derive(Clone)]
8pub struct RecipientListConfig {
9    pub expression: RecipientListExpression,
10    pub delimiter: String,
11    pub parallel: bool,
12    pub parallel_limit: Option<usize>,
13    pub stop_on_exception: bool,
14    pub strategy: crate::MulticastStrategy,
15}
16
17impl RecipientListConfig {
18    pub fn new(expression: RecipientListExpression) -> Self {
19        Self {
20            expression,
21            delimiter: ",".to_string(),
22            parallel: false,
23            parallel_limit: None,
24            stop_on_exception: false,
25            strategy: crate::MulticastStrategy::default(),
26        }
27    }
28
29    pub fn delimiter(mut self, d: impl Into<String>) -> Self {
30        self.delimiter = d.into();
31        self
32    }
33
34    pub fn parallel(mut self, parallel: bool) -> Self {
35        self.parallel = parallel;
36        self
37    }
38
39    pub fn parallel_limit(mut self, limit: usize) -> Self {
40        self.parallel_limit = Some(limit);
41        self
42    }
43
44    pub fn stop_on_exception(mut self, stop: bool) -> Self {
45        self.stop_on_exception = stop;
46        self
47    }
48
49    pub fn strategy(mut self, strategy: crate::MulticastStrategy) -> Self {
50        self.strategy = strategy;
51        self
52    }
53}
54
55impl std::fmt::Debug for RecipientListConfig {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        f.debug_struct("RecipientListConfig")
58            .field("delimiter", &self.delimiter)
59            .field("parallel", &self.parallel)
60            .field("parallel_limit", &self.parallel_limit)
61            .field("stop_on_exception", &self.stop_on_exception)
62            .finish()
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use std::sync::Arc;
69
70    use super::*;
71
72    fn noop_expr() -> RecipientListExpression {
73        Arc::new(|_| String::new())
74    }
75
76    #[test]
77    fn new_has_defaults() {
78        let cfg = RecipientListConfig::new(noop_expr());
79        assert_eq!(cfg.delimiter, ",");
80        assert!(!cfg.parallel);
81        assert!(cfg.parallel_limit.is_none());
82        assert!(!cfg.stop_on_exception);
83    }
84
85    #[test]
86    fn builder_chaining() {
87        let cfg = RecipientListConfig::new(noop_expr())
88            .delimiter(";")
89            .parallel(true)
90            .parallel_limit(4)
91            .stop_on_exception(true)
92            .strategy(crate::MulticastStrategy::CollectAll);
93        assert_eq!(cfg.delimiter, ";");
94        assert!(cfg.parallel);
95        assert_eq!(cfg.parallel_limit, Some(4));
96        assert!(cfg.stop_on_exception);
97    }
98
99    #[test]
100    fn clone_preserves_values() {
101        let cfg = RecipientListConfig::new(noop_expr())
102            .delimiter("|")
103            .parallel(true);
104        let cloned = cfg.clone();
105        assert_eq!(cloned.delimiter, "|");
106        assert!(cloned.parallel);
107    }
108
109    #[test]
110    fn debug_format() {
111        let cfg = RecipientListConfig::new(noop_expr());
112        let debug = format!("{cfg:?}");
113        assert!(debug.contains("RecipientListConfig"));
114        assert!(debug.contains("delimiter"));
115    }
116}