40 lines
842 B
Rust
40 lines
842 B
Rust
|
use serde::{Deserialize, Serialize};
|
||
|
|
||
|
#[derive(Serialize, Deserialize)]
|
||
|
pub struct Message {
|
||
|
pub init: Option<Init>,
|
||
|
pub segment: Option<Segment>,
|
||
|
}
|
||
|
|
||
|
#[derive(Serialize, Deserialize)]
|
||
|
pub struct Init {
|
||
|
pub id: String,
|
||
|
}
|
||
|
|
||
|
#[derive(Serialize, Deserialize)]
|
||
|
pub struct Segment {
|
||
|
pub init: String,
|
||
|
pub timestamp: u64,
|
||
|
}
|
||
|
|
||
|
impl Message {
|
||
|
pub fn new() -> Self {
|
||
|
Message {
|
||
|
init: None,
|
||
|
segment: None,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn serialize(&self) -> anyhow::Result<Vec<u8>> {
|
||
|
let str = serde_json::to_string(self)?;
|
||
|
let bytes = str.as_bytes();
|
||
|
let size = bytes.len() + 8;
|
||
|
|
||
|
let mut out = Vec::with_capacity(size);
|
||
|
out.extend_from_slice(b"warp");
|
||
|
out.extend_from_slice(&size.to_be_bytes());
|
||
|
out.extend_from_slice(bytes);
|
||
|
|
||
|
Ok(out)
|
||
|
}
|
||
|
}
|