adb_kit/
config.rs

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