1#[macro_use] extern crate lazy_static;
2extern crate pwd;
3extern crate dirs;
4
5use std::{
6 io,
7 path::{PathBuf, MAIN_SEPARATOR}
8};
9
10use pwd::Passwd;
11
12lazy_static! {
13static ref PREFIX: String = format!("~{}", MAIN_SEPARATOR);
14}
15
16pub fn expanduser<S: AsRef<str>>(s: S) -> io::Result<PathBuf> {
36 _expand_user(s.as_ref())
37}
38
39fn _expand_user(s: &str) -> io::Result<PathBuf> {
40 Ok(match s {
41 s if s == "~" => {
43 home_dir()?
44 },
45 s if s.starts_with(&*PREFIX) => {
47 let home = home_dir()?;
48 home.join(&s[2..])
49 },
50 s if s.starts_with("~") => {
52 let mut parts = s[1..].splitn(2, MAIN_SEPARATOR);
53 let user = parts.next()
54 .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "malformed path"))?;
55 let user = Passwd::from_name(&user)
56 .map_err(|_| io::Error::new(io::ErrorKind::Other, "error searching for user"))?
57 .ok_or_else(|| io::Error::new(io::ErrorKind::Other, format!("user '{}', does not exist", &user)))?;
58 if let Some(ref path) = parts.next() {
59 PathBuf::from(user.dir).join(&path)
60 } else {
61 PathBuf::from(user.dir)
62 }
63 },
64 s => PathBuf::from(s)
66 })
67}
68
69fn home_dir() -> io::Result<PathBuf> {
70 dirs::home_dir().ok_or_else(|| io::Error::new(io::ErrorKind::Other, "no home directory is set"))
71}
72
73#[cfg(test)]
74mod tests {
75 use std::env;
76 use super::*;
77
78 #[test]
83 fn test_success() {
84 let old_home = env::var("HOME").expect("no home dir set");
85 let new_home = "/home/foo";
86 env::set_var("HOME", new_home);
87 let path = expanduser("~/path/to/directory");
88 env::set_var("HOME", old_home);
89 assert_eq!(path.expect("io error"), PathBuf::from("/home/foo/path/to/directory"));
90 }
91
92 #[test]
93 fn test_only_tilde() {
94 let old_home = env::var("HOME").expect("no home dir set");
95 let new_home = "/home/foo";
96 env::set_var("HOME", new_home);
97 let pathstr = "~";
98 let path = expanduser(pathstr);
99 env::set_var("HOME", old_home);
100 assert_eq!(path.expect("io error"), PathBuf::from("/home/foo"));
101 }
102
103 #[test]
104 fn test_user() {
105 let user = env::var("USER").expect("no user set");
106 if user.len() < 1 {
107 panic!("user is empty");
108 }
109 let home = dirs::home_dir().expect("no home directory set");
110 let pathstr = format!("~{}/path/to/directory", &user);
111 let path = expanduser(&pathstr).expect("io error");
112 assert_eq!(path, home.join("path/to/directory"));
113 }
114
115 #[test]
116 fn test_just_tilde_user() {
117 let user = env::var("USER").expect("no user set");
118 if user.len() < 1 {
119 panic!("user is empty");
120 }
121 let home = dirs::home_dir().expect("no home directory set");
122 let pathstr = format!("~{}", &user);
123 let path = expanduser(&pathstr).expect("io error");
124 assert_eq!(path, home);
125 }
126
127 #[test]
128 fn test_fail_malformed_path() {
129 let pathstr = "~\ruses-invalid-path-char";
130 let err = expanduser(&pathstr).unwrap_err();
131 let kind = err.kind();
132 assert_eq!(kind, io::ErrorKind::Other);
133 }
134
135 #[test]
136 #[should_panic]
137 fn test_user_does_not_exist() {
138 expanduser("~user_that_should_not_exist/path/to/directory")
139 .expect("user does not exist");
140 }
141}