use std::sync::Arc;
use std::time::Duration;
pub type TokenProvider = Arc<
dyn Fn() -> std::pin::Pin<Box<dyn std::future::Future<Output = Option<String>> + Send>>
+ Send
+ Sync,
>;
#[derive(Clone)]
pub struct SyncConfig {
pub url: String,
pub jwt_token: String,
pub client_version: String,
pub min_backoff: Duration,
pub max_backoff: Duration,
pub ping_interval: Duration,
pub max_batch_size: usize,
pub token_provider: Option<TokenProvider>,
pub token_lifetime_secs: u64,
pub tenant_id: u64,
}
impl std::fmt::Debug for SyncConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SyncConfig")
.field("url", &self.url)
.field("jwt_token", &"[REDACTED]")
.field("client_version", &self.client_version)
.field("min_backoff", &self.min_backoff)
.field("max_backoff", &self.max_backoff)
.field("ping_interval", &self.ping_interval)
.field("max_batch_size", &self.max_batch_size)
.field("token_provider", &self.token_provider.is_some())
.field("token_lifetime_secs", &self.token_lifetime_secs)
.field("tenant_id", &self.tenant_id)
.finish()
}
}
impl SyncConfig {
pub fn new(url: impl Into<String>, jwt_token: impl Into<String>) -> Self {
Self {
url: url.into(),
jwt_token: jwt_token.into(),
client_version: env!("CARGO_PKG_VERSION").to_string(),
min_backoff: Duration::from_secs(1),
max_backoff: Duration::from_secs(60),
ping_interval: Duration::from_secs(30),
max_batch_size: 100,
token_provider: None,
token_lifetime_secs: 0,
tenant_id: 1,
}
}
pub fn with_token_provider(mut self, provider: TokenProvider, lifetime_secs: u64) -> Self {
self.token_provider = Some(provider);
self.token_lifetime_secs = lifetime_secs;
self
}
pub fn with_tenant_id(mut self, tenant_id: u64) -> Self {
self.tenant_id = tenant_id;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_tenant_id_is_one() {
let config = SyncConfig::new("wss://localhost:9090/sync", "t");
assert_eq!(config.tenant_id, 1);
}
#[test]
fn with_tenant_id_sets_tenant() {
let config = SyncConfig::new("wss://localhost:9090/sync", "t").with_tenant_id(42);
assert_eq!(config.tenant_id, 42);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SyncState {
Disconnected,
Connecting,
Connected,
Reconnecting,
}