use std::{ fs::File, io::{BufRead, BufReader, Error}, }; use crate::Session; pub struct Hosts(Vec); impl Hosts { pub fn open() -> Result { File::open("/etc/hosts").map(Self::parse_file).map(Self) } fn parse_file(file: File) -> Vec { BufReader::new(file) .lines() .flatten() .filter_map(Self::parse_line) .collect() } fn parse_line(line: String) -> Option { line.split_whitespace() .take_while(|s| !s.starts_with('#')) .last() // Skip BOM .filter(|&s| s != "\u{feff}") .map(Session::discover) } } impl IntoIterator for Hosts { type Item = Session; type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } }