Skip to main content

flare_core/client/builder/
base.rs

1//! 客户端构建器基类
2//!
3//! 提供所有构建器共享的配置方法,体现"公共逻辑统一处理"的设计原则。
4//!
5//! ## 设计原则
6//!
7//! - **统一配置接口**:所有构建器(简单模式、观察者模式、Flare 模式)共享相同的配置方法
8//! - **减少代码重复**:通过组合模式,让各个构建器复用配置逻辑
9//! - **类型安全**:使用 Rust 类型系统确保配置的正确性
10
11use crate::client::config::ClientConfig;
12use crate::common::compression::CompressionAlgorithm;
13use crate::common::config_types::{HeartbeatConfig, TlsConfig, TransportProtocol};
14use crate::common::device::DeviceInfo;
15use crate::common::protocol::SerializationFormat;
16use std::time::Duration;
17
18/// 客户端构建器基类配置
19///
20/// 提供所有构建器共享的配置方法
21/// 使用组合模式,让各个构建器可以复用这些配置逻辑
22pub struct BaseClientBuilderConfig {
23    pub config: ClientConfig,
24}
25
26impl BaseClientBuilderConfig {
27    /// 创建新的构建器配置
28    pub fn new(server_url: impl Into<String>) -> Self {
29        Self {
30            config: ClientConfig::new(server_url.into()),
31        }
32    }
33
34    /// 设置传输协议
35    #[must_use]
36    pub fn with_protocol(mut self, protocol: TransportProtocol) -> Self {
37        self.config.transport = protocol;
38        self
39    }
40
41    /// 启用多协议竞速
42    #[must_use]
43    pub fn with_protocol_race(mut self, protocols: Vec<TransportProtocol>) -> Self {
44        self.config = self.config.with_protocol_race(protocols);
45        self
46    }
47
48    /// 为特定协议设置服务器地址
49    #[must_use]
50    pub fn with_protocol_url(mut self, protocol: TransportProtocol, url: String) -> Self {
51        self.config = self.config.with_protocol_url(protocol, url);
52        self
53    }
54
55    /// 设置用户 ID
56    #[must_use]
57    pub fn with_user_id(mut self, user_id: String) -> Self {
58        self.config = self.config.with_user_id(user_id);
59        self
60    }
61
62    /// 设置 Token(用于认证)
63    #[must_use]
64    pub fn with_token(mut self, token: String) -> Self {
65        self.config = self.config.with_token(token);
66        self
67    }
68
69    /// 设置序列化格式(用于协商,默认 JSON)
70    #[must_use]
71    pub fn with_format(mut self, format: SerializationFormat) -> Self {
72        self.config = self.config.with_format(format);
73        self
74    }
75
76    /// 设置压缩算法(用于协商,默认 None)
77    #[must_use]
78    pub fn with_compression(mut self, compression: CompressionAlgorithm) -> Self {
79        self.config = self.config.with_compression(compression);
80        self
81    }
82
83    /// 强制指定序列化格式(不进行协商)
84    #[must_use]
85    pub fn force_format(mut self, format: SerializationFormat) -> Self {
86        self.config = self.config.force_format(format);
87        self
88    }
89
90    /// 强制指定压缩算法(不进行协商)
91    #[must_use]
92    pub fn force_compression(mut self, compression: CompressionAlgorithm) -> Self {
93        self.config = self.config.force_compression(compression);
94        self
95    }
96
97    /// 设置设备信息
98    #[must_use]
99    pub fn with_device_info(mut self, device_info: DeviceInfo) -> Self {
100        self.config = self.config.with_device_info(device_info);
101        self
102    }
103
104    /// 设置心跳配置
105    #[must_use]
106    pub fn with_heartbeat(mut self, heartbeat: HeartbeatConfig) -> Self {
107        self.config = self.config.with_heartbeat(heartbeat);
108        self
109    }
110
111    /// 设置 TLS 配置
112    #[must_use]
113    pub fn with_tls(mut self, tls: TlsConfig) -> Self {
114        self.config = self.config.with_tls(tls);
115        self
116    }
117
118    /// 设置连接超时
119    #[must_use]
120    pub fn with_connect_timeout(mut self, timeout: Duration) -> Self {
121        self.config = self.config.with_connect_timeout(timeout);
122        self
123    }
124
125    /// 设置协议竞速超时
126    #[must_use]
127    pub fn with_race_timeout(mut self, timeout: Duration) -> Self {
128        self.config = self.config.with_race_timeout(timeout);
129        self
130    }
131
132    /// 设置重连间隔
133    #[must_use]
134    pub fn with_reconnect_interval(mut self, interval: Duration) -> Self {
135        self.config = self.config.with_reconnect_interval(interval);
136        self
137    }
138
139    /// 设置最大重连次数
140    #[must_use]
141    pub fn with_max_reconnect_attempts(mut self, max: Option<u32>) -> Self {
142        self.config = self.config.with_max_reconnect_attempts(max);
143        self
144    }
145
146    /// 启用消息路由
147    #[must_use]
148    pub fn enable_router(mut self) -> Self {
149        self.config = self.config.enable_router();
150        self
151    }
152}