/ src / rpc / from_impl.rs
from_impl.rs
  1  /* This file is part of DarkFi (https://dark.fi)
  2   *
  3   * Copyright (C) 2020-2025 Dyne.org foundation
  4   *
  5   * This program is free software: you can redistribute it and/or modify
  6   * it under the terms of the GNU Affero General Public License as
  7   * published by the Free Software Foundation, either version 3 of the
  8   * License, or (at your option) any later version.
  9   *
 10   * This program is distributed in the hope that it will be useful,
 11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13   * GNU Affero General Public License for more details.
 14   *
 15   * You should have received a copy of the GNU Affero General Public License
 16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 17   */
 18  
 19  use super::util::*;
 20  use crate::net;
 21  
 22  #[cfg(feature = "event-graph")]
 23  use crate::event_graph;
 24  
 25  #[cfg(feature = "net")]
 26  impl From<net::channel::ChannelInfo> for JsonValue {
 27      fn from(info: net::channel::ChannelInfo) -> JsonValue {
 28          json_map([
 29              ("addr", JsonStr(info.resolve_addr.unwrap_or(info.connect_addr).to_string())),
 30              ("id", JsonNum(info.id.into())),
 31          ])
 32      }
 33  }
 34  
 35  #[cfg(feature = "net")]
 36  impl From<net::dnet::MessageInfo> for JsonValue {
 37      fn from(info: net::dnet::MessageInfo) -> JsonValue {
 38          json_map([
 39              ("chan", info.chan.into()),
 40              ("cmd", JsonStr(info.cmd)),
 41              ("time", JsonStr(info.time.0.to_string())),
 42          ])
 43      }
 44  }
 45  
 46  #[cfg(feature = "net")]
 47  impl From<net::dnet::InboundInfo> for JsonValue {
 48      fn from(info: net::dnet::InboundInfo) -> JsonValue {
 49          json_map([
 50              ("addr", JsonStr(info.addr.to_string())),
 51              ("channel_id", JsonNum(info.channel_id.into())),
 52          ])
 53      }
 54  }
 55  
 56  #[cfg(feature = "net")]
 57  impl From<net::dnet::OutboundSlotSleeping> for JsonValue {
 58      fn from(info: net::dnet::OutboundSlotSleeping) -> JsonValue {
 59          json_map([("slot", JsonNum(info.slot.into()))])
 60      }
 61  }
 62  
 63  #[cfg(feature = "net")]
 64  impl From<net::dnet::OutboundSlotConnecting> for JsonValue {
 65      fn from(info: net::dnet::OutboundSlotConnecting) -> JsonValue {
 66          json_map([("slot", JsonNum(info.slot.into())), ("addr", JsonStr(info.addr.to_string()))])
 67      }
 68  }
 69  
 70  #[cfg(feature = "net")]
 71  impl From<net::dnet::OutboundSlotConnected> for JsonValue {
 72      fn from(info: net::dnet::OutboundSlotConnected) -> JsonValue {
 73          json_map([
 74              ("slot", JsonNum(info.slot.into())),
 75              ("addr", JsonStr(info.addr.to_string())),
 76              ("channel_id", JsonNum(info.channel_id.into())),
 77          ])
 78      }
 79  }
 80  
 81  #[cfg(feature = "net")]
 82  impl From<net::dnet::OutboundSlotDisconnected> for JsonValue {
 83      fn from(info: net::dnet::OutboundSlotDisconnected) -> JsonValue {
 84          json_map([("slot", JsonNum(info.slot.into())), ("err", JsonStr(info.err))])
 85      }
 86  }
 87  
 88  #[cfg(feature = "net")]
 89  impl From<net::dnet::OutboundPeerDiscovery> for JsonValue {
 90      fn from(info: net::dnet::OutboundPeerDiscovery) -> JsonValue {
 91          json_map([
 92              ("attempt", JsonNum(info.attempt.into())),
 93              ("state", JsonStr(info.state.to_string())),
 94          ])
 95      }
 96  }
 97  
 98  #[cfg(feature = "net")]
 99  impl From<net::dnet::DirectConnecting> for JsonValue {
100      fn from(info: net::dnet::DirectConnecting) -> JsonValue {
101          json_map([("connect_addr", JsonStr(info.connect_addr.to_string()))])
102      }
103  }
104  
105  #[cfg(feature = "net")]
106  impl From<net::dnet::DirectConnected> for JsonValue {
107      fn from(info: net::dnet::DirectConnected) -> JsonValue {
108          json_map([
109              ("connect_addr", JsonStr(info.connect_addr.to_string())),
110              ("addr", JsonStr(info.addr.to_string())),
111              ("channel_id", JsonNum(info.channel_id.into())),
112          ])
113      }
114  }
115  
116  #[cfg(feature = "net")]
117  impl From<net::dnet::DirectDisconnected> for JsonValue {
118      fn from(info: net::dnet::DirectDisconnected) -> JsonValue {
119          json_map([
120              ("connect_addr", JsonStr(info.connect_addr.to_string())),
121              ("err", JsonStr(info.err)),
122          ])
123      }
124  }
125  
126  #[cfg(feature = "net")]
127  impl From<net::dnet::DnetEvent> for JsonValue {
128      fn from(event: net::dnet::DnetEvent) -> JsonValue {
129          match event {
130              net::dnet::DnetEvent::SendMessage(info) => {
131                  json_map([("event", json_str("send")), ("info", info.into())])
132              }
133              net::dnet::DnetEvent::RecvMessage(info) => {
134                  json_map([("event", json_str("recv")), ("info", info.into())])
135              }
136              net::dnet::DnetEvent::InboundConnected(info) => {
137                  json_map([("event", json_str("inbound_connected")), ("info", info.into())])
138              }
139              net::dnet::DnetEvent::InboundDisconnected(info) => {
140                  json_map([("event", json_str("inbound_disconnected")), ("info", info.into())])
141              }
142              net::dnet::DnetEvent::OutboundSlotSleeping(info) => {
143                  json_map([("event", json_str("outbound_slot_sleeping")), ("info", info.into())])
144              }
145              net::dnet::DnetEvent::OutboundSlotConnecting(info) => {
146                  json_map([("event", json_str("outbound_slot_connecting")), ("info", info.into())])
147              }
148              net::dnet::DnetEvent::OutboundSlotConnected(info) => {
149                  json_map([("event", json_str("outbound_slot_connected")), ("info", info.into())])
150              }
151              net::dnet::DnetEvent::OutboundSlotDisconnected(info) => {
152                  json_map([("event", json_str("outbound_slot_disconnected")), ("info", info.into())])
153              }
154              net::dnet::DnetEvent::OutboundPeerDiscovery(info) => {
155                  json_map([("event", json_str("outbound_peer_discovery")), ("info", info.into())])
156              }
157              net::dnet::DnetEvent::DirectConnecting(info) => {
158                  json_map([("event", json_str("direct_connecting")), ("info", info.into())])
159              }
160              net::dnet::DnetEvent::DirectConnected(info) => {
161                  json_map([("event", json_str("direct_connected")), ("info", info.into())])
162              }
163              net::dnet::DnetEvent::DirectDisconnected(info) => {
164                  json_map([("event", json_str("direct_disconnected")), ("info", info.into())])
165              }
166              net::dnet::DnetEvent::DirectPeerDiscovery(info) => {
167                  json_map([("event", json_str("direct_peer_discovery")), ("info", info.into())])
168              }
169          }
170      }
171  }
172  
173  #[cfg(feature = "event-graph")]
174  impl From<event_graph::Event> for JsonValue {
175      fn from(event: event_graph::Event) -> JsonValue {
176          let parents =
177              event.parents.into_iter().map(|id| JsonStr(id.to_string())).collect::<Vec<_>>();
178          json_map([
179              ("timestamp", JsonNum(event.timestamp as f64)),
180              ("content", JsonStr(bs58::encode(event.content()).into_string())),
181              ("parents", JsonArray(parents)),
182              ("layer", JsonNum(event.layer as f64)),
183          ])
184      }
185  }
186  
187  #[cfg(feature = "event-graph")]
188  impl From<event_graph::deg::MessageInfo> for JsonValue {
189      fn from(info: event_graph::deg::MessageInfo) -> JsonValue {
190          json_map([
191              ("info", JsonArray(info.info.into_iter().map(JsonStr).collect())),
192              ("cmd", JsonStr(info.cmd)),
193              ("time", JsonStr(info.time.0.to_string())),
194          ])
195      }
196  }
197  
198  #[cfg(feature = "event-graph")]
199  impl From<event_graph::deg::DegEvent> for JsonValue {
200      fn from(event: event_graph::deg::DegEvent) -> JsonValue {
201          match event {
202              event_graph::deg::DegEvent::SendMessage(info) => {
203                  json_map([("event", json_str("send")), ("info", info.into())])
204              }
205              event_graph::deg::DegEvent::RecvMessage(info) => {
206                  json_map([("event", json_str("recv")), ("info", info.into())])
207              }
208          }
209      }
210  }