Skip to main content

HostsBuilder

Struct HostsBuilder 

Source
pub struct HostsBuilder { /* private fields */ }
Expand description

HostsBuilder manages a section of /etc/hosts file that contains a list of IP to hostname mappings. A hosts file can have multiple sections that are distinguished by tag names.

§Examples

use hostsfile::{HostsBuilder, Result};
let mut hosts = HostsBuilder::new("dns");
hosts.add_hostname("8.8.8.8".parse().unwrap(), "google-dns1");
hosts.add_hostname("8.8.4.4".parse().unwrap(), "google-dns2");
hosts.write_to("/tmp/hosts")?;

/tmp/hosts will have a section:

# DO NOT EDIT dns BEGIN
8.8.8.8 google-dns1
8.8.4.4 google-dns2
# DO NOT EDIT dns END

Another run of HostsBuilder with the same tag name overrides the section.

use hostsfile::{HostsBuilder, Result};
let mut hosts = HostsBuilder::new("dns");
hosts.add_hostnames("1.1.1.1".parse().unwrap(), &["cloudflare-dns", "apnic-dns"]);
hosts.write_to("/tmp/hosts")?;

/tmp/hosts will have a section:

# DO NOT EDIT dns BEGIN
1.1.1.1 cloudflare-dns apnic-dns
# DO NOT EDIT dns END

On Windows the host file format is slightly different in this case:

# DO NOT EDIT dns BEGIN
1.1.1.1 cloudflare-dns
1.1.1.1 apnic-dns
# DO NOT EDIT dns END

Implementations§

Source§

impl HostsBuilder

Source

pub fn new<S: Into<String>>(tag: S) -> Self

Creates a new HostsBuilder with the given tag name. It corresponds to a section in the hosts file containing a list of IP to hostname mappings.

Source

pub fn add_hostname<S: ToString>(&mut self, ip: IpAddr, hostname: S)

Adds a mapping of ip to hostname. If there hostnames associated with the IP already, the hostname will be appended to the list.

Source

pub fn add_hostnames<I: IntoIterator<Item = impl ToString>>( &mut self, ip: IpAddr, hostnames: I, )

Adds a mapping of ip to a list of hostnames. If there hostnames associated with the IP already, the new hostnames will be appended to the list.

Source

pub fn write(&self) -> Result<bool>

Inserts a new section to the system’s default hosts file. If there is a section with the same tag name already, it will be replaced with the new list instead. Returns true if the hosts file has changed.

Source

pub fn default_path() -> Result<PathBuf>

Returns the default hosts path based on the current OS.

Source

pub fn get_temp_path(hosts_path: &Path) -> Result<PathBuf>

Source

pub fn write_to<P: AsRef<Path>>(&self, hosts_path: P) -> Result<bool>

Inserts a new section to the specified hosts file. If there is a section with the same tag name already, it will be replaced with the new list instead.

hosts_path is the full path to write to, including the filename.

On Windows, the format of one hostname per line will be used, all other systems will use the same format as Unix and Unix-like systems (i.e. allow multiple hostnames per line).

Returns true if the hosts file has changed.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.