Skip to main content

camel_api/
dynamic_router.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use crate::Exchange;
5
6pub type RouterExpression = Arc<dyn Fn(&Exchange) -> Option<String> + Send + Sync>;
7
8#[derive(Clone)]
9pub struct DynamicRouterConfig {
10    pub expression: RouterExpression,
11    pub uri_delimiter: String,
12    pub cache_size: i32,
13    pub ignore_invalid_endpoints: bool,
14    pub max_iterations: usize,
15    pub timeout: Option<Duration>,
16}
17
18impl DynamicRouterConfig {
19    pub fn new(expression: RouterExpression) -> Self {
20        Self {
21            expression,
22            uri_delimiter: ",".to_string(),
23            cache_size: 1000,
24            ignore_invalid_endpoints: false,
25            max_iterations: 1000,
26            timeout: Some(Duration::from_secs(60)),
27        }
28    }
29
30    pub fn uri_delimiter(mut self, d: impl Into<String>) -> Self {
31        self.uri_delimiter = d.into();
32        self
33    }
34
35    pub fn cache_size(mut self, n: i32) -> Self {
36        self.cache_size = n;
37        self
38    }
39
40    pub fn ignore_invalid_endpoints(mut self, ignore: bool) -> Self {
41        self.ignore_invalid_endpoints = ignore;
42        self
43    }
44
45    pub fn max_iterations(mut self, n: usize) -> Self {
46        self.max_iterations = n;
47        self
48    }
49
50    pub fn timeout(mut self, d: Duration) -> Self {
51        self.timeout = Some(d);
52        self
53    }
54
55    pub fn no_timeout(mut self) -> Self {
56        self.timeout = None;
57        self
58    }
59}
60
61impl std::fmt::Debug for DynamicRouterConfig {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        f.debug_struct("DynamicRouterConfig")
64            .field("uri_delimiter", &self.uri_delimiter)
65            .field("cache_size", &self.cache_size)
66            .field("ignore_invalid_endpoints", &self.ignore_invalid_endpoints)
67            .field("max_iterations", &self.max_iterations)
68            .field("timeout", &self.timeout)
69            .finish()
70    }
71}