diff options
author | Toby Vincent <tobyv13@gmail.com> | 2023-03-19 21:38:46 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2023-03-19 21:38:46 -0500 |
commit | 82598a3adc9d950c34346ccd0128d6ef78bbb437 (patch) | |
tree | f98fe22afd44a759556c1bb68c7f73d8593c8d6e /src | |
parent | 928995171b8f3f19df96cfd5870f7e88f11c3978 (diff) |
fix: impl From<String> for Session for `Discovered` sessions
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 16 | ||||
-rw-r--r-- | src/ssh.rs | 14 |
2 files changed, 17 insertions, 13 deletions
@@ -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<String> 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, + } + } +} @@ -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<Session>; fn sessions(&self) -> Result<Self::Iter, Self::Error> { - 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 |