diff options
author | Toby Vincent <tobyv13@gmail.com> | 2023-04-02 15:22:35 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2023-04-02 15:23:02 -0500 |
commit | c90dc69bd3dccfad43e3a2f26713543b5c876005 (patch) | |
tree | 694676106b7ae94ddf7179a05a061cfa2056d965 /src/unix.rs | |
parent | e11e8bbf14be8f84b57013e4ace1e61071853c12 (diff) |
feat: rewrite to use Extend trait and remove tmux control
Diffstat (limited to 'src/unix.rs')
-rw-r--r-- | src/unix.rs | 42 |
1 files changed, 30 insertions, 12 deletions
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<Session>); - type Iter = Vec<Session>; - - fn sessions(&self) -> Result<Self::Iter, Self::Error> { - let sessions = hostfile::parse_hostfile()? - .into_iter() - .flat_map(|h| h.names.into_iter()) - .map(Into::into) +impl Hosts { + pub fn open() -> Result<Self, Error> { + let buf_reader = BufReader::new(File::open("/etc/hosts")?); + let inner: Vec<Session> = 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::<Vec<_>>() + }) .collect(); + Ok(Self(inner)) + } +} + +impl IntoIterator for Hosts { + type Item = Session; + + type IntoIter = std::vec::IntoIter<Self::Item>; - Ok(sessions) + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() } } |