use std::time::Duration;
use url::Url;
#[derive(Debug, Clone)]
pub struct Config {
pub node_url: Url,
pub reconnection_timeout: Duration,
pub reconnection_delay: Duration,
}
impl Config {
#[must_use]
pub fn new(node_url: Url) -> Self {
Self {
node_url,
reconnection_timeout: Duration::MAX,
reconnection_delay: Duration::from_secs(1),
}
}
#[must_use]
pub fn with_reconnection_timeout(mut self, timeout: Duration) -> Self {
self.reconnection_timeout = timeout;
self
}
#[must_use]
pub fn with_reconnection_delay(mut self, delay: Duration) -> Self {
self.reconnection_delay = delay;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_populates_url_and_defaults_match_ts_reference() {
let url = Url::parse("ws://127.0.0.1:9944").unwrap();
let cfg = Config::new(url.clone());
assert_eq!(cfg.node_url, url);
assert_eq!(cfg.reconnection_delay, Duration::from_secs(1));
assert_eq!(cfg.reconnection_timeout, Duration::MAX);
}
#[test]
fn builder_overrides_apply_in_isolation() {
let url = Url::parse("wss://node.example:443").unwrap();
let cfg = Config::new(url)
.with_reconnection_delay(Duration::from_millis(250))
.with_reconnection_timeout(Duration::from_secs(30));
assert_eq!(cfg.reconnection_delay, Duration::from_millis(250));
assert_eq!(cfg.reconnection_timeout, Duration::from_secs(30));
}
}