use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct JumpHost {
pub user: Option<String>,
pub host: String,
pub port: Option<u16>,
}
impl JumpHost {
pub fn new(host: String, user: Option<String>, port: Option<u16>) -> Self {
Self { user, host, port }
}
pub fn effective_user(&self) -> String {
self.user.clone().unwrap_or_else(whoami::username)
}
pub fn effective_port(&self) -> u16 {
self.port.unwrap_or(22)
}
pub fn to_connection_string(&self) -> String {
match (&self.user, &self.port) {
(Some(user), Some(port)) => format!("{}@{}:{}", user, self.host, port),
(Some(user), None) => format!("{}@{}", user, self.host),
(None, Some(port)) => format!("{}:{}", self.host, port),
(None, None) => self.host.clone(),
}
}
}
impl fmt::Display for JumpHost {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_connection_string())
}
}