from_impl.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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. use super::util::*;
  19. use crate::net;
  20. #[cfg(feature = "event-graph")]
  21. use crate::event_graph;
  22. #[cfg(feature = "net")]
  23. impl From<net::channel::ChannelInfo> for JsonValue {
  24. fn from(info: net::channel::ChannelInfo) -> JsonValue {
  25. json_map([
  26. ("addr", JsonStr(info.connect_addr.to_string())),
  27. ("id", JsonNum(info.id.into())),
  28. ])
  29. }
  30. }
  31. #[cfg(feature = "net")]
  32. impl From<net::dnet::MessageInfo> for JsonValue {
  33. fn from(info: net::dnet::MessageInfo) -> JsonValue {
  34. json_map([
  35. ("chan", info.chan.into()),
  36. ("cmd", JsonStr(info.cmd)),
  37. ("time", JsonStr(info.time.0.to_string())),
  38. ])
  39. }
  40. }
  41. #[cfg(feature = "net")]
  42. impl From<net::dnet::InboundInfo> for JsonValue {
  43. fn from(info: net::dnet::InboundInfo) -> JsonValue {
  44. json_map([
  45. ("addr", JsonStr(info.addr.to_string())),
  46. ("channel_id", JsonNum(info.channel_id.into())),
  47. ])
  48. }
  49. }
  50. #[cfg(feature = "net")]
  51. impl From<net::dnet::OutboundSlotSleeping> for JsonValue {
  52. fn from(info: net::dnet::OutboundSlotSleeping) -> JsonValue {
  53. json_map([("slot", JsonNum(info.slot.into()))])
  54. }
  55. }
  56. #[cfg(feature = "net")]
  57. impl From<net::dnet::OutboundSlotConnecting> for JsonValue {
  58. fn from(info: net::dnet::OutboundSlotConnecting) -> JsonValue {
  59. json_map([("slot", JsonNum(info.slot.into())), ("addr", JsonStr(info.addr.to_string()))])
  60. }
  61. }
  62. #[cfg(feature = "net")]
  63. impl From<net::dnet::OutboundSlotConnected> for JsonValue {
  64. fn from(info: net::dnet::OutboundSlotConnected) -> JsonValue {
  65. json_map([
  66. ("slot", JsonNum(info.slot.into())),
  67. ("addr", JsonStr(info.addr.to_string())),
  68. ("channel_id", JsonNum(info.channel_id.into())),
  69. ])
  70. }
  71. }
  72. #[cfg(feature = "net")]
  73. impl From<net::dnet::OutboundSlotDisconnected> for JsonValue {
  74. fn from(info: net::dnet::OutboundSlotDisconnected) -> JsonValue {
  75. json_map([("slot", JsonNum(info.slot.into())), ("err", JsonStr(info.err))])
  76. }
  77. }
  78. #[cfg(feature = "net")]
  79. impl From<net::dnet::OutboundPeerDiscovery> for JsonValue {
  80. fn from(info: net::dnet::OutboundPeerDiscovery) -> JsonValue {
  81. json_map([
  82. ("attempt", JsonNum(info.attempt.into())),
  83. ("state", JsonStr(info.state.to_string())),
  84. ])
  85. }
  86. }
  87. #[cfg(feature = "net")]
  88. impl From<net::dnet::DnetEvent> for JsonValue {
  89. fn from(event: net::dnet::DnetEvent) -> JsonValue {
  90. match event {
  91. net::dnet::DnetEvent::SendMessage(info) => {
  92. json_map([("event", json_str("send")), ("info", info.into())])
  93. }
  94. net::dnet::DnetEvent::RecvMessage(info) => {
  95. json_map([("event", json_str("recv")), ("info", info.into())])
  96. }
  97. net::dnet::DnetEvent::InboundConnected(info) => {
  98. json_map([("event", json_str("inbound_connected")), ("info", info.into())])
  99. }
  100. net::dnet::DnetEvent::InboundDisconnected(info) => {
  101. json_map([("event", json_str("inbound_disconnected")), ("info", info.into())])
  102. }
  103. net::dnet::DnetEvent::OutboundSlotSleeping(info) => {
  104. json_map([("event", json_str("outbound_slot_sleeping")), ("info", info.into())])
  105. }
  106. net::dnet::DnetEvent::OutboundSlotConnecting(info) => {
  107. json_map([("event", json_str("outbound_slot_connecting")), ("info", info.into())])
  108. }
  109. net::dnet::DnetEvent::OutboundSlotConnected(info) => {
  110. json_map([("event", json_str("outbound_slot_connected")), ("info", info.into())])
  111. }
  112. net::dnet::DnetEvent::OutboundSlotDisconnected(info) => {
  113. json_map([("event", json_str("outbound_slot_disconnected")), ("info", info.into())])
  114. }
  115. net::dnet::DnetEvent::OutboundPeerDiscovery(info) => {
  116. json_map([("event", json_str("outbound_peer_discovery")), ("info", info.into())])
  117. }
  118. }
  119. }
  120. }
  121. #[cfg(feature = "event-graph")]
  122. impl From<event_graph::Event> for JsonValue {
  123. fn from(event: event_graph::Event) -> JsonValue {
  124. let parents =
  125. event.parents.into_iter().map(|id| JsonStr(id.to_string())).collect::<Vec<_>>();
  126. json_map([
  127. ("timestamp", JsonNum(event.timestamp as f64)),
  128. ("content", JsonStr(bs58::encode(event.content()).into_string())),
  129. ("parents", JsonArray(parents)),
  130. ("layer", JsonNum(event.layer as f64)),
  131. ])
  132. }
  133. }
  134. #[cfg(feature = "event-graph")]
  135. impl From<event_graph::deg::MessageInfo> for JsonValue {
  136. fn from(info: event_graph::deg::MessageInfo) -> JsonValue {
  137. json_map([
  138. ("info", JsonArray(info.info.into_iter().map(JsonStr).collect())),
  139. ("cmd", JsonStr(info.cmd)),
  140. ("time", JsonStr(info.time.0.to_string())),
  141. ])
  142. }
  143. }
  144. #[cfg(feature = "event-graph")]
  145. impl From<event_graph::deg::DegEvent> for JsonValue {
  146. fn from(event: event_graph::deg::DegEvent) -> JsonValue {
  147. match event {
  148. event_graph::deg::DegEvent::SendMessage(info) => {
  149. json_map([("event", json_str("send")), ("info", info.into())])
  150. }
  151. event_graph::deg::DegEvent::RecvMessage(info) => {
  152. json_map([("event", json_str("recv")), ("info", info.into())])
  153. }
  154. }
  155. }
  156. }