use std::io::Write; use camino::Utf8PathBuf; use gny::blobby::{ BinRead, BinWrite, ByteCounter, Configuration, Decode, DecodeError, Encode, EncodeError, derive::{Decode, Encode}, }; #[derive(Clone, Debug, Encode, Decode)] pub struct Response { msg: Message, body: Vec, } impl Response { pub fn new(msg: Message) -> Self { Self { msg, body: vec![] } } pub fn message(&self) -> &Message { &self.msg } pub fn with_body(mut self, body: T) -> Result { let body_len = ByteCounter::new_counter(Configuration::default()).encodable_len(&body)?; self.body.resize(body_len, 0); let mut enc = BinWrite::from_buffer(&mut self.body, Configuration::default()); enc.encode(&body)?; Ok(self) } pub fn get_body(&self) -> Result { let mut dec = BinRead::from_buffer(&self.body, Configuration::default()); dec.decode() } pub fn send(self, w: W) -> Result<(), EncodeError> { let len = ByteCounter::new_counter(Configuration::default()).encodable_len(&self)?; let mut bw = BinWrite::new(w, Configuration::default()); let count = bw.u16(len as u16)? + bw.encode(&self)?; tracing::trace!("sent {count} bytes"); Ok(()) } pub fn from_buffer(buf: &[u8]) -> Result { let mut br = BinRead::from_buffer(buf, Configuration::default()); br.decode() } } #[derive(Copy, Clone, Debug, Encode, Decode, PartialEq)] pub enum Message { GraphBounds, GeneratedGraphs, } #[derive(Copy, Clone, Debug, Encode, Decode)] pub struct GraphBounds { pub mem_upper: u64, pub mem_current: u64, pub net_upper: u64, pub cpu_upper: u8, } #[derive(Clone, Debug, Encode, Decode)] pub struct GeneratedGraphs { pub meminfo: Utf8PathBuf, pub cpuinfo: Utf8PathBuf, pub netinfo: Utf8PathBuf, pub bounds: GraphBounds, }