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
use std::io::Write;

use vortex_error::VortexResult;

use crate::messages::{EncoderMessage, MessageEncoder};

pub struct SyncMessageWriter<W> {
    write: W,
    encoder: MessageEncoder,
}

impl<W: Write> SyncMessageWriter<W> {
    pub fn new(write: W) -> Self {
        Self {
            write,
            encoder: MessageEncoder::default(),
        }
    }

    pub fn write_message(&mut self, message: EncoderMessage) -> VortexResult<()> {
        for buffer in self.encoder.encode(message) {
            self.write.write_all(&buffer)?;
        }
        Ok(())
    }
}