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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
extern crate regex; use regex::*; use std::process::{Command, Output}; type EscapedString = String; pub fn escape_text(input_str: String) -> EscapedString { let space_re = Regex::new(r" ").unwrap(); let symb_re = Regex::new("([\\()<>|;&*~\"\'])").unwrap(); let space_str = space_re.replace_all(&input_str, "%s"); return symb_re.replace_all(&space_str, r"\$1").into_owned(); } pub fn echo_adb(str: EscapedString) -> Output { return Command::new("adb") .args(&["shell", "input", "text", &str]) .output() .expect("Failed to echo to device."); } #[cfg(test)] mod tests { use super::*; #[test] fn test_open_paren() { let input = "(".to_string(); assert_eq!(escape_text(input), r"\("); } #[test] fn test_close_paren() { let input = ")".to_string(); assert_eq!(escape_text(input), r"\)"); } #[test] fn test_open_chevron() { let input = "<".to_string(); assert_eq!(escape_text(input), r"\<"); } #[test] fn test_close_chevron() { let input = ">".to_string(); assert_eq!(escape_text(input), r"\>"); } #[test] fn test_pipe() { let input = "|".to_string(); assert_eq!(escape_text(input), r"\|"); } #[test] fn test_semicolon() { let input = ";".to_string(); assert_eq!(escape_text(input), r"\;"); } #[test] fn test_ampersand() { let input = "&".to_string(); assert_eq!(escape_text(input), r"\&"); } #[test] fn test_star() { let input = "*".to_string(); assert_eq!(escape_text(input), r"\*"); } #[test] fn test_backslash() { let input = r"\".to_string(); assert_eq!(escape_text(input), "\\"); } #[test] fn test_tilde() { let input = "~".to_string(); assert_eq!(escape_text(input), r"\~"); } #[test] fn test_quote() { let input = "\"".to_string(); assert_eq!(escape_text(input), "\\\""); } #[test] fn test_apostrophe() { let input = "'".to_string(); assert_eq!(escape_text(input), "\\'"); } #[test] fn test_space() { let input = " ".to_string(); assert_eq!(escape_text(input), "%s"); } }