Skip to main content

dht_rpc/
message.rs

1use crate::util::{debug_vec, pretty_bytes};
2
3use super::{Command, Peer};
4
5/// ReplyMsgData is the Reply data structure that is encoded to/from bytes
6#[derive(Clone, PartialEq)]
7pub struct ReplyMsgData {
8    pub tid: u16,
9    pub to: Peer,
10    pub id: Option<[u8; 32]>,
11    /// TODO remove option? we always send token with reply I thought
12    pub token: Option<[u8; 32]>,
13    pub closer_nodes: Vec<Peer>,
14    pub error: usize,
15    pub value: Option<Vec<u8>>,
16}
17
18/// RequestMsgData is the Request data structure that is encoded to/from bytes
19#[derive(Clone, PartialEq)]
20pub struct RequestMsgData {
21    pub tid: u16,
22    pub to: Peer,
23    pub command: Command,
24    pub id: Option<[u8; 32]>,
25    pub token: Option<[u8; 32]>,
26    pub target: Option<[u8; 32]>,
27    pub value: Option<Vec<u8>>,
28}
29
30impl RequestMsgData {
31    pub fn from_ids_and_inner_data(
32        tid: u16,
33        id: Option<[u8; 32]>,
34        data: RequestMsgDataInner,
35    ) -> Self {
36        Self {
37            tid,
38            id,
39            to: data.to,
40            token: data.token,
41            command: data.command,
42            target: data.target,
43            value: data.value,
44        }
45    }
46}
47
48impl ReplyMsgData {
49    pub fn is_error(&self) -> bool {
50        self.error != 0
51    }
52}
53
54pub struct RequestMsgDataInner {
55    pub to: Peer,
56    pub token: Option<[u8; 32]>,
57    pub command: Command,
58    pub target: Option<[u8; 32]>,
59    pub value: Option<Vec<u8>>,
60}
61
62macro_rules! opt_map_inner {
63    ($debug_struct:tt, $name:expr, $name_s:tt, $func:tt) => {
64        match &$name {
65            Some(bytes) => $debug_struct.field($name_s, &format_args!("Some({})", $func(bytes))),
66            None => $debug_struct.field($name_s, &None::<String>),
67        };
68    };
69}
70
71impl std::fmt::Debug for RequestMsgData {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        let mut debug_struct = f.debug_struct("RequestMsgData");
74        debug_struct.field("tid", &self.tid).field("to", &self.to);
75        opt_map_inner!(debug_struct, self.id, "id", pretty_bytes);
76        opt_map_inner!(debug_struct, self.token, "token", pretty_bytes);
77        debug_struct.field("command", &self.command);
78        opt_map_inner!(debug_struct, self.target, "target", pretty_bytes);
79        opt_map_inner!(debug_struct, self.value, "value", pretty_bytes);
80        debug_struct.finish()
81    }
82}
83impl std::fmt::Debug for ReplyMsgData {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        let mut debug_struct = f.debug_struct("ReplyMsgData");
86        debug_struct.field("tid", &self.tid).field("to", &self.to);
87        opt_map_inner!(debug_struct, self.id, "id", pretty_bytes);
88        opt_map_inner!(debug_struct, self.token, "token", pretty_bytes);
89        debug_struct
90            .field("closer_nodes", &debug_vec(&self.closer_nodes))
91            .field("error", &self.error);
92        opt_map_inner!(debug_struct, self.value, "value", pretty_bytes);
93        debug_struct.finish()
94    }
95}
96
97/// MsgData is the data structure that is converted to/from bytes
98#[derive(Debug, Clone, PartialEq)]
99pub enum MsgData {
100    Request(RequestMsgData),
101    Reply(ReplyMsgData),
102}
103
104impl From<RequestMsgData> for MsgData {
105    fn from(value: RequestMsgData) -> Self {
106        MsgData::Request(value)
107    }
108}
109impl From<ReplyMsgData> for MsgData {
110    fn from(value: ReplyMsgData) -> Self {
111        MsgData::Reply(value)
112    }
113}
114
115impl MsgData {
116    pub fn tid(&self) -> u16 {
117        match self {
118            MsgData::Request(RequestMsgData { tid, .. }) => *tid,
119            MsgData::Reply(ReplyMsgData { tid, .. }) => *tid,
120        }
121    }
122    pub fn to(&self) -> Peer {
123        match self {
124            MsgData::Request(x) => x.to.clone(),
125            MsgData::Reply(x) => x.to.clone(),
126        }
127    }
128}