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
68
69
70
71
72
73
use std::process::Command;

use crate::{ADBPathCommand, ADBResult};

use super::CompressionAlgorithm;

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

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

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

    /// Only push files that are newer on the host than the device
    pub fn sync(mut self) -> Self {
        self.shell.arg("--sync");
        self
    }

    /// Push files to device without storing to the filesystem
    pub fn dry_run(mut self) -> Self {
        self.shell.arg("-n");
        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 ADBPush {
    fn build(&mut self) -> Result<&mut Command, String> {
        match &self.path {
            Some(path) => {
                self.shell
                    .arg(format!("{}\\{}", path, self.local))
                    .arg(&self.remote);
                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
    }
}