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
32
33
34
35
36
37
38
39
40
41
42
use std::io;
use std::io::Write;

use thiserror::Error;

pub use crate::de::Deserialize;
pub use crate::ser::Serialize;

pub mod de;
pub mod ser;

#[cfg(feature = "parser")]
pub mod parser;

pub type Result<T> = core::result::Result<T, ProtocolError>;

pub const NODE_START: [u8; 4] = [0; 4];
pub const NODE_END: [u8; 4] = [0xFF; 4];

pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
where
    T: Serialize,
{
    let mut c = Vec::new();

    value.serialize(&mut c, 0x01)?;
    c.flush()?;

    Ok(c)
}

#[derive(Error, Debug)]
pub enum ProtocolError {
    #[error("underlying transport error")]
    Io(#[from] io::Error),
    #[error("deserialization of message failed: {0}")]
    Deserialization(String),
}

pub trait ClientType: Serialize + Deserialize + Default {
    const ID: u16;
}