#![warn(missing_docs)]
use core::time::Duration;
use crate::{error::InvalidArg, ConfigError, Result};
pub mod team;
pub use team::*;
pub const MAX_SYNC_INTERVAL: Duration = Duration::from_secs(365 * 24 * 60 * 60);
#[derive(Clone, Debug)]
pub struct SyncPeerConfig {
interval: Option<Duration>,
sync_now: bool,
#[cfg(feature = "preview")]
sync_on_hello: bool,
}
impl SyncPeerConfig {
pub fn builder() -> SyncPeerConfigBuilder {
Default::default()
}
}
impl From<SyncPeerConfig> for aranya_daemon_api::SyncPeerConfig {
fn from(value: SyncPeerConfig) -> Self {
Self {
interval: value.interval,
sync_now: value.sync_now,
#[cfg(feature = "preview")]
sync_on_hello: value.sync_on_hello,
}
}
}
#[derive(Debug)]
pub struct SyncPeerConfigBuilder {
interval: Option<Duration>,
sync_now: bool,
#[cfg(feature = "preview")]
sync_on_hello: bool,
}
impl SyncPeerConfigBuilder {
pub fn new() -> Self {
Default::default()
}
pub fn build(self) -> Result<SyncPeerConfig> {
if let Some(interval) = self.interval {
if interval > MAX_SYNC_INTERVAL {
return Err(ConfigError::InvalidArg(InvalidArg::new(
"duration",
"must not exceed 1 year to prevent overflow",
))
.into());
}
}
Ok(SyncPeerConfig {
interval: self.interval,
sync_now: self.sync_now,
#[cfg(feature = "preview")]
sync_on_hello: self.sync_on_hello,
})
}
pub fn interval(mut self, duration: Duration) -> Self {
self.interval = Some(duration);
self
}
pub fn sync_now(mut self, sync_now: bool) -> Self {
self.sync_now = sync_now;
self
}
#[cfg(feature = "preview")]
#[cfg_attr(docsrs, doc(cfg(feature = "preview")))]
pub fn sync_on_hello(mut self, sync_on_hello: bool) -> Self {
self.sync_on_hello = sync_on_hello;
self
}
}
impl Default for SyncPeerConfigBuilder {
fn default() -> Self {
Self {
interval: None,
sync_now: true,
#[cfg(feature = "preview")]
sync_on_hello: false,
}
}
}
#[cfg(feature = "preview")]
#[cfg_attr(docsrs, doc(cfg(feature = "preview")))]
#[derive(Clone, Debug)]
pub struct HelloSubscriptionConfig {
graph_change_debounce: Duration,
expiration: Duration,
periodic_interval: Duration,
}
#[cfg(feature = "preview")]
impl HelloSubscriptionConfig {
pub fn builder() -> HelloSubscriptionConfigBuilder {
Default::default()
}
pub fn graph_change_debounce(&self) -> Duration {
self.graph_change_debounce
}
pub fn expiration(&self) -> Duration {
self.expiration
}
pub fn periodic_interval(&self) -> Duration {
self.periodic_interval
}
}
#[cfg(feature = "preview")]
impl Default for HelloSubscriptionConfig {
fn default() -> Self {
Self {
graph_change_debounce: Duration::from_millis(100),
expiration: MAX_SYNC_INTERVAL,
periodic_interval: Duration::from_secs(10),
}
}
}
#[cfg(feature = "preview")]
#[cfg_attr(docsrs, doc(cfg(feature = "preview")))]
#[derive(Debug)]
pub struct HelloSubscriptionConfigBuilder {
graph_change_debounce: Duration,
expiration: Duration,
periodic_interval: Duration,
}
#[cfg(feature = "preview")]
impl HelloSubscriptionConfigBuilder {
pub fn new() -> Self {
Default::default()
}
pub fn build(self) -> Result<HelloSubscriptionConfig> {
Ok(HelloSubscriptionConfig {
graph_change_debounce: self.graph_change_debounce,
expiration: self.expiration,
periodic_interval: self.periodic_interval,
})
}
pub fn graph_change_debounce(mut self, duration: Duration) -> Self {
self.graph_change_debounce = duration;
self
}
pub fn expiration(mut self, duration: Duration) -> Self {
self.expiration = duration;
self
}
pub fn periodic_interval(mut self, interval: Duration) -> Self {
self.periodic_interval = interval;
self
}
}
#[cfg(feature = "preview")]
impl Default for HelloSubscriptionConfigBuilder {
fn default() -> Self {
let config = HelloSubscriptionConfig::default();
Self {
graph_change_debounce: config.graph_change_debounce,
expiration: config.expiration,
periodic_interval: config.periodic_interval,
}
}
}