rrun_ssh/
remote_location.rs1use std::ffi::OsString;
2use std::fmt;
3use std::path::{Path, PathBuf};
4
5#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct RemoteLocation {
8 pub host: OsString,
10
11 pub path: PathBuf,
13}
14impl RemoteLocation {
15 pub fn new(s: &str) -> Result<RemoteLocation, ()> {
17 let mut split = s.split(':');
18
19 let host = match split.next() {
20 None | Some("") => return Err(()),
21 Some(h) => h,
22 };
23
24 let path = Path::new(try!(split.next().ok_or(()))).to_path_buf();
25
26 if split.next().is_some() {
27 return Err(());
28 }
29
30 Ok(RemoteLocation {
31 host: host.into(),
32 path: path,
33 })
34 }
35}
36impl fmt::Display for RemoteLocation {
37 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38 write!(f,
39 "{}:{}",
40 self.host.as_os_str().to_str().unwrap(),
41 self.path.display())
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use std::ffi::OsString;
48 use std::path::PathBuf;
49 use super::RemoteLocation;
50
51 #[test]
52 fn empty_string() {
53 assert!(RemoteLocation::new("").is_err());
54 }
55
56 #[test]
57 fn home() {
58 assert!(RemoteLocation::new("/home/user").is_err());
59 }
60
61 #[test]
62 fn root() {
63 assert!(RemoteLocation::new("/").is_err());
64 }
65
66 #[test]
67 fn path_without_host() {
68 assert!(RemoteLocation::new(":/").is_err());
69 }
70
71 #[test]
72 fn host_without_path() {
73 let RemoteLocation { host, path } = RemoteLocation::new("server:").unwrap();
74 assert_eq!(host, OsString::from("server"));
75 assert_eq!(path, PathBuf::from(""));
76 }
77
78 #[test]
79 fn host_with_path() {
80 let RemoteLocation { host, path } = RemoteLocation::new("server:/home/user").unwrap();
81 assert_eq!(host, OsString::from("server"));
82 assert_eq!(path, PathBuf::from("/home/user/"));
83 }
84}