use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteTarget {
pub user: String,
pub host: String,
pub port: u16,
pub path: String,
}
impl std::fmt::Display for RemoteTarget {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}@{}:{}", self.user, self.host, self.path)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Location {
Local(PathBuf),
Remote(RemoteTarget),
}
impl Location {
#[must_use]
pub fn parse(s: &str) -> Self {
if let Some((user, rest)) = s.split_once('@') {
if user.contains('/') || user.contains('\\') {
return Location::Local(PathBuf::from(s));
}
let (host, path) = parse_remote(rest);
if host.contains('/') || host.contains('\\') {
return Location::Local(PathBuf::from(s));
}
Location::Remote(RemoteTarget {
user: user.to_string(),
host,
port: DEFAULT_PORT,
path,
})
} else {
Location::Local(PathBuf::from(s))
}
}
}
impl std::fmt::Display for Location {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Local(path) => write!(f, "{}", path.display()),
Self::Remote(r) => write!(f, "{r}"),
}
}
}
const DEFAULT_PORT: u16 = 22;
fn parse_remote(rest: &str) -> (String, String) {
let (host_port, slash_path) = match rest.split_once('/') {
Some((hp, p)) => (hp, Some(p)),
None => (rest, None),
};
let absolute = host_port.ends_with(':') && slash_path.is_some();
let path = slash_path.map_or(String::new(), |p| {
if absolute {
format!("/{p}")
} else {
p.to_string()
}
});
let (host, path) = if let Some(rest_brackets) = host_port.strip_prefix('[') {
match rest_brackets.split_once(']') {
Some((host, tail)) => {
let path = match tail.strip_prefix(':') {
Some(suffix) => join_suffix(&path, suffix),
None => path,
};
(host, path)
}
None => (host_port, path),
}
} else if host_port.starts_with("::") {
(host_port, path)
} else {
match host_port.split_once(':') {
Some((host, suffix)) => (host, join_suffix(&path, suffix)),
None => (host_port, path),
}
};
(host.to_string(), path)
}
fn join_suffix(path: &str, suffix: &str) -> String {
if suffix.is_empty() {
path.to_string()
} else if path.is_empty() {
suffix.to_string()
} else if path.starts_with('/') {
path.to_string()
} else {
format!("{suffix}/{path}")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_numeric_suffix_is_path_not_port() {
let loc = Location::parse("user@127.0.0.1:4433/backup");
match loc {
Location::Remote(r) => {
assert_eq!(r.user, "user");
assert_eq!(r.host, "127.0.0.1");
assert_eq!(r.port, 22);
assert_eq!(r.path, "4433/backup");
}
Location::Local(_) => panic!("expected remote"),
}
let loc = Location::parse("user@127.0.0.1:4433");
match loc {
Location::Remote(r) => {
assert_eq!(r.port, 22);
assert_eq!(r.path, "4433");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_host_slash_path_defaults_port() {
let loc = Location::parse("user@127.0.0.1:/data");
match loc {
Location::Remote(r) => {
assert_eq!(r.port, 22);
assert_eq!(r.path, "/data");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_rsync_host_colon_path_keeps_path() {
let loc = Location::parse("user@127.0.0.1:backup");
match loc {
Location::Remote(r) => {
assert_eq!(r.port, 22);
assert_eq!(r.path, "backup");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_rsync_host_colon_path_with_slashes_keeps_all_segments() {
let loc = Location::parse("user@127.0.0.1:softwares/cp2");
match loc {
Location::Remote(r) => {
assert_eq!(r.port, 22);
assert_eq!(r.path, "softwares/cp2");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_absolute_path_after_colon() {
let loc = Location::parse("user@host:/home/user/softwares/cp2");
match loc {
Location::Remote(r) => {
assert_eq!(r.port, 22);
assert_eq!(r.path, "/home/user/softwares/cp2");
}
Location::Local(_) => panic!("expected remote"),
}
let loc = Location::parse("user@host:/");
assert_eq!(
match loc {
Location::Remote(r) => r.path,
Location::Local(_) => panic!("expected remote"),
},
"/"
);
let loc = Location::parse("user@host:");
assert_eq!(
match loc {
Location::Remote(r) => r.path,
Location::Local(_) => panic!("expected remote"),
},
""
);
}
#[test]
fn parse_hostname_kept_verbatim() {
let loc = Location::parse("alice@backup.example.com:2222");
match loc {
Location::Remote(r) => {
assert_eq!(r.host, "backup.example.com");
assert_eq!(r.port, 22);
assert_eq!(r.path, "2222");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_local() {
assert!(matches!(
Location::parse("/tmp/foo"),
Location::Local(_)
));
}
#[test]
fn parse_ipv6_bracketed_with_numeric_suffix() {
let loc = Location::parse("user@[::1]:2222/backup");
match loc {
Location::Remote(r) => {
assert_eq!(r.user, "user");
assert_eq!(r.host, "::1");
assert_eq!(r.port, 22);
assert_eq!(r.path, "2222/backup");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_ipv6_bracketed_without_port() {
let loc = Location::parse("user@[::1]/data");
match loc {
Location::Remote(r) => {
assert_eq!(r.host, "::1");
assert_eq!(r.port, 22);
assert_eq!(r.path, "data");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_ipv6_bracketed_numeric_suffix_only() {
let loc = Location::parse("user@[fe80::1]:4433");
match loc {
Location::Remote(r) => {
assert_eq!(r.host, "fe80::1");
assert_eq!(r.port, 22);
assert_eq!(r.path, "4433");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_ipv6_bracketed_absolute_path() {
let loc = Location::parse("user@[::1]:/data");
match loc {
Location::Remote(r) => {
assert_eq!(r.host, "::1");
assert_eq!(r.port, 22);
assert_eq!(r.path, "/data");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_ipv6_unbracketed_without_port() {
let loc = Location::parse("user@::1/data");
match loc {
Location::Remote(r) => {
assert_eq!(r.host, "::1");
assert_eq!(r.port, 22);
assert_eq!(r.path, "data");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_numeric_overflow_suffix_is_plain_path() {
let loc = Location::parse("user@[::1]:99999/x");
match loc {
Location::Remote(r) => {
assert_eq!(r.port, 22);
assert_eq!(r.path, "99999/x");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_first_colon_splits_verbatim_path() {
let loc = Location::parse("user@host:a:b");
match loc {
Location::Remote(r) => {
assert_eq!(r.user, "user");
assert_eq!(r.host, "host");
assert_eq!(r.path, "a:b");
}
Location::Local(_) => panic!("expected remote"),
}
}
#[test]
fn parse_at_sign_in_local_path_stays_local() {
assert!(matches!(
Location::parse("./foo@bar"),
Location::Local(_)
));
assert!(matches!(
Location::parse("C:\\Users\\alice@corp\\file"),
Location::Local(_)
));
assert!(matches!(
Location::parse("alice@host\\backup"),
Location::Local(_)
));
}
#[test]
fn parse_empty_host_not_localhost() {
let loc = Location::parse("user@:path");
match loc {
Location::Remote(r) => assert_eq!(r.host, ""),
Location::Local(_) => panic!("expected remote"),
}
let loc = Location::parse("user@:");
match loc {
Location::Remote(r) => assert_eq!(r.host, ""),
Location::Local(_) => panic!("expected remote"),
}
}
}