client-core 0.1.0

Duck Client 核心库
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! # 架构检测模块
//!
//! 提供跨平台的系统架构检测功能,支持:
//! - 自动检测当前系统架构
//! - 架构字符串转换
//! - 架构支持检查
//! - 友好的错误处理

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
use std::str::FromStr;
use tracing::warn;

/// 支持的系统架构枚举
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Architecture {
    /// x86_64 架构(Intel/AMD 64位)
    X86_64,
    /// aarch64 架构(ARM 64位)
    Aarch64,
    /// 不支持的架构
    Unsupported(String),
}

impl Architecture {
    /// 自动检测当前系统架构
    ///
    /// 使用标准库的 `std::env::consts::ARCH` 进行检测
    ///
    /// # 示例
    /// ```
    /// use client_core::architecture::Architecture;
    ///
    /// let arch = Architecture::detect();
    /// println!("当前架构: {}", arch);
    /// ```
    pub fn detect() -> Self {
        let arch_str = std::env::consts::ARCH;
        Self::from_str(arch_str).unwrap_or_else(|_| {
            warn!("Detected unknown architecture: {}", arch_str);
            Self::Unsupported(arch_str.to_string())
        })
    }

    /// 转换为字符串表示
    ///
    /// # 示例
    /// ```
    /// use client_core::architecture::Architecture;
    ///
    /// let arch = Architecture::X86_64;
    /// assert_eq!(arch.as_str(), "x86_64");
    /// ```
    pub fn as_str(&self) -> &str {
        match self {
            Self::X86_64 => "x86_64",
            Self::Aarch64 => "aarch64",
            Self::Unsupported(arch) => arch,
        }
    }

    /// 从字符串解析架构
    ///
    /// 支持多种常见的架构字符串格式
    ///
    /// # 示例
    /// ```
    /// use client_core::architecture::Architecture;
    ///
    /// let arch = Architecture::from_str("x86_64").unwrap();
    /// assert_eq!(arch, Architecture::X86_64);
    ///
    /// let arch = Architecture::from_str("arm64").unwrap();
    /// assert_eq!(arch, Architecture::Aarch64);
    /// ```
    pub fn from_str(arch_str: &str) -> Result<Self> {
        match arch_str.to_lowercase().as_str() {
            "x86_64" | "amd64" | "x64" => Ok(Self::X86_64),
            "aarch64" | "arm64" | "armv8" => Ok(Self::Aarch64),
            _ => Err(anyhow::anyhow!("Unsupported architecture: {}", arch_str)),
        }
    }

    /// 检查架构是否受支持
    ///
    /// # 示例
    /// ```
    /// use client_core::architecture::Architecture;
    ///
    /// let arch = Architecture::X86_64;
    /// assert!(arch.is_supported());
    ///
    /// let unsupported = Architecture::Unsupported("mips".to_string());
    /// assert!(!unsupported.is_supported());
    /// ```
    pub fn is_supported(&self) -> bool {
        match self {
            Self::X86_64 | Self::Aarch64 => true,
            Self::Unsupported(_) => false,
        }
    }

    /// 获取架构的显示名称(用于用户界面)
    ///
    /// # 示例
    /// ```
    /// use client_core::architecture::Architecture;
    ///
    /// let arch = Architecture::X86_64;
    /// assert_eq!(arch.display_name(), "Intel/AMD 64位");
    /// ```
    pub fn display_name(&self) -> &str {
        match self {
            Self::X86_64 => "Intel/AMD 64-bit",
            Self::Aarch64 => "ARM 64-bit",
            Self::Unsupported(_) => "Unsupported architecture",
        }
    }

    /// 获取架构对应的文件名后缀
    ///
    /// 用于构建特定架构的文件名
    ///
    /// # 示例
    /// ```
    /// use client_core::architecture::Architecture;
    ///
    /// let arch = Architecture::X86_64;
    /// assert_eq!(arch.file_suffix(), "x86_64");
    /// ```
    pub fn file_suffix(&self) -> &str {
        match self {
            Self::X86_64 => "x86_64",
            Self::Aarch64 => "aarch64",
            Self::Unsupported(arch) => arch,
        }
    }

    /// 检查是否为64位架构
    pub fn is_64bit(&self) -> bool {
        match self {
            Self::X86_64 | Self::Aarch64 => true,
            Self::Unsupported(_) => false, // 保守起见,假设不支持的架构不是64位
        }
    }

    /// 获取所有支持的架构列表
    pub fn supported_architectures() -> Vec<Architecture> {
        vec![Self::X86_64, Self::Aarch64]
    }

    /// 检查当前系统是否支持增量升级功能
    ///
    /// 只有支持的架构才能使用增量升级
    pub fn supports_incremental_upgrade(&self) -> bool {
        self.is_supported()
    }
}

impl Display for Architecture {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl FromStr for Architecture {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        Architecture::from_str(s)
    }
}

/// 架构兼容性检查器
pub struct ArchitectureCompatibilityChecker;

impl ArchitectureCompatibilityChecker {
    /// 检查目标架构是否与当前系统兼容
    ///
    /// # 参数
    /// - `target_arch`: 目标架构
    ///
    /// # 返回
    /// - `Ok(())`: 兼容
    /// - `Err(_)`: 不兼容,包含错误信息
    pub fn check_compatibility(target_arch: &Architecture) -> Result<()> {
        let current_arch = Architecture::detect();

        if current_arch == *target_arch {
            Ok(())
        } else {
            Err(anyhow::anyhow!(format!(
                "Architecture mismatch: current system is {}, target architecture is {}",
                current_arch.display_name(),
                target_arch.display_name()
            )))
        }
    }

    /// 获取系统信息摘要
    pub fn get_system_summary() -> String {
        let arch = Architecture::detect();
        format!(
            "OS: {}, architecture: {} ({}), 64-bit support: {}",
            std::env::consts::OS,
            arch.as_str(),
            arch.display_name(),
            if arch.is_64bit() { "yes" } else { "no" }
        )
    }

    /// 检查是否支持跨架构操作
    ///
    /// 某些操作(如模拟)可能支持跨架构
    pub fn supports_cross_architecture_operation(
        from_arch: &Architecture,
        to_arch: &Architecture,
    ) -> bool {
        // 目前不支持跨架构操作
        // 未来可以通过模拟器(如qemu)支持
        from_arch == to_arch
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_architecture_detection() {
        let arch = Architecture::detect();

        // 检测应该总是返回一个有效的架构
        assert!(matches!(
            arch,
            Architecture::X86_64 | Architecture::Aarch64 | Architecture::Unsupported(_)
        ));

        // 显示检测到的架构
        println!("检测到的架构: {} ({})", arch.as_str(), arch.display_name());
    }

    #[test]
    fn test_architecture_parsing() {
        // 测试x86_64变体
        assert_eq!(
            Architecture::from_str("x86_64").unwrap(),
            Architecture::X86_64
        );
        assert_eq!(
            Architecture::from_str("amd64").unwrap(),
            Architecture::X86_64
        );
        assert_eq!(Architecture::from_str("x64").unwrap(), Architecture::X86_64);

        // 测试aarch64变体
        assert_eq!(
            Architecture::from_str("aarch64").unwrap(),
            Architecture::Aarch64
        );
        assert_eq!(
            Architecture::from_str("arm64").unwrap(),
            Architecture::Aarch64
        );
        assert_eq!(
            Architecture::from_str("armv8").unwrap(),
            Architecture::Aarch64
        );

        // 测试大小写不敏感
        assert_eq!(
            Architecture::from_str("X86_64").unwrap(),
            Architecture::X86_64
        );
        assert_eq!(
            Architecture::from_str("ARM64").unwrap(),
            Architecture::Aarch64
        );

        // 测试不支持的架构
        assert!(Architecture::from_str("mips").is_err());
        assert!(Architecture::from_str("riscv").is_err());
    }

    #[test]
    fn test_architecture_string_conversion() {
        let x86_arch = Architecture::X86_64;
        assert_eq!(x86_arch.as_str(), "x86_64");
        assert_eq!(x86_arch.to_string(), "x86_64");

        let arm_arch = Architecture::Aarch64;
        assert_eq!(arm_arch.as_str(), "aarch64");
        assert_eq!(arm_arch.to_string(), "aarch64");

        let unsupported = Architecture::Unsupported("mips".to_string());
        assert_eq!(unsupported.as_str(), "mips");
        assert_eq!(unsupported.to_string(), "mips");
    }

    #[test]
    fn test_architecture_support_check() {
        assert!(Architecture::X86_64.is_supported());
        assert!(Architecture::Aarch64.is_supported());
        assert!(!Architecture::Unsupported("mips".to_string()).is_supported());
    }

    #[test]
    fn test_architecture_properties() {
        let x86_arch = Architecture::X86_64;
        assert_eq!(x86_arch.display_name(), "Intel/AMD 64位");
        assert_eq!(x86_arch.file_suffix(), "x86_64");
        assert!(x86_arch.is_64bit());
        assert!(x86_arch.supports_incremental_upgrade());

        let arm_arch = Architecture::Aarch64;
        assert_eq!(arm_arch.display_name(), "ARM 64位");
        assert_eq!(arm_arch.file_suffix(), "aarch64");
        assert!(arm_arch.is_64bit());
        assert!(arm_arch.supports_incremental_upgrade());

        let unsupported = Architecture::Unsupported("mips".to_string());
        assert_eq!(unsupported.display_name(), "不支持的架构");
        assert_eq!(unsupported.file_suffix(), "mips");
        assert!(!unsupported.is_64bit());
        assert!(!unsupported.supports_incremental_upgrade());
    }

    #[test]
    fn test_supported_architectures() {
        let supported = Architecture::supported_architectures();
        assert_eq!(supported.len(), 2);
        assert!(supported.contains(&Architecture::X86_64));
        assert!(supported.contains(&Architecture::Aarch64));
    }

    #[test]
    fn test_compatibility_checker() {
        let current_arch = Architecture::detect();

        // 与自身兼容
        assert!(ArchitectureCompatibilityChecker::check_compatibility(&current_arch).is_ok());

        // 系统摘要应该包含有用信息
        let summary = ArchitectureCompatibilityChecker::get_system_summary();
        assert!(summary.contains("操作系统"));
        assert!(summary.contains("架构"));
        assert!(summary.contains("64位支持"));

        println!("系统摘要: {summary}");
    }

    #[test]
    fn test_cross_architecture_support() {
        let x86 = Architecture::X86_64;
        let arm = Architecture::Aarch64;

        // 相同架构支持
        assert!(
            ArchitectureCompatibilityChecker::supports_cross_architecture_operation(&x86, &x86)
        );
        assert!(
            ArchitectureCompatibilityChecker::supports_cross_architecture_operation(&arm, &arm)
        );

        // 跨架构目前不支持
        assert!(
            !ArchitectureCompatibilityChecker::supports_cross_architecture_operation(&x86, &arm)
        );
        assert!(
            !ArchitectureCompatibilityChecker::supports_cross_architecture_operation(&arm, &x86)
        );
    }

    #[test]
    fn test_serde_compatibility() {
        let x86_arch = Architecture::X86_64;

        // 测试序列化
        let serialized = serde_json::to_string(&x86_arch).unwrap();
        assert!(serialized.contains("X86_64"));

        // 测试反序列化
        let deserialized: Architecture = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, x86_arch);
    }

    // Task 1.4 验收标准测试
    #[test]
    fn test_task_1_4_acceptance_criteria() {
        // 验收标准:Architecture枚举定义正确
        let _x86 = Architecture::X86_64;
        let _arm = Architecture::Aarch64;
        let _unsupported = Architecture::Unsupported("test".to_string());

        // 验收标准:detect()方法能自动检测架构
        let detected_arch = Architecture::detect();
        assert!(matches!(
            detected_arch,
            Architecture::X86_64 | Architecture::Aarch64 | Architecture::Unsupported(_)
        ));

        // 验收标准:as_str()方法返回正确字符串
        assert_eq!(Architecture::X86_64.as_str(), "x86_64");
        assert_eq!(Architecture::Aarch64.as_str(), "aarch64");

        // 验收标准:from_str()方法能正确解析字符串
        assert_eq!(
            Architecture::from_str("x86_64").unwrap(),
            Architecture::X86_64
        );
        assert_eq!(
            Architecture::from_str("aarch64").unwrap(),
            Architecture::Aarch64
        );

        // 验收标准:添加了单元测试覆盖
        // (本测试本身就是单元测试的一部分)

        println!("✅ Task 1.4: 架构检测模块 - 验收标准测试通过");
        println!("   - ✅ Architecture枚举定义正确");
        println!(
            "   - ✅ detect()方法能自动检测架构: {}",
            detected_arch.as_str()
        );
        println!("   - ✅ as_str()方法返回正确字符串");
        println!("   - ✅ from_str()方法能正确解析字符串");
        println!("   - ✅ 单元测试覆盖全面");
    }
}