adb_kit/
config.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4/// ADB 配置结构体
5#[derive(Debug, Clone, Deserialize, Serialize)]
6pub struct ADBConfig {
7    /// ADB 可执行文件路径
8    pub path: PathBuf,
9    /// 重试最大次数
10    pub max_retries: u32,
11    /// 重试延迟(毫秒)
12    pub retry_delay: u64,
13    /// 操作超时(毫秒)
14    pub timeout: u64,
15    /// 日志级别
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub log_level: Option<String>,
18    /// 额外的命令行参数
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub additional_args: Option<Vec<String>>,
21}
22
23impl Default for ADBConfig {
24    fn default() -> Self {
25        ADBConfig {
26            path: PathBuf::from("adb"),
27            max_retries: 3,
28            retry_delay: 1000,
29            timeout: 30000, // 30秒超时
30            log_level: None,
31            additional_args: None,
32        }
33    }
34}
35
36/// ADB 配置构建器
37#[derive(Default)]
38pub struct ADBConfigBuilder {
39    path: Option<PathBuf>,
40    max_retries: Option<u32>,
41    retry_delay: Option<u64>,
42    timeout: Option<u64>,
43    log_level: Option<String>,
44    additional_args: Option<Vec<String>>,
45}
46
47impl ADBConfigBuilder {
48    /// 设置 ADB 可执行文件路径
49    pub fn path(mut self, path: impl Into<PathBuf>) -> Self {
50        self.path = Some(path.into());
51        self
52    }
53
54    /// 设置最大重试次数
55    pub fn max_retries(mut self, retries: u32) -> Self {
56        self.max_retries = Some(retries);
57        self
58    }
59
60    /// 设置重试延迟
61    pub fn retry_delay(mut self, delay: u64) -> Self {
62        self.retry_delay = Some(delay);
63        self
64    }
65
66    /// 设置操作超时
67    pub fn timeout(mut self, timeout: u64) -> Self {
68        self.timeout = Some(timeout);
69        self
70    }
71
72    /// 设置日志级别
73    pub fn log_level(mut self, level: &str) -> Self {
74        self.log_level = Some(level.to_string());
75        self
76    }
77
78    /// 添加额外命令行参数
79    pub fn add_arg(mut self, arg: &str) -> Self {
80        if self.additional_args.is_none() {
81            self.additional_args = Some(Vec::new());
82        }
83
84        if let Some(args) = &mut self.additional_args {
85            args.push(arg.to_string());
86        }
87
88        self
89    }
90
91    /// 构建 ADB 配置
92    pub fn build(self) -> ADBConfig {
93        let default = ADBConfig::default();
94
95        ADBConfig {
96            path: self.path.unwrap_or(default.path),
97            max_retries: self.max_retries.unwrap_or(default.max_retries),
98            retry_delay: self.retry_delay.unwrap_or(default.retry_delay),
99            timeout: self.timeout.unwrap_or(default.timeout),
100            log_level: self.log_level,
101            additional_args: self.additional_args,
102        }
103    }
104}