1#![warn(missing_docs)]
2
3use core::time::Duration;
6
7use crate::{error::InvalidArg, ConfigError, Result};
8
9pub mod team;
10pub use team::*;
11
12pub const MAX_SYNC_INTERVAL: Duration = Duration::from_secs(365 * 24 * 60 * 60);
17
18#[derive(Clone, Debug)]
20pub struct SyncPeerConfig {
21 interval: Option<Duration>,
22 sync_now: bool,
23 #[cfg(feature = "preview")]
24 sync_on_hello: bool,
25}
26
27impl SyncPeerConfig {
28 pub fn builder() -> SyncPeerConfigBuilder {
30 Default::default()
31 }
32}
33
34impl From<SyncPeerConfig> for aranya_daemon_api::SyncPeerConfig {
35 fn from(value: SyncPeerConfig) -> Self {
36 Self {
37 interval: value.interval,
38 sync_now: value.sync_now,
39 #[cfg(feature = "preview")]
40 sync_on_hello: value.sync_on_hello,
41 }
42 }
43}
44
45#[derive(Debug)]
47pub struct SyncPeerConfigBuilder {
48 interval: Option<Duration>,
49 sync_now: bool,
50 #[cfg(feature = "preview")]
51 sync_on_hello: bool,
52}
53
54impl SyncPeerConfigBuilder {
55 pub fn new() -> Self {
57 Default::default()
58 }
59
60 pub fn build(self) -> Result<SyncPeerConfig> {
62 if let Some(interval) = self.interval {
65 if interval > MAX_SYNC_INTERVAL {
66 return Err(ConfigError::InvalidArg(InvalidArg::new(
67 "duration",
68 "must not exceed 1 year to prevent overflow",
69 ))
70 .into());
71 }
72 }
73
74 Ok(SyncPeerConfig {
75 interval: self.interval,
76 sync_now: self.sync_now,
77 #[cfg(feature = "preview")]
78 sync_on_hello: self.sync_on_hello,
79 })
80 }
81
82 pub fn interval(mut self, duration: Duration) -> Self {
88 self.interval = Some(duration);
89 self
90 }
91
92 pub fn sync_now(mut self, sync_now: bool) -> Self {
96 self.sync_now = sync_now;
97 self
98 }
99
100 #[cfg(feature = "preview")]
105 #[cfg_attr(docsrs, doc(cfg(feature = "preview")))]
106 pub fn sync_on_hello(mut self, sync_on_hello: bool) -> Self {
107 self.sync_on_hello = sync_on_hello;
108 self
109 }
110}
111
112impl Default for SyncPeerConfigBuilder {
113 fn default() -> Self {
114 Self {
115 interval: None,
116 sync_now: true,
117 #[cfg(feature = "preview")]
118 sync_on_hello: false,
119 }
120 }
121}
122
123#[cfg(feature = "preview")]
129#[cfg_attr(docsrs, doc(cfg(feature = "preview")))]
130#[derive(Clone, Debug)]
131pub struct HelloSubscriptionConfig {
132 graph_change_debounce: Duration,
133 expiration: Duration,
134 periodic_interval: Duration,
135}
136
137#[cfg(feature = "preview")]
138impl HelloSubscriptionConfig {
139 pub fn builder() -> HelloSubscriptionConfigBuilder {
141 Default::default()
142 }
143
144 pub fn graph_change_debounce(&self) -> Duration {
149 self.graph_change_debounce
150 }
151
152 pub fn expiration(&self) -> Duration {
154 self.expiration
155 }
156
157 pub fn periodic_interval(&self) -> Duration {
159 self.periodic_interval
160 }
161}
162
163#[cfg(feature = "preview")]
164impl Default for HelloSubscriptionConfig {
165 fn default() -> Self {
166 Self {
167 graph_change_debounce: Duration::from_millis(100),
168 expiration: MAX_SYNC_INTERVAL,
169 periodic_interval: Duration::from_secs(10),
170 }
171 }
172}
173
174#[cfg(feature = "preview")]
176#[cfg_attr(docsrs, doc(cfg(feature = "preview")))]
177#[derive(Debug)]
178pub struct HelloSubscriptionConfigBuilder {
179 graph_change_debounce: Duration,
180 expiration: Duration,
181 periodic_interval: Duration,
182}
183
184#[cfg(feature = "preview")]
185impl HelloSubscriptionConfigBuilder {
186 pub fn new() -> Self {
188 Default::default()
189 }
190
191 pub fn build(self) -> Result<HelloSubscriptionConfig> {
193 Ok(HelloSubscriptionConfig {
194 graph_change_debounce: self.graph_change_debounce,
195 expiration: self.expiration,
196 periodic_interval: self.periodic_interval,
197 })
198 }
199
200 pub fn graph_change_debounce(mut self, duration: Duration) -> Self {
205 self.graph_change_debounce = duration;
206 self
207 }
208
209 pub fn expiration(mut self, duration: Duration) -> Self {
214 self.expiration = duration;
215 self
216 }
217
218 pub fn periodic_interval(mut self, interval: Duration) -> Self {
222 self.periodic_interval = interval;
223 self
224 }
225}
226
227#[cfg(feature = "preview")]
228impl Default for HelloSubscriptionConfigBuilder {
229 fn default() -> Self {
230 let config = HelloSubscriptionConfig::default();
231 Self {
232 graph_change_debounce: config.graph_change_debounce,
233 expiration: config.expiration,
234 periodic_interval: config.periodic_interval,
235 }
236 }
237}