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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use memberlist_core::{
  bytes::Bytes,
  transport::{Id, Node, Transformable},
  CheapClone,
};
use ruserf_types::{
  FilterTransformError, JoinMessage, LeaveMessage, Member, MessageType, NodeTransformError,
  PushPullMessage, QueryMessage, QueryResponseMessage, SerfMessageTransformError,
  TagsTransformError, UserEventMessage,
};

use crate::{
  coordinate::{Coordinate, CoordinateTransformError},
  types::{AsMessageRef, Filter, SerfMessage, Tags, UnknownMessageType},
};

/// A delegate for encoding and decoding.
pub trait TransformDelegate: Send + Sync + 'static {
  /// The error type for the transformation.
  type Error: std::error::Error + From<UnknownMessageType> + Send + Sync + 'static;
  /// The Id type.
  type Id: Id;
  /// The Address type.
  type Address: CheapClone + Send + Sync + 'static;

  /// Encodes the filter into bytes.
  fn encode_filter(filter: &Filter<Self::Id>) -> Result<Bytes, Self::Error>;

  /// Decodes the filter from the given bytes, returning the number of bytes consumed and the filter.
  fn decode_filter(bytes: &[u8]) -> Result<(usize, Filter<Self::Id>), Self::Error>;

  /// Returns the encoded length of the node.
  fn node_encoded_len(node: &Node<Self::Id, Self::Address>) -> usize;

  /// Encodes the node into the given buffer, returning the number of bytes written.
  fn encode_node(
    node: &Node<Self::Id, Self::Address>,
    dst: &mut [u8],
  ) -> Result<usize, Self::Error>;

  /// Decodes [`Node`] from the given bytes, returning the number of bytes consumed and the node.
  fn decode_node(
    bytes: impl AsRef<[u8]>,
  ) -> Result<(usize, Node<Self::Id, Self::Address>), Self::Error>;

  /// Returns the encoded length of the id.
  fn id_encoded_len(id: &Self::Id) -> usize;

  /// Encodes the id into the given buffer, returning the number of bytes written.
  fn encode_id(id: &Self::Id, dst: &mut [u8]) -> Result<usize, Self::Error>;

  /// Decodes the id from the given bytes, returning the number of bytes consumed and the id.
  fn decode_id(bytes: &[u8]) -> Result<(usize, Self::Id), Self::Error>;

  /// Returns the encoded length of the address.
  fn address_encoded_len(address: &Self::Address) -> usize;

  /// Encodes the address into the given buffer, returning the number of bytes written.
  fn encode_address(address: &Self::Address, dst: &mut [u8]) -> Result<usize, Self::Error>;

  /// Decodes the address from the given bytes, returning the number of bytes consumed and the address.
  fn decode_address(bytes: &[u8]) -> Result<(usize, Self::Address), Self::Error>;

  /// Encoded length of the coordinate.
  fn coordinate_encoded_len(coordinate: &Coordinate) -> usize;

  /// Encodes the coordinate into the given buffer, returning the number of bytes written.
  fn encode_coordinate(coordinate: &Coordinate, dst: &mut [u8]) -> Result<usize, Self::Error>;

  /// Decodes the coordinate from the given bytes, returning the number of bytes consumed and the coordinate.
  fn decode_coordinate(bytes: &[u8]) -> Result<(usize, Coordinate), Self::Error>;

  /// Encoded length of the tags.
  fn tags_encoded_len(tags: &Tags) -> usize;

  /// Encodes the tags into the given buffer, returning the number of bytes written.
  fn encode_tags(tags: &Tags, dst: &mut [u8]) -> Result<usize, Self::Error>;

  /// Decodes the tags from the given bytes, returning the number of bytes consumed and the tags.
  fn decode_tags(bytes: &[u8]) -> Result<(usize, Tags), Self::Error>;

  /// Encoded length of the message.
  fn message_encoded_len(msg: impl AsMessageRef<Self::Id, Self::Address>) -> usize;

  /// Encodes the message into the given buffer, returning the number of bytes written.
  ///
  /// **NOTE**:
  ///
  /// 1. The buffer must be large enough to hold the encoded message.
  /// The length of the buffer can be obtained by calling [`TransformDelegate::message_encoded_len`].
  /// 2. A message type byte will be automatically prepended to the buffer,
  /// so users do not need to encode the message type byte by themselves.
  fn encode_message(
    msg: impl AsMessageRef<Self::Id, Self::Address>,
    dst: impl AsMut<[u8]>,
  ) -> Result<usize, Self::Error>;

  /// Decodes the message from the given bytes, returning the number of bytes consumed and the message.
  fn decode_message(
    ty: MessageType,
    bytes: impl AsRef<[u8]>,
  ) -> Result<(usize, SerfMessage<Self::Id, Self::Address>), Self::Error>;
}

/// The error type for the LPE transformation.
#[derive(thiserror::Error)]
pub enum LpeTransformError<I, A>
where
  I: Transformable + core::hash::Hash + Eq,
  A: Transformable + core::hash::Hash + Eq,
{
  /// Id transformation error.
  #[error(transparent)]
  Id(<I as Transformable>::Error),
  /// Address transformation error.
  #[error(transparent)]
  Address(<A as Transformable>::Error),
  /// Coordinate transformation error.
  #[error(transparent)]
  Coordinate(#[from] CoordinateTransformError),
  /// Node transformation error.
  #[error(transparent)]
  Node(#[from] NodeTransformError<I, A>),
  /// Filter transformation error.
  #[error(transparent)]
  Filter(#[from] FilterTransformError<I>),
  /// Tags transformation error.
  #[error(transparent)]
  Tags(#[from] TagsTransformError),
  /// Serf message transformation error.
  #[error(transparent)]
  Message(#[from] SerfMessageTransformError<I, A>),
  /// Unknown message type error.
  #[error(transparent)]
  UnknownMessage(#[from] UnknownMessageType),
  /// Unexpected relay message.
  #[error("unexpected relay message")]
  UnexpectedRelayMessage,
}

impl<I, A> core::fmt::Debug for LpeTransformError<I, A>
where
  I: Transformable + core::hash::Hash + Eq,
  A: Transformable + core::hash::Hash + Eq,
{
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    write!(f, "{}", self)
  }
}

/// A length-prefixed encoding [`TransformDelegate`] implementation
pub struct LpeTransfromDelegate<I, A>(std::marker::PhantomData<(I, A)>);

impl<I, A> Default for LpeTransfromDelegate<I, A> {
  fn default() -> Self {
    Self(Default::default())
  }
}

impl<I, A> Clone for LpeTransfromDelegate<I, A> {
  fn clone(&self) -> Self {
    *self
  }
}

impl<I, A> Copy for LpeTransfromDelegate<I, A> {}

impl<I, A> TransformDelegate for LpeTransfromDelegate<I, A>
where
  I: Id,
  A: Transformable + CheapClone + core::hash::Hash + Eq + Send + Sync + 'static,
{
  type Error = LpeTransformError<Self::Id, Self::Address>;
  type Id = I;
  type Address = A;

  fn encode_filter(filter: &Filter<Self::Id>) -> Result<Bytes, Self::Error> {
    filter
      .encode_to_vec()
      .map(Bytes::from)
      .map_err(Self::Error::Filter)
  }

  fn decode_filter(bytes: &[u8]) -> Result<(usize, Filter<Self::Id>), Self::Error> {
    Filter::decode(bytes).map_err(Self::Error::Filter)
  }

  fn node_encoded_len(node: &Node<Self::Id, Self::Address>) -> usize {
    Transformable::encoded_len(node)
  }

  fn encode_node(
    node: &Node<Self::Id, Self::Address>,
    dst: &mut [u8],
  ) -> Result<usize, Self::Error> {
    Transformable::encode(node, dst).map_err(Self::Error::Node)
  }

  fn decode_node(
    bytes: impl AsRef<[u8]>,
  ) -> Result<(usize, Node<Self::Id, Self::Address>), Self::Error> {
    Transformable::decode(bytes.as_ref()).map_err(Self::Error::Node)
  }

  fn id_encoded_len(id: &Self::Id) -> usize {
    Transformable::encoded_len(id)
  }

  fn encode_id(id: &Self::Id, dst: &mut [u8]) -> Result<usize, Self::Error> {
    Transformable::encode(id, dst).map_err(Self::Error::Id)
  }

  fn decode_id(bytes: &[u8]) -> Result<(usize, Self::Id), Self::Error> {
    Transformable::decode(bytes).map_err(Self::Error::Id)
  }

  fn address_encoded_len(address: &Self::Address) -> usize {
    Transformable::encoded_len(address)
  }

  fn encode_address(address: &Self::Address, dst: &mut [u8]) -> Result<usize, Self::Error> {
    Transformable::encode(address, dst).map_err(Self::Error::Address)
  }

  fn decode_address(bytes: &[u8]) -> Result<(usize, Self::Address), Self::Error> {
    Transformable::decode(bytes).map_err(Self::Error::Address)
  }

  fn coordinate_encoded_len(coordinate: &Coordinate) -> usize {
    Transformable::encoded_len(coordinate)
  }

  fn encode_coordinate(coordinate: &Coordinate, dst: &mut [u8]) -> Result<usize, Self::Error> {
    Transformable::encode(coordinate, dst).map_err(Self::Error::Coordinate)
  }

  fn decode_coordinate(bytes: &[u8]) -> Result<(usize, Coordinate), Self::Error> {
    Transformable::decode(bytes).map_err(Self::Error::Coordinate)
  }

  fn tags_encoded_len(tags: &Tags) -> usize {
    Transformable::encoded_len(tags)
  }

  fn encode_tags(tags: &Tags, dst: &mut [u8]) -> Result<usize, Self::Error> {
    Transformable::encode(tags, dst).map_err(Self::Error::Tags)
  }

  fn decode_tags(bytes: &[u8]) -> Result<(usize, Tags), Self::Error> {
    Transformable::decode(bytes).map_err(Self::Error::Tags)
  }

  fn message_encoded_len(msg: impl AsMessageRef<Self::Id, Self::Address>) -> usize {
    let msg = msg.as_message_ref();
    ruserf_types::Encodable::encoded_len(&msg)
  }

  fn encode_message(
    msg: impl AsMessageRef<Self::Id, Self::Address>,
    mut dst: impl AsMut<[u8]>,
  ) -> Result<usize, Self::Error> {
    let msg = msg.as_message_ref();
    ruserf_types::Encodable::encode(&msg, dst.as_mut()).map_err(Into::into)
  }

  fn decode_message(
    ty: MessageType,
    bytes: impl AsRef<[u8]>,
  ) -> Result<(usize, SerfMessage<Self::Id, Self::Address>), Self::Error> {
    match ty {
      MessageType::Leave => LeaveMessage::decode(bytes.as_ref())
        .map(|(n, m)| (n, SerfMessage::Leave(m)))
        .map_err(|e| Self::Error::Message(e.into())),
      MessageType::Join => JoinMessage::decode(bytes.as_ref())
        .map(|(n, m)| (n, SerfMessage::Join(m)))
        .map_err(|e| Self::Error::Message(e.into())),
      MessageType::PushPull => PushPullMessage::decode(bytes.as_ref())
        .map(|(n, m)| (n, SerfMessage::PushPull(m)))
        .map_err(|e| Self::Error::Message(e.into())),
      MessageType::UserEvent => UserEventMessage::decode(bytes.as_ref())
        .map(|(n, m)| (n, SerfMessage::UserEvent(m)))
        .map_err(|e| Self::Error::Message(e.into())),
      MessageType::Query => QueryMessage::decode(bytes.as_ref())
        .map(|(n, m)| (n, SerfMessage::Query(m)))
        .map_err(|e| Self::Error::Message(e.into())),
      MessageType::QueryResponse => QueryResponseMessage::decode(bytes.as_ref())
        .map(|(n, m)| (n, SerfMessage::QueryResponse(m)))
        .map_err(|e| Self::Error::Message(e.into())),
      MessageType::ConflictResponse => Member::decode(bytes.as_ref())
        .map(|(n, m)| (n, SerfMessage::ConflictResponse(m)))
        .map_err(|e| Self::Error::Message(e.into())),
      MessageType::Relay => Err(Self::Error::UnexpectedRelayMessage),
      #[cfg(feature = "encryption")]
      MessageType::KeyRequest => ruserf_types::KeyRequestMessage::decode(bytes.as_ref())
        .map(|(n, m)| (n, SerfMessage::KeyRequest(m)))
        .map_err(|e| Self::Error::Message(e.into())),
      #[cfg(feature = "encryption")]
      MessageType::KeyResponse => ruserf_types::KeyResponseMessage::decode(bytes.as_ref())
        .map(|(n, m)| (n, SerfMessage::KeyResponse(m)))
        .map_err(|e| Self::Error::Message(e.into())),
      _ => unreachable!(),
    }
  }
}