summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2023-03-19 21:38:46 -0500
committerToby Vincent <tobyv13@gmail.com>2023-03-19 21:38:46 -0500
commit82598a3adc9d950c34346ccd0128d6ef78bbb437 (patch)
treef98fe22afd44a759556c1bb68c7f73d8593c8d6e
parent928995171b8f3f19df96cfd5870f7e88f11c3978 (diff)
fix: impl From<String> for Session for `Discovered` sessions
-rw-r--r--src/lib.rs16
-rw-r--r--src/ssh.rs14
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<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,
+ }
+ }
+}
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<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