use std::io::{self, Write}; /// Out controls the actual output of all search results for a particular file /// to the end user. /// /// (The difference between Out and Printer is that a Printer works with /// individual search results where as Out works with search results for each /// file as a whole. For example, it knows when to print a file separator.) pub struct Out { wtr: io::BufWriter, printed: bool, file_separator: Option>, } impl Out { /// Create a new Out that writes to the wtr given. pub fn new(wtr: W) -> Out { Out { wtr: io::BufWriter::new(wtr), printed: false, file_separator: None, } } /// If set, the separator is printed between matches from different files. /// By default, no separator is printed. /// /// If sep is empty, then no file separator is printed. pub fn file_separator(mut self, sep: Vec) -> Out { self.file_separator = Some(sep); self } /// Write the search results of a single file to the underlying wtr and /// flush wtr. pub fn write(&mut self, buf: &[u8]) { if let Some(ref sep) = self.file_separator { if self.printed { let _ = self.wtr.write_all(sep); let _ = self.wtr.write_all(b"\n"); } } let _ = self.wtr.write_all(buf); let _ = self.wtr.flush(); self.printed = true; } }