1use dfir_rs::DemuxEnum;
23use crate::model::{Clock, Namespaces};
4use crate::{ClientRequest, GossipMessage, Key};
56/// 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.
11Get { key: Key, addr: A },
12/// A set request with the key, value and the address of the client.
13Set { key: Key, value: String, addr: A },
14/// A delete request with the key and the address of the client.
15Delete { key: Key, addr: A },
16}
1718impl<A> ClientRequestWithAddress<A> {
19/// Create a `ClientRequestWithAddress` from a `ClientRequest` and an address.
20pub fn from_request_and_address(request: ClientRequest, addr: A) -> Self {
21match 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}
2829/// 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.
34Gossip {
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.
41Ack {
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.
47Nack {
48 message_id: String,
49 member_id: String,
50 addr: A,
51 },
52}
5354impl<A> GossipRequestWithAddress<A> {
55/// Create a `GossipRequestWithAddress` from a `GossipMessage` and an address.
56pub fn from_request_and_address(request: GossipMessage, addr: A) -> Self {
57match 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 },
6869 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}