summaryrefslogtreecommitdiffstats
path: root/src/unix.rs
blob: 402e476ad8a77ece7781ecd389cf1dd3b8710c0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use std::{
    fs::File,
    io::{BufRead, BufReader, Error},
};

use crate::Session;

pub struct Hosts(Vec<Session>);

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>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}