adb_utils/commands/general/
version.rs

1use std::process::Command;
2
3use crate::{ADBCommand, ADBResult};
4
5/// Show the adb version
6pub struct ADBVersion {
7    shell: Command,
8}
9
10impl Default for ADBVersion {
11    fn default() -> Self {
12        let mut cmd = Command::new("adb");
13        cmd.arg("version");
14
15        Self { shell: cmd }
16    }
17}
18
19impl ADBCommand for ADBVersion {
20    fn build(&mut self) -> Result<&mut Command, String> {
21        Ok(&mut self.shell)
22    }
23
24    fn process_output(&self, output: ADBResult) -> ADBResult {
25        output
26    }
27}
28
29/// Helper struct to help parse the result from [ADBVersion]
30pub struct Version {
31    pub adb_version: String,
32    pub version: String,
33    pub install_path: String,
34}
35
36impl From<ADBResult> for Version {
37    fn from(value: ADBResult) -> Self {
38        #[cfg(target_os = "windows")]
39        let value = ADBResult {
40            data: value.data.replace("\r\n", "\n"),
41        };
42        let adb_version_index = "Android Debug Bridge version ".len();
43        let version_index = value.data.find("Version ").unwrap();
44        let install_index = value.data.find("Installed as ").unwrap();
45
46        Self {
47            adb_version: value
48                .data
49                .get(adb_version_index..version_index - 1)
50                .unwrap()
51                .to_owned(),
52            version: value
53                .data
54                .get(version_index + "Version ".len()..install_index - 1)
55                .unwrap()
56                .to_owned(),
57            install_path: value
58                .data
59                .get(install_index + "Installed at ".len()..value.data.len())
60                .unwrap()
61                .to_owned(),
62        }
63    }
64}