cargo_mobile2/android/adb/
get_prop.rs

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
use crate::{
    android::env::Env,
    util::cli::{Report, Reportable},
};
use std::str;
use thiserror::Error;

use super::adb;

#[derive(Debug, Error)]
pub enum Error {
    #[error("Failed to run `adb shell getprop {prop}`: {source}")]
    LookupFailed {
        prop: String,
        source: super::RunCheckedError,
    },
    #[error(transparent)]
    Io(#[from] std::io::Error),
}

impl Error {
    fn prop(&self) -> &str {
        match self {
            Self::LookupFailed { prop, .. } => prop,
            Self::Io(_) => unreachable!(),
        }
    }
}

impl Reportable for Error {
    fn report(&self) -> Report {
        let msg = format!("Failed to run `adb shell getprop {}`", self.prop());
        match self {
            Self::LookupFailed { source, .. } => source.report(&msg),
            Self::Io(err) => Report::error("IO error", err),
        }
    }
}

pub fn get_prop(env: &Env, serial_no: &str, prop: &str) -> Result<String, Error> {
    let prop_ = prop.to_string();
    let handle = adb(env, ["-s", serial_no])
        .before_spawn(move |cmd| {
            cmd.args(["shell", "getprop", &prop_]);
            Ok(())
        })
        .stdin_file(os_pipe::dup_stdin().unwrap())
        .stdout_capture()
        .stderr_capture()
        .start()?;

    let output = handle.wait()?;
    super::check_authorized(output).map_err(|source| Error::LookupFailed {
        prop: prop.to_owned(),
        source,
    })
}