use std::{ fs::File, io::{BufRead, BufReader, Error, ErrorKind}, }; use directories::UserDirs; use crate::Session; pub struct KnownHosts(Vec); impl KnownHosts { pub fn open() -> Result { let path = UserDirs::new() .ok_or(ErrorKind::NotFound)? .home_dir() .join(".ssh/known_hosts"); let file = File::open(path)?; let inner = BufReader::new(file) .lines() .flatten() .take_while(|s| !s.starts_with('#')) .filter_map(|l| l.split_whitespace().next().map(str::to_owned)) .map(Session::discover) .collect(); Ok(Self(inner)) } } impl IntoIterator for KnownHosts { type Item = Session; type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } }