diff options
author | Toby Vincent <tobyv13@gmail.com> | 2023-04-03 19:12:14 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2023-04-03 19:12:14 -0500 |
commit | c94960b49f463b3c4a7da9fb1b6b2c122f7dd125 (patch) | |
tree | 6be101dd21b61775313e04b6c695629b183f48b4 /src/unix.rs | |
parent | 8a6631053a48a64f0c3b21ec0d92b6b687de9638 (diff) |
refactor: clean up logic and impl trait for writing sessions
Diffstat (limited to 'src/unix.rs')
-rw-r--r-- | src/unix.rs | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/src/unix.rs b/src/unix.rs index 402e476..d549b63 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -9,20 +9,24 @@ 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 + File::open("/etc/hosts").map(Self::parse_file).map(Self) + } + + fn parse_file(file: File) -> Vec<Session> { + BufReader::new(file) .lines() .flatten() + .filter_map(Self::parse_line) + .collect() + } + + fn parse_line(line: String) -> Option<Session> { + line.split_whitespace() .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)) + .last() + // Skip BOM + .filter(|&s| s != "\u{feff}") + .map(Session::discover) } } |