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
use crate::{Error, MessageType, Result, WireMsg};
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug, Formatter};
#[derive(Clone, Eq, Serialize, Deserialize)]
pub struct NodeMessage(#[serde(with = "serde_bytes")] pub Vec<u8>);
impl NodeMessage {
pub fn new(bytes: Bytes) -> Self {
Self(bytes.to_vec())
}
pub fn from(bytes: Bytes) -> Result<Self> {
let deserialized = WireMsg::deserialize(bytes)?;
if let MessageType::NodeMessage(msg) = deserialized {
Ok(msg)
} else {
Err(Error::FailedToParse("bytes as a node message".to_string()))
}
}
pub fn serialize(&self) -> Result<Bytes> {
WireMsg::serialize_node_msg(self)
}
}
impl PartialEq for NodeMessage {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Debug for NodeMessage {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
formatter
.debug_tuple("NodeMessage")
.field(&format_args!("{:10}", hex_fmt::HexFmt(&self.0)))
.finish()
}
}