pub fn write_to<P: AsRef<Path>>(&self, hosts_path: P) -> io::Result<()> {
        let hosts_path = hosts_path.as_ref();
        let begin_marker = format!("# DO NOT EDIT {} BEGIN", &self.tag);
        let end_marker = format!("# DO NOT EDIT {} END", &self.tag);

        let hosts_file = OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .open(hosts_path)?;
        let mut lines = BufReader::new(hosts_file)
            .lines()
            .map(|line| line.unwrap())
            .collect::<Vec<_>>();

        let begin = lines.iter().position(|line| line.trim() == begin_marker);
        let end = lines.iter().position(|line| line.trim() == end_marker);

        let insert = match (begin, end) {
            (Some(begin), Some(end)) => {
                lines.drain(begin..end + 1);
                begin
            },
            (None, None) => {
                // Insert a blank line before a new section.
                if let Some(last_line) = lines.iter().last() {
                    if !last_line.is_empty() {
                        lines.push("".to_string());
                    }
                }
                lines.len()
            },
            _ => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("start or end marker missing in {:?}", &hosts_path),
                ));
            },
        };

        let mut s = vec![];

        for line in &lines[..insert] {
            writeln!(&mut s, "{}", line)?;
        }
        if !self.hostname_map.is_empty() {
            writeln!(&mut s, "{}", begin_marker)?;
            for (ip, hostnames) in &self.hostname_map {
                if cfg!(windows) {
                    // windows only allows one hostname per line
                    for hostname in hostnames {
                        writeln!(&mut s, "{} {}", ip, hostname)?;
                    }
                } else {
                    // assume the same format as Unix
                    writeln!(&mut s, "{} {}", ip, hostnames.join(" "))?;
                }
            }
            writeln!(&mut s, "{}", end_marker)?;
        }
        for line in &lines[insert..] {
            writeln!(&mut s, "{}", line)?;
        }

        OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .truncate(true)
            .open(hosts_path)?
            .write_all(&s)?;

        Ok(())
    }
/// 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.
///
/// 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).
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.

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 .
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 . 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 .
pub fn write_to<P: AsRef<Path>>(&self, hosts_path: P) -> io::Result<()> {
        let hosts_path = hosts_path.as_ref();
        let begin_marker = format!("# DO NOT EDIT {} BEGIN", &self.tag);
        let end_marker = format!("# DO NOT EDIT {} END", &self.tag);
        let hosts_file = OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .open(hosts_path)?;
        let mut lines = BufReader::new(hosts_file)
            .lines()
            .map(|line| line.unwrap())
            .collect::<Vec<_>>();
        let begin = lines.iter().position(|line| line.trim() == begin_marker);
        let end = lines.iter().position(|line| line.trim() == end_marker);
        let insert = match (begin, end) {
            (Some(begin), Some(end)) => {
                lines.drain(begin..end + 1);
                begin
            },
            (None, None) => {
                // Insert a blank line before a new section.
                if let Some(last_line) = lines.iter().last() {
                    if !last_line.is_empty() {
                        lines.push("".to_string());
                    }
                }
                lines.len()
            },
            _ => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("start or end marker missing in {:?}", &hosts_path),
                ));
            },
        };
        let mut s = vec![];
        for line in &lines[..insert] {
            writeln!(&mut s, "{}", line)?;
        }
        if !self.hostname_map.is_empty() {
            writeln!(&mut s, "{}", begin_marker)?;
            for (ip, hostnames) in &self.hostname_map {
                if cfg!(windows) {
                    // windows only allows one hostname per line
                    for hostname in hostnames {
                        writeln!(&mut s, "{} {}", ip, hostname)?;
                    }
                } else {
                    // assume the same format as Unix
                    writeln!(&mut s, "{} {}", ip, hostnames.join(" "))?;
                }
            }
            writeln!(&mut s, "{}", end_marker)?;
        }
        for line in &lines[insert..] {
            writeln!(&mut s, "{}", line)?;
        }
        OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .truncate(true)
            .open(hosts_path)?
            .write_all(&s)?;
        Ok(())
    }
pub fn write_to < P : AsRef < Path > > ( & self , hosts_path : P ) -> io :: Result < ( ) > { let hosts_path = hosts_path . as_ref ( ) ; let begin_marker = format ! ( " " self tag ) ; let end_marker = format ! ( " " self tag ) ; let hosts_file = OpenOptions :: new ( ) . create ( true ) . read ( true ) . write ( true ) . open ( hosts_path ) ? ; let mut lines = BufReader :: new ( hosts_file ) . lines ( ) . map ( | line | line . unwrap ( ) ) . collect :: < Vec < _ > > ( ) ; let begin = lines . iter ( ) . position ( | line | line . trim ( ) == begin_marker ) ; let end = lines . iter ( ) . position ( | line | line . trim ( ) == end_marker ) ; let insert = match ( begin , end ) { ( Some ( begin ) , Some ( end ) ) => { lines . drain ( begin .. end + 1 ) ; begin } , ( None , None ) => { // Insert a blank line before a new section. if let Some ( last_line ) = lines . iter ( ) . last ( ) { if ! last_line . is_empty ( ) { lines . push ( " " . to_string ( ) ) ; } } lines . len ( ) } , _ => { return Err ( io :: Error :: new ( io :: ErrorKind :: InvalidData , format ! ( " " hosts_path ) , ) ) ; } , } ; let mut s = vec ! [ ] ; for line in & lines [ .. insert ] { writeln ! ( mut s " " line ) ? ; } if ! self . hostname_map . is_empty ( ) { writeln ! ( mut s " " begin_marker ) ? ; for ( ip , hostnames ) in & self . hostname_map { if cfg ! ( windows ) { // windows only allows one hostname per line for hostname in hostnames { writeln ! ( mut s " " ip hostname ) ? ; } } else { // assume the same format as Unix writeln ! ( mut s " " ip hostnames join ( " " ) ) ? ; } } writeln ! ( mut s " " end_marker ) ? ; } for line in & lines [ insert .. ] { writeln ! ( mut s " " line ) ? ; } OpenOptions :: new ( ) . create ( true ) . read ( true ) . write ( true ) . truncate ( true ) . open ( hosts_path ) ? . write_all ( & s ) ? ; Ok ( ( ) ) }
Inserts a new section to the specified hosts file.
Inserts a new section to the specified hosts file .
&self
hosts_path
P