gossip_kv/
util.rs

1use dfir_rs::DemuxEnum;
2
3use crate::model::{Clock, Namespaces};
4use crate::{ClientRequest, GossipMessage, Key};
5
6/// Convenience enum to represent a client request with the address of the client. Makes it
7/// possible to use `demux_enum` in the surface syntax.
8#[derive(Debug, DemuxEnum)]
9pub enum ClientRequestWithAddress<A> {
10    /// A get request with the key and the address of the client.
11    Get { key: Key, addr: A },
12    /// A set request with the key, value and the address of the client.
13    Set { key: Key, value: String, addr: A },
14    /// A delete request with the key and the address of the client.
15    Delete { key: Key, addr: A },
16}
17
18impl<A> ClientRequestWithAddress<A> {
19    /// Create a `ClientRequestWithAddress` from a `ClientRequest` and an address.
20    pub fn from_request_and_address(request: ClientRequest, addr: A) -> Self {
21        match request {
22            ClientRequest::Get { key } => Self::Get { key, addr },
23            ClientRequest::Set { key, value } => Self::Set { key, value, addr },
24            ClientRequest::Delete { key } => Self::Delete { key, addr },
25        }
26    }
27}
28
29/// Convenience enum to represent a gossip request with the address of the client. Makes it
30/// possible to use `demux_enum` in the surface syntax.
31#[derive(Debug, DemuxEnum)]
32pub enum GossipRequestWithAddress<A> {
33    /// A gossip request with the message id, writes and the address of the client.
34    Gossip {
35        message_id: String,
36        member_id: String,
37        writes: Namespaces<Clock>,
38        addr: A,
39    },
40    /// An ack request with the message id and the address of the client.
41    Ack {
42        message_id: String,
43        member_id: String,
44        addr: A,
45    },
46    /// A nack request with the message id and the address of the client.
47    Nack {
48        message_id: String,
49        member_id: String,
50        addr: A,
51    },
52}
53
54impl<A> GossipRequestWithAddress<A> {
55    /// Create a `GossipRequestWithAddress` from a `GossipMessage` and an address.
56    pub fn from_request_and_address(request: GossipMessage, addr: A) -> Self {
57        match request {
58            GossipMessage::Gossip {
59                message_id,
60                member_id,
61                writes,
62            } => Self::Gossip {
63                message_id,
64                member_id,
65                writes,
66                addr,
67            },
68
69            GossipMessage::Ack {
70                message_id,
71                member_id,
72            } => Self::Ack {
73                message_id,
74                addr,
75                member_id,
76            },
77            GossipMessage::Nack {
78                message_id,
79                member_id,
80            } => Self::Nack {
81                message_id,
82                addr,
83                member_id,
84            },
85        }
86    }
87}