summaryrefslogtreecommitdiff
path: root/common/src/msg.rs
diff options
context:
space:
mode:
authorAria <me@aria.rip>2023-09-02 21:59:29 +0100
committerAria <me@aria.rip>2023-09-02 21:59:29 +0100
commit0a0fb30472aa97ae852ad6f23b224861aa46f358 (patch)
treece401563cbc552c3a33d36afbd39934c35d08f2b /common/src/msg.rs
echo chal
Diffstat (limited to 'common/src/msg.rs')
-rw-r--r--common/src/msg.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/common/src/msg.rs b/common/src/msg.rs
new file mode 100644
index 0000000..23db171
--- /dev/null
+++ b/common/src/msg.rs
@@ -0,0 +1,50 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct Message<B> {
+ #[serde(flatten)]
+ pub header: MessageHeader,
+ pub body: MaelstromBodyOr<B>,
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct MessageHeader {
+ pub src: String,
+ #[serde(rename = "dest")]
+ pub dst: String,
+}
+
+impl MessageHeader {
+ pub fn flip(self) -> Self {
+ Self {
+ src: self.dst,
+ dst: self.src,
+ }
+ }
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(untagged)]
+pub enum MaelstromBodyOr<B> {
+ MaelstromBody {
+ #[serde(flatten)]
+ inner: MaelstromBody,
+ },
+ Other {
+ #[serde(flatten)]
+ inner: B,
+ },
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(tag = "type")]
+pub enum MaelstromBody {
+ #[serde(rename = "init")]
+ Init {
+ node_id: String,
+ node_ids: Vec<String>,
+ msg_id: usize,
+ },
+ #[serde(rename = "init_ok")]
+ InitOk { msg_id: usize, in_reply_to: usize },
+}