adb_utils/commands/general/
devices.rs

1use std::process::Command;
2
3use crate::{ADBCommand, ADBResult};
4
5/// List connected devices
6pub struct ADBDevices {
7    shell: Command,
8}
9
10impl Default for ADBDevices {
11    fn default() -> Self {
12        let mut cmd = Command::new("adb");
13        cmd.arg("devices");
14        Self { shell: cmd }
15    }
16}
17
18impl ADBDevices {
19    pub fn long(mut self) -> Self {
20        self.shell.arg("-l");
21        self
22    }
23}
24
25impl ADBCommand for ADBDevices {
26    fn build(&mut self) -> Result<&mut Command, String> {
27        Ok(&mut self.shell)
28    }
29
30    fn process_output(&self, output: ADBResult) -> ADBResult {
31        ADBResult {
32            data: output
33                .data
34                .replace("List of devices attached", "")
35                .replace("device", "")
36                .replace("\r\n", "\n")
37                .replace("\n\n", "\n")
38                .replace('\t', ""),
39        }
40    }
41}
42
43/// Helper struct to help parse the result from [ADBDevices]
44#[derive(Default)]
45pub struct DeviceInfo {
46    pub product: String,
47    pub model: String,
48    pub transport_id: u32,
49}
50
51impl From<ADBResult> for DeviceInfo {
52    fn from(value: ADBResult) -> Self {
53        let product_index = match value.data.find("product:") {
54            Some(index) => index,
55            None => return Self::default(),
56        };
57        let model_index = match value.data.find("model:") {
58            Some(index) => index,
59            None => return Self::default(),
60        };
61        let transport_index = match value.data.find("transport_id:") {
62            Some(index) => index,
63            None => return Self::default(),
64        };
65
66        Self {
67            product: value
68                .data
69                .get(product_index + "product:".len()..model_index - 1)
70                .unwrap()
71                .to_owned(),
72            model: value
73                .data
74                .get(model_index + "model:".len()..transport_index - 1)
75                .unwrap()
76                .to_owned(),
77            transport_id: value
78                .data
79                .get(transport_index + "transport_id:".len()..value.data.len() - 1)
80                .unwrap()
81                .parse::<u32>()
82                .unwrap(),
83        }
84    }
85}