zusi_protocol/
lib.rs

1use std::io;
2use std::io::Write;
3
4use thiserror::Error;
5
6pub use crate::de::Deserialize;
7pub use crate::ser::Serialize;
8
9#[cfg(feature = "derive")]
10pub use zusi_protocol_derive::{Deserialize, Serialize};
11
12pub mod de;
13pub mod ser;
14
15#[cfg(feature = "parser")]
16pub mod parser;
17
18pub type Result<T> = core::result::Result<T, ProtocolError>;
19
20pub const NODE_START: [u8; 4] = [0; 4];
21pub const NODE_END: [u8; 4] = [0xFF; 4];
22
23pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
24where
25    T: Serialize,
26{
27    let mut c = Vec::new();
28
29    value.serialize(&mut c, 0x01)?;
30    c.flush()?;
31
32    Ok(c)
33}
34
35#[derive(Error, Debug)]
36pub enum ProtocolError {
37    #[error("underlying transport error")]
38    Io(#[from] io::Error),
39    #[error("deserialization of message failed: {0}")]
40    Deserialization(String),
41}
42
43pub trait ClientType: Serialize + Deserialize + Default {
44    const ID: u16;
45}