summaryrefslogtreecommitdiffstats
path: root/src/writer.rs
blob: 3b9e710248c75ccf9213fe943637c27ca41f764e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::io::{self, BufWriter, LineWriter, Write};

use crate::config::Config;
use crate::writeable::Writeable;

pub trait WriteReceiver: Write {
    fn write_choice<Wa: Writeable>(
        &mut self,
        b: Wa,
        config: &Config,
        print_separator: bool,
    ) -> io::Result<()> {
        let num_bytes_written = self.write(&b.to_byte_buf())?;
        if num_bytes_written > 0 && print_separator {
            self.write_separator(config)?;
        };
        Ok(())
    }

    fn write_separator(&mut self, config: &Config) -> io::Result<()> {
        self.write(&config.output_separator).map(|_| ())
    }
}

impl<W: Write> WriteReceiver for BufWriter<W> {}

impl<W: Write> WriteReceiver for LineWriter<W> {}