cargo_mobile2/android/adb/
mod.rs

1pub mod device_list;
2pub mod device_name;
3pub mod get_prop;
4
5pub use self::{device_list::device_list, device_name::device_name, get_prop::get_prop};
6
7use super::env::Env;
8use crate::{env::ExplicitEnv as _, util::cli::Report, DuctExpressionExt};
9use std::{ffi::OsString, str, string::FromUtf8Error};
10use thiserror::Error;
11
12pub fn adb<U>(env: &Env, args: U) -> duct::Expression
13where
14    U: IntoIterator,
15    U::Item: Into<OsString>,
16{
17    duct::cmd(env.platform_tools_path().join("adb"), args).vars(env.explicit_env())
18}
19
20#[derive(Debug, Error)]
21pub enum RunCheckedError {
22    #[error(transparent)]
23    InvalidUtf8(#[from] FromUtf8Error),
24    #[error("This device doesn't yet trust this computer. On the device, you should see a prompt like \"Allow USB debugging?\". Pressing \"Allow\" should fix this.")]
25    Unauthorized,
26}
27
28impl RunCheckedError {
29    pub fn report(&self, msg: &str) -> Report {
30        match self {
31            Self::InvalidUtf8(err) => Report::error(msg, err),
32            Self::Unauthorized => Report::action_request(msg, self),
33        }
34    }
35}
36
37fn check_authorized(output: &std::process::Output) -> Result<String, RunCheckedError> {
38    if !output.status.success() {
39        if let Ok(stderr) = String::from_utf8(output.stderr.clone()) {
40            if stderr.contains("error: device unauthorized") {
41                return Err(RunCheckedError::Unauthorized);
42            }
43        }
44    }
45    let stdout = String::from_utf8(output.stdout.clone())?.trim().to_string();
46    Ok(stdout)
47}