Initial project
This commit is contained in:
commit
10a732af07
|
|
@ -0,0 +1,3 @@
|
||||||
|
/target
|
||||||
|
.vscode/
|
||||||
|
Cargo.lock
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
[package]
|
||||||
|
name = "tourbillon"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
color-eyre = "0.6.2"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
mod protocol;
|
||||||
|
use protocol::message::Message;
|
||||||
|
|
||||||
|
fn main() -> color_eyre::Result<()> {
|
||||||
|
color_eyre::install()?;
|
||||||
|
|
||||||
|
println!("Hello, world!");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod message;
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct Message {
|
||||||
|
src: String,
|
||||||
|
#[serde(rename = "dest")]
|
||||||
|
dst: String,
|
||||||
|
body: MessageBody,
|
||||||
|
}
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct MessageBody {
|
||||||
|
#[serde(rename = "msg_id")]
|
||||||
|
id: Option<u64>,
|
||||||
|
in_reply_to: Option<u64>,
|
||||||
|
|
||||||
|
#[serde(flatten)]
|
||||||
|
data: MessagePayload,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, PartialEq)]
|
||||||
|
#[serde(tag = "type")]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum MessagePayload {
|
||||||
|
Init {
|
||||||
|
node_id: String,
|
||||||
|
#[serde(rename = "node_ids")]
|
||||||
|
nodes: Vec<String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn protocol_decode_init() -> color_eyre::Result<()> {
|
||||||
|
let json_input = r###"{
|
||||||
|
"src": "c1",
|
||||||
|
"dest": "n3",
|
||||||
|
"body": {
|
||||||
|
"type": "init",
|
||||||
|
"msg_id": 1,
|
||||||
|
"node_id": "n3",
|
||||||
|
"node_ids": ["n1", "n2", "n3"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"###;
|
||||||
|
|
||||||
|
let msg = serde_json::from_str::<Message>(json_input)?;
|
||||||
|
|
||||||
|
assert!(matches!(msg.body.data, MessagePayload::Init { .. }));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue