1use std::time::Duration;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct WsConfig {
25 pub heartbeat_interval: Duration,
27 pub broadcast_capacity: usize,
29 pub max_connections: usize,
31}
32
33impl Default for WsConfig {
34 fn default() -> Self {
35 Self {
36 heartbeat_interval: Duration::from_secs(30),
37 broadcast_capacity: 1024,
38 max_connections: 1000,
39 }
40 }
41}
42
43impl WsConfig {
44 pub fn new() -> Self {
46 Self::default()
47 }
48
49 pub fn builder() -> WsConfigBuilder {
51 WsConfigBuilder::default()
52 }
53}
54
55#[derive(Debug, Clone)]
57pub struct WsConfigBuilder {
58 heartbeat_interval: Duration,
59 broadcast_capacity: usize,
60 max_connections: usize,
61}
62
63impl Default for WsConfigBuilder {
64 fn default() -> Self {
65 let d = WsConfig::default();
66 Self {
67 heartbeat_interval: d.heartbeat_interval,
68 broadcast_capacity: d.broadcast_capacity,
69 max_connections: d.max_connections,
70 }
71 }
72}
73
74impl WsConfigBuilder {
75 pub fn heartbeat_interval(mut self, v: Duration) -> Self {
77 self.heartbeat_interval = v;
78 self
79 }
80
81 pub fn broadcast_capacity(mut self, v: usize) -> Self {
83 self.broadcast_capacity = v;
84 self
85 }
86
87 pub fn max_connections(mut self, v: usize) -> Self {
89 self.max_connections = v;
90 self
91 }
92
93 pub fn build(self) -> WsConfig {
95 WsConfig {
96 heartbeat_interval: self.heartbeat_interval,
97 broadcast_capacity: self.broadcast_capacity,
98 max_connections: self.max_connections,
99 }
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use super::*;
106
107 #[test]
108 fn defaults() {
109 let c = WsConfig::default();
110 assert_eq!(c.heartbeat_interval, Duration::from_secs(30));
111 assert_eq!(c.broadcast_capacity, 1024);
112 assert_eq!(c.max_connections, 1000);
113 }
114
115 #[test]
116 fn builder_overrides() {
117 let c = WsConfig::builder()
118 .heartbeat_interval(Duration::from_secs(10))
119 .broadcast_capacity(512)
120 .max_connections(10)
121 .build();
122 assert_eq!(c.heartbeat_interval, Duration::from_secs(10));
123 assert_eq!(c.broadcast_capacity, 512);
124 assert_eq!(c.max_connections, 10);
125 }
126
127 #[test]
128 fn builder_default_equals_config_default() {
129 let via_builder = WsConfig::builder().build();
130 assert_eq!(via_builder, WsConfig::default());
131 }
132}