from_impl.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 = "net")]
  21. impl From<net::channel::ChannelInfo> for JsonValue {
  22. fn from(info: net::channel::ChannelInfo) -> JsonValue {
  23. json_map([("addr", JsonStr(info.addr.to_string())), ("id", JsonNum(info.id.into()))])
  24. }
  25. }
  26. #[cfg(feature = "net")]
  27. impl From<net::dnet::MessageInfo> for JsonValue {
  28. fn from(info: net::dnet::MessageInfo) -> JsonValue {
  29. json_map([
  30. ("chan", info.chan.into()),
  31. ("cmd", JsonStr(info.cmd)),
  32. ("time", JsonStr(info.time.0.to_string())),
  33. ])
  34. }
  35. }
  36. #[cfg(feature = "net")]
  37. impl From<net::dnet::InboundInfo> for JsonValue {
  38. fn from(info: net::dnet::InboundInfo) -> JsonValue {
  39. json_map([
  40. ("addr", JsonStr(info.addr.to_string())),
  41. ("channel_id", JsonNum(info.channel_id.into())),
  42. ])
  43. }
  44. }
  45. #[cfg(feature = "net")]
  46. impl From<net::dnet::OutboundSlotSleeping> for JsonValue {
  47. fn from(info: net::dnet::OutboundSlotSleeping) -> JsonValue {
  48. json_map([("slot", JsonNum(info.slot.into()))])
  49. }
  50. }
  51. #[cfg(feature = "net")]
  52. impl From<net::dnet::OutboundSlotConnecting> for JsonValue {
  53. fn from(info: net::dnet::OutboundSlotConnecting) -> JsonValue {
  54. json_map([("slot", JsonNum(info.slot.into())), ("addr", JsonStr(info.addr.to_string()))])
  55. }
  56. }
  57. #[cfg(feature = "net")]
  58. impl From<net::dnet::OutboundSlotConnected> for JsonValue {
  59. fn from(info: net::dnet::OutboundSlotConnected) -> JsonValue {
  60. json_map([
  61. ("slot", JsonNum(info.slot.into())),
  62. ("addr", JsonStr(info.addr.to_string())),
  63. ("channel_id", JsonNum(info.channel_id.into())),
  64. ])
  65. }
  66. }
  67. #[cfg(feature = "net")]
  68. impl From<net::dnet::OutboundSlotDisconnected> for JsonValue {
  69. fn from(info: net::dnet::OutboundSlotDisconnected) -> JsonValue {
  70. json_map([("slot", JsonNum(info.slot.into())), ("err", JsonStr(info.err))])
  71. }
  72. }
  73. #[cfg(feature = "net")]
  74. impl From<net::dnet::OutboundPeerDiscovery> for JsonValue {
  75. fn from(info: net::dnet::OutboundPeerDiscovery) -> JsonValue {
  76. json_map([
  77. ("attempt", JsonNum(info.attempt.into())),
  78. ("state", JsonStr(info.state.to_string())),
  79. ])
  80. }
  81. }
  82. #[cfg(feature = "net")]
  83. impl From<net::dnet::DnetEvent> for JsonValue {
  84. fn from(event: net::dnet::DnetEvent) -> JsonValue {
  85. match event {
  86. net::dnet::DnetEvent::SendMessage(info) => {
  87. json_map([("event", json_str("send")), ("info", info.into())])
  88. }
  89. net::dnet::DnetEvent::RecvMessage(info) => {
  90. json_map([("event", json_str("recv")), ("info", info.into())])
  91. }
  92. net::dnet::DnetEvent::InboundConnected(info) => {
  93. json_map([("event", json_str("inbound_connected")), ("info", info.into())])
  94. }
  95. net::dnet::DnetEvent::InboundDisconnected(info) => {
  96. json_map([("event", json_str("inbound_disconnected")), ("info", info.into())])
  97. }
  98. net::dnet::DnetEvent::OutboundSlotSleeping(info) => {
  99. json_map([("event", json_str("outbound_slot_sleeping")), ("info", info.into())])
  100. }
  101. net::dnet::DnetEvent::OutboundSlotConnecting(info) => {
  102. json_map([("event", json_str("outbound_slot_connecting")), ("info", info.into())])
  103. }
  104. net::dnet::DnetEvent::OutboundSlotConnected(info) => {
  105. json_map([("event", json_str("outbound_slot_connected")), ("info", info.into())])
  106. }
  107. net::dnet::DnetEvent::OutboundSlotDisconnected(info) => {
  108. json_map([("event", json_str("outbound_slot_disconnected")), ("info", info.into())])
  109. }
  110. net::dnet::DnetEvent::OutboundPeerDiscovery(info) => {
  111. json_map([("event", json_str("outbound_peer_discovery")), ("info", info.into())])
  112. }
  113. }
  114. }
  115. }