1use std::path::PathBuf;
2
3pub fn is_not_empty(s: &str) -> bool {
4 !s.trim().is_empty()
5}
6
7pub fn escape_osa_exp(s: &str) -> String {
9 s.replace('\\', "\\\\").replace('"', "\\\"")
10}
11
12pub fn expand_tilde(path: &str) -> PathBuf {
16 if let Some(rest) = path.strip_prefix('~') {
17 let home = home::home_dir().unwrap_or_default();
18 home.join(rest.trim_start_matches('/'))
19 } else {
20 PathBuf::from(path)
21 }
22}
23
24#[cfg(test)]
25mod tests {
26 use super::*;
27
28 #[test]
29 fn escape_applescript() {
30 assert_eq!(escape_osa_exp(r#"say "hello""#), r#"say \"hello\""#);
31 assert_eq!(escape_osa_exp(r"path\to"), r"path\\to");
32 }
33
34 #[test]
35 fn expand_tilde_replaces_home() {
36 let result = expand_tilde("~/Documents/test.txt");
37 let s = result.to_string_lossy();
38 assert!(!s.starts_with('~'));
39 assert!(s.ends_with("Documents/test.txt"));
40 }
41
42 #[test]
43 fn expand_tilde_no_change_for_absolute() {
44 let result = expand_tilde("/usr/local/bin");
45 assert_eq!(result, PathBuf::from("/usr/local/bin"));
46 }
47
48 #[test]
49 fn expand_tilde_bare() {
50 let result = expand_tilde("~");
51 assert!(!result.to_string_lossy().contains('~'));
52 assert!(!result.to_string_lossy().is_empty());
53 }
54}