Skip to main content

axonflow_sdk_rust/
config.rs

1use std::fmt;
2use std::time::Duration;
3
4#[derive(Default, Debug, Clone, PartialEq, Eq)]
5pub enum Mode {
6    #[default]
7    Production,
8    Sandbox,
9}
10
11#[derive(Debug, Clone)]
12pub struct RetryConfig {
13    /// Whether retries are enabled. Note: even when `true`, mutations
14    /// (`execute-plan`, `generate-plan`, `cancel-plan`, `update-plan`) are
15    /// never retried to avoid double-execution.
16    pub enabled: bool,
17    pub max_attempts: u32,
18    pub initial_delay: Duration,
19}
20
21impl Default for RetryConfig {
22    fn default() -> Self {
23        Self {
24            enabled: true,
25            max_attempts: 3,
26            initial_delay: Duration::from_secs(1),
27        }
28    }
29}
30
31#[derive(Debug, Clone)]
32pub struct CacheConfig {
33    pub enabled: bool,
34    pub ttl: Duration,
35    /// Maximum number of entries in the cache. When the cache reaches this
36    /// size, the least recently used entry is evicted. Defaults to 10,000.
37    pub max_capacity: u64,
38}
39
40impl Default for CacheConfig {
41    fn default() -> Self {
42        Self {
43            enabled: true,
44            ttl: Duration::from_secs(60),
45            max_capacity: 10_000,
46        }
47    }
48}
49
50#[derive(Clone)]
51pub struct AxonFlowConfig {
52    pub endpoint: String,
53    pub client_id: Option<String>,
54    pub client_secret: Option<String>,
55    pub license_key: Option<String>,
56    pub mode: Mode,
57    pub debug: bool,
58    pub timeout: Duration,
59    pub map_timeout: Duration,
60    pub retry: RetryConfig,
61    pub cache: CacheConfig,
62    pub insecure_skip_tls_verify: bool,
63}
64
65impl fmt::Debug for AxonFlowConfig {
66    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67        f.debug_struct("AxonFlowConfig")
68            .field("endpoint", &self.endpoint)
69            .field("client_id", &self.client_id)
70            .field(
71                "client_secret",
72                &self.client_secret.as_ref().map(|_| "[REDACTED]"),
73            )
74            .field(
75                "license_key",
76                &self.license_key.as_ref().map(|_| "[REDACTED]"),
77            )
78            .field("mode", &self.mode)
79            .field("debug", &self.debug)
80            .field("timeout", &self.timeout)
81            .field("map_timeout", &self.map_timeout)
82            .field("retry", &self.retry)
83            .field("cache", &self.cache)
84            .field("insecure_skip_tls_verify", &self.insecure_skip_tls_verify)
85            .finish()
86    }
87}
88
89impl Default for AxonFlowConfig {
90    fn default() -> Self {
91        Self {
92            endpoint: String::new(),
93            client_id: None,
94            client_secret: None,
95            license_key: None,
96            mode: Mode::default(),
97            debug: false,
98            timeout: Duration::from_secs(60),
99            map_timeout: Duration::from_secs(120),
100            retry: RetryConfig::default(),
101            cache: CacheConfig::default(),
102            insecure_skip_tls_verify: false,
103        }
104    }
105}
106
107impl AxonFlowConfig {
108    pub fn new(endpoint: impl Into<String>) -> Self {
109        Self {
110            endpoint: endpoint.into(),
111            ..Default::default()
112        }
113    }
114
115    /// Convenience constructor for local development. Defaults to
116    /// `http://localhost:8080` (the docker-compose agent default), sets
117    /// `mode = Mode::Sandbox`, and enables debug logging.
118    ///
119    /// Sandbox-mode clients fire anonymous telemetry tagged `stream="sandbox"`
120    /// — see `heartbeat::maybe_send_heartbeat`. Set `AXONFLOW_TELEMETRY=off`
121    /// to opt out (the SOLE opt-out lever; there is intentionally no
122    /// programmatic disable on the SDK config).
123    pub fn sandbox(client_id: impl Into<String>, client_secret: impl Into<String>) -> Self {
124        Self {
125            endpoint: "http://localhost:8080".to_string(),
126            client_id: Some(client_id.into()),
127            client_secret: Some(client_secret.into()),
128            mode: Mode::Sandbox,
129            debug: true,
130            ..Default::default()
131        }
132    }
133
134    pub fn with_auth(
135        mut self,
136        client_id: impl Into<String>,
137        client_secret: impl Into<String>,
138    ) -> Self {
139        self.client_id = Some(client_id.into());
140        self.client_secret = Some(client_secret.into());
141        self
142    }
143
144    pub fn with_license_key(mut self, license_key: impl Into<String>) -> Self {
145        self.license_key = Some(license_key.into());
146        self
147    }
148
149    pub fn with_mode(mut self, mode: Mode) -> Self {
150        self.mode = mode;
151        self
152    }
153
154    pub fn with_timeout(mut self, timeout: Duration) -> Self {
155        self.timeout = timeout;
156        self
157    }
158
159    pub fn with_map_timeout(mut self, timeout: Duration) -> Self {
160        self.map_timeout = timeout;
161        self
162    }
163
164    pub fn with_retry(mut self, retry: RetryConfig) -> Self {
165        self.retry = retry;
166        self
167    }
168
169    pub fn with_cache(mut self, cache: CacheConfig) -> Self {
170        self.cache = cache;
171        self
172    }
173}