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
58
59
60
61
62
63
64
65
66
67
use std::process::Command;

use crate::{ADBPathCommand, ADBResult};

use super::CompressionAlgorithm;

/// Copy files/directories from device
pub struct ADBPull {
    path: Option<String>,
    remote: String,
    local: String,
    shell: Command,
}

impl ADBPull {
    pub fn new(remote: &str, local: &str) -> Self {
        let mut cmd = Command::new("adb");
        cmd.arg("pull");

        ADBPull {
            path: None,
            remote: remote.to_owned(),
            local: local.to_owned(),
            shell: cmd,
        }
    }

    /// Preserve file timestamp and mode
    pub fn timestamp(mut self) -> Self {
        self.shell.arg("-a");
        self
    }

    /// Enable compression with the specified algorithm
    pub fn compression(mut self, algorithm: CompressionAlgorithm) -> Self {
        self.shell.arg("-z").arg(algorithm.to_string());
        self
    }

    /// Disable compression
    pub fn no_compression(mut self) -> Self {
        self.shell.arg("-Z");
        self
    }
}

impl ADBPathCommand for ADBPull {
    fn build(&mut self) -> Result<&mut Command, String> {
        match &self.path {
            Some(path) => {
                self.shell
                    .arg(format!("{}{}", path, self.remote))
                    .arg(&self.local);
                Ok(&mut self.shell)
            }
            None => Err("No path specified".to_string()),
        }
    }

    fn process_output(&self, output: ADBResult) -> ADBResult {
        output
    }

    fn path(&mut self, path: Option<String>) {
        self.path = path
    }
}