1extern crate regex;
2
3use regex::*;
4use std::process::{Command, Output};
5
6type EscapedString = String;
7
8pub fn escape_text(input_str: String) -> EscapedString {
9 let space_re = Regex::new(r" ").unwrap();
10 let symb_re = Regex::new("([\\()<>|;&*~\"\'])").unwrap();
11 let space_str = space_re.replace_all(&input_str, "%s");
12 return symb_re.replace_all(&space_str, r"\$1").into_owned();
13}
14
15pub fn echo_adb(str: EscapedString) -> Output {
16 return Command::new("adb")
17 .args(&["shell", "input", "text", &str])
18 .output()
19 .expect("Failed to echo to device.");
20}
21
22#[cfg(test)]
23mod tests {
24 use super::*;
25
26 #[test]
27 fn test_open_paren() {
28 let input = "(".to_string();
29 assert_eq!(escape_text(input), r"\(");
30 }
31
32 #[test]
33 fn test_close_paren() {
34 let input = ")".to_string();
35 assert_eq!(escape_text(input), r"\)");
36 }
37
38 #[test]
39 fn test_open_chevron() {
40 let input = "<".to_string();
41 assert_eq!(escape_text(input), r"\<");
42 }
43
44 #[test]
45 fn test_close_chevron() {
46 let input = ">".to_string();
47 assert_eq!(escape_text(input), r"\>");
48 }
49
50 #[test]
51 fn test_pipe() {
52 let input = "|".to_string();
53 assert_eq!(escape_text(input), r"\|");
54 }
55
56 #[test]
57 fn test_semicolon() {
58 let input = ";".to_string();
59 assert_eq!(escape_text(input), r"\;");
60 }
61
62 #[test]
63 fn test_ampersand() {
64 let input = "&".to_string();
65 assert_eq!(escape_text(input), r"\&");
66 }
67
68 #[test]
69 fn test_star() {
70 let input = "*".to_string();
71 assert_eq!(escape_text(input), r"\*");
72 }
73
74 #[test]
75 fn test_backslash() {
76 let input = r"\".to_string();
77 assert_eq!(escape_text(input), "\\");
78 }
79
80 #[test]
81 fn test_tilde() {
82 let input = "~".to_string();
83 assert_eq!(escape_text(input), r"\~");
84 }
85
86 #[test]
87 fn test_quote() {
88 let input = "\"".to_string();
89 assert_eq!(escape_text(input), "\\\"");
90 }
91
92 #[test]
93 fn test_apostrophe() {
94 let input = "'".to_string();
95 assert_eq!(escape_text(input), "\\'");
96 }
97
98 #[test]
99 fn test_space() {
100 let input = " ".to_string();
101 assert_eq!(escape_text(input), "%s");
102 }
103}