From c90dc69bd3dccfad43e3a2f26713543b5c876005 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sun, 2 Apr 2023 15:22:35 -0500 Subject: feat: rewrite to use Extend trait and remove tmux control --- src/unix.rs | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'src/unix.rs') diff --git a/src/unix.rs b/src/unix.rs index 5414125..402e476 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -1,19 +1,37 @@ -use crate::{Session, SessionSource}; +use std::{ + fs::File, + io::{BufRead, BufReader, Error}, +}; -pub struct HostFile; +use crate::Session; -impl SessionSource for HostFile { - type Error = &'static str; +pub struct Hosts(Vec); - type Iter = Vec; - - fn sessions(&self) -> Result { - let sessions = hostfile::parse_hostfile()? - .into_iter() - .flat_map(|h| h.names.into_iter()) - .map(Into::into) +impl Hosts { + pub fn open() -> Result { + let buf_reader = BufReader::new(File::open("/etc/hosts")?); + let inner: Vec = buf_reader + .lines() + .flatten() + .take_while(|s| !s.starts_with('#')) + .flat_map(|l| { + l.split_whitespace() + .skip(1) + .take_while(|s| !s.starts_with('#')) + .map(Session::from) + .collect::>() + }) .collect(); + Ok(Self(inner)) + } +} + +impl IntoIterator for Hosts { + type Item = Session; + + type IntoIter = std::vec::IntoIter; - Ok(sessions) + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() } } -- cgit v1.2.3-70-g09d2