Skip to main content

camel_api/
routing_slip.rs

1use std::sync::Arc;
2
3use crate::Exchange;
4
5pub type RoutingSlipExpression = Arc<dyn Fn(&Exchange) -> Option<String> + Send + Sync>;
6
7#[derive(Clone)]
8pub struct RoutingSlipConfig {
9    pub expression: RoutingSlipExpression,
10    pub uri_delimiter: String,
11    pub cache_size: i32,
12    pub ignore_invalid_endpoints: bool,
13}
14
15impl RoutingSlipConfig {
16    pub fn new(expression: RoutingSlipExpression) -> Self {
17        Self {
18            expression,
19            uri_delimiter: ",".to_string(),
20            cache_size: 1000,
21            ignore_invalid_endpoints: false,
22        }
23    }
24
25    pub fn uri_delimiter(mut self, d: impl Into<String>) -> Self {
26        self.uri_delimiter = d.into();
27        self
28    }
29
30    pub fn cache_size(mut self, n: i32) -> Self {
31        self.cache_size = n;
32        self
33    }
34
35    pub fn ignore_invalid_endpoints(mut self, ignore: bool) -> Self {
36        self.ignore_invalid_endpoints = ignore;
37        self
38    }
39}
40
41impl std::fmt::Debug for RoutingSlipConfig {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        f.debug_struct("RoutingSlipConfig")
44            .field("uri_delimiter", &self.uri_delimiter)
45            .field("cache_size", &self.cache_size)
46            .field("ignore_invalid_endpoints", &self.ignore_invalid_endpoints)
47            .finish()
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use std::sync::Arc;
54
55    use super::*;
56
57    fn noop_expr() -> RoutingSlipExpression {
58        Arc::new(|_| None)
59    }
60
61    #[test]
62    fn new_has_defaults() {
63        let cfg = RoutingSlipConfig::new(noop_expr());
64        assert_eq!(cfg.uri_delimiter, ",");
65        assert_eq!(cfg.cache_size, 1000);
66        assert!(!cfg.ignore_invalid_endpoints);
67    }
68
69    #[test]
70    fn builder_chaining() {
71        let cfg = RoutingSlipConfig::new(noop_expr())
72            .uri_delimiter("|")
73            .cache_size(50)
74            .ignore_invalid_endpoints(true);
75        assert_eq!(cfg.uri_delimiter, "|");
76        assert_eq!(cfg.cache_size, 50);
77        assert!(cfg.ignore_invalid_endpoints);
78    }
79
80    #[test]
81    fn clone_preserves_values() {
82        let cfg = RoutingSlipConfig::new(noop_expr()).uri_delimiter(";");
83        let cloned = cfg.clone();
84        assert_eq!(cloned.uri_delimiter, ";");
85    }
86
87    #[test]
88    fn debug_format() {
89        let cfg = RoutingSlipConfig::new(noop_expr());
90        let debug = format!("{cfg:?}");
91        assert!(debug.contains("RoutingSlipConfig"));
92        assert!(debug.contains("uri_delimiter"));
93    }
94}