qlty_cloud/format/
protos.rs

1use super::Formatter;
2use prost::{bytes::BytesMut, Message};
3use std::io::Write;
4
5#[derive(Debug)]
6pub struct ProtosFormatter<T>
7where
8    T: IntoIterator,
9    T::Item: Message,
10{
11    records: T,
12}
13
14impl<T> ProtosFormatter<T>
15where
16    T: IntoIterator + Clone + 'static,
17    T::Item: Message,
18{
19    pub fn new(records: T) -> Box<dyn Formatter> {
20        Box::new(Self {
21            records: records.clone(),
22        })
23    }
24}
25
26impl<T> Formatter for ProtosFormatter<T>
27where
28    T: IntoIterator + Clone,
29    T::Item: Message,
30{
31    fn write_to(&self, writer: &mut dyn Write) -> anyhow::Result<()> {
32        let mut buffer = BytesMut::new();
33
34        for record in self.records.clone().into_iter() {
35            record.encode_length_delimited(&mut buffer).unwrap();
36        }
37
38        writer.write_all(&buffer)?;
39        Ok(())
40    }
41}
42
43#[derive(Debug)]
44pub struct ProtoFormatter<T: Message> {
45    record: T,
46}
47
48impl<T: Message + 'static> ProtoFormatter<T> {
49    pub fn new(record: T) -> Box<dyn Formatter> {
50        Box::new(Self { record })
51    }
52}
53
54impl<T: Message> Formatter for ProtoFormatter<T> {
55    fn write_to(&self, writer: &mut dyn Write) -> anyhow::Result<()> {
56        let mut buffer = BytesMut::new();
57        self.record.encode(&mut buffer)?;
58        writer.write_all(&buffer)?;
59        Ok(())
60    }
61}