summaryrefslogtreecommitdiffstats
path: root/src/writer.rs
blob: 00a93a1715260291e3e79a8e59296f0272932e0e (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
28
29
30
31
use std::io::{BufWriter, Write};

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

pub trait WriteReceiver {
    fn write_choice<Wa: Writeable>(&mut self, b: Wa, config: &Config, print_separator: bool);
    fn write_separator(&mut self, config: &Config);
}

impl<W: Write> WriteReceiver for BufWriter<W> {
    fn write_choice<Wa: Writeable>(&mut self, b: Wa, config: &Config, print_separator: bool) {
        let num_bytes_written = match self.write(&b.to_byte_buf()) {
            Ok(x) => x,
            Err(e) => {
                eprintln!("Failed to write to output: {}", e);
                0
            }
        };
        if num_bytes_written > 0 && print_separator {
            self.write_separator(config);
        };
    }

    fn write_separator(&mut self, config: &Config) {
        match self.write(&config.output_separator) {
            Ok(_) => (),
            Err(e) => eprintln!("Failed to write to output: {}", e),
        }
    }
}