summaryrefslogtreecommitdiff
path: root/common/src/msg.rs
blob: 23db17126dd5c722cc3ba2658b85e1943db6ae56 (plain)
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
43
44
45
46
47
48
49
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 },
}