Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace parse-hosts crate with 10 lines of code. #19411

Merged
merged 1 commit into from Nov 28, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Replace parse-hosts crate with 10 lines of code.

This removes 3927 lines of Rust code in 6 crates from the dependency graph:
parse-hosts, multistr, bow, extra-default, len-trait, and push-trait.

One of these crates doesn’t build in today’s Nightly:
rust-lang/rust#46328
  • Loading branch information
SimonSapin committed Nov 28, 2017
commit eb3652b3c6ac8f0c27f75e259a37f81089911ba6

Some generated files are not rendered by default. Learn more.

@@ -28,7 +28,6 @@ mime_guess = "1.8.0"
msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
openssl = "0.9"
parse-hosts = "0.5.0"
profile_traits = {path = "../profile_traits"}
serde = "1.0"
serde_json = "1.0"
@@ -2,13 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use parse_hosts::HostsFile;
use std::borrow::Cow;
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{BufReader, Read};
use std::net::IpAddr;
use std::net::{IpAddr, Ipv4Addr};
use std::sync::Mutex;

lazy_static! {
@@ -32,10 +31,17 @@ pub fn replace_host_table(table: HashMap<String, IpAddr>) {
}

pub fn parse_hostsfile(hostsfile_content: &str) -> HashMap<String, IpAddr> {
HostsFile::read_buffered(hostsfile_content.as_bytes())
.pairs()
.filter_map(Result::ok)
.collect()
hostsfile_content.lines().filter_map(|line| {
let mut iter = line.split('#').next().unwrap().split_whitespace();
Some((iter.next()?.parse().ok()?, iter))
}).flat_map(|(ip, hosts)| {
hosts.filter(|host| {
let invalid = ['\0', '\t', '\n', '\r', ' ', '#', '%', '/', ':', '?', '@', '[', '\\', ']'];
host.parse::<Ipv4Addr>().is_err() && !host.contains(&invalid[..])
}).map(move |host| {
(host.to_owned(), ip)
})
}).collect()
}

pub fn replace_host(host: &str) -> Cow<str> {
@@ -24,7 +24,6 @@ extern crate mime_guess;
extern crate msg;
extern crate net_traits;
extern crate openssl;
extern crate parse_hosts;
extern crate profile_traits;
#[macro_use] extern crate serde;
extern crate serde_json;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.