From 82598a3adc9d950c34346ccd0128d6ef78bbb437 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sun, 19 Mar 2023 21:38:46 -0500 Subject: fix: impl From for Session for `Discovered` sessions --- src/lib.rs | 16 +++++++++++++++- src/ssh.rs | 14 ++------------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4bb2310..d9a9862 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ use std::{ collections::{hash_map::Entry, HashMap}, fmt::Display, iter::IntoIterator, - time::Duration, + time::{Duration, SystemTime, UNIX_EPOCH}, }; use serde::{Deserialize, Serialize}; @@ -84,3 +84,17 @@ impl Display for Session { write!(f, "{}", self.name) } } + +impl From for Session { + fn from(name: String) -> Self { + let timestamp = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Current time is pre-epoch. (Time traveler?)"); + + Self { + state: State::Discovered, + timestamp, + name, + } + } +} diff --git a/src/ssh.rs b/src/ssh.rs index 29c6de3..4acbe61 100644 --- a/src/ssh.rs +++ b/src/ssh.rs @@ -1,9 +1,7 @@ -use std::time::{SystemTime, UNIX_EPOCH}; - use directories::UserDirs; use ssh2::KnownHostFileKind; -use crate::{Session, SessionSource, State}; +use crate::{Session, SessionSource}; impl SessionSource for ssh2::Session { type Error = ssh2::Error; @@ -11,10 +9,6 @@ impl SessionSource for ssh2::Session { type Iter = Vec; fn sessions(&self) -> Result { - let timestamp = SystemTime::now() - .duration_since(UNIX_EPOCH) - .expect("Current time is pre-epoch. (Time traveler?)"); - let mut known_hosts = self.known_hosts()?; let file = UserDirs::new() @@ -28,11 +22,7 @@ impl SessionSource for ssh2::Session { .hosts()? .into_iter() .filter_map(|h| match h.name() { - Some(name) => Some(Session { - state: State::Discovered, - timestamp, - name: name.to_owned(), - }), + Some(name) => Some(name.to_owned().into()), None => { tracing::warn!("Invalid host: No plain text host name exists"); None -- cgit v1.2.3-70-g09d2