pub mod gossip;
pub mod sampling;
use std::error::Error;
use serde::{Deserialize, Serialize};
pub const MASK_MESSAGE_PROTOCOL: u8 = 0xF0; pub const MESSAGE_PROTOCOL_SAMPLING_MESSAGE: u8 = 0x10; pub const MESSAGE_PROTOCOL_HEADER_MESSAGE: u8 = 0x20; pub const MESSAGE_PROTOCOL_CONTENT_MESSAGE: u8 = 0x40; pub const MESSAGE_PROTOCOL_NOOP_MESSAGE: u8 = 0x80;
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum MessageType {
Request = 1,
Response = 2,
}
pub trait Message {
fn protocol(&self) -> u8;
fn as_bytes(&self) -> Result<Vec<u8>, Box<dyn Error>>
where Self: Serialize
{
match serde_cbor::to_vec(&self) {
Ok(bytes) => Ok(bytes),
Err(e) => Err(e)?,
}
}
fn from_bytes<'a>(bytes: &'a [u8]) -> Result<Self, Box<dyn Error>>
where Self: Sized + Deserialize<'a>
{
match serde_cbor::from_slice::<Self>(bytes) {
Ok(m) => Ok(m),
Err(e) => Err(e)?
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NoopMessage;
impl Message for NoopMessage {
fn protocol(&self) -> u8 {
MESSAGE_PROTOCOL_NOOP_MESSAGE
}
}