from_impl.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 std::collections::HashMap;
  19. use tinyjson::JsonValue::{self, Number as JsonNum, Object as JsonObj, String as JsonStr};
  20. use crate::net;
  21. // helper functions
  22. fn json_map<const N: usize>(vals: [(&str, JsonValue); N]) -> JsonValue {
  23. JsonObj(HashMap::from(vals.map(|(k, v)| (k.to_string(), v))))
  24. }
  25. fn json_str(val: &str) -> JsonValue {
  26. JsonStr(val.to_string())
  27. }
  28. #[cfg(feature = "net")]
  29. impl From<net::channel::ChannelInfo> for JsonValue {
  30. fn from(info: net::channel::ChannelInfo) -> JsonValue {
  31. json_map([("addr", JsonStr(info.addr.to_string())), ("id", JsonNum(info.id.into()))])
  32. }
  33. }
  34. #[cfg(feature = "net")]
  35. impl From<net::dnet::MessageInfo> for JsonValue {
  36. fn from(info: net::dnet::MessageInfo) -> JsonValue {
  37. json_map([
  38. ("chan", info.chan.into()),
  39. ("cmd", JsonStr(info.cmd)),
  40. ("time", JsonStr(info.time.0.to_string())),
  41. ])
  42. }
  43. }
  44. #[cfg(feature = "net")]
  45. impl From<net::dnet::OutboundSlotSleeping> for JsonValue {
  46. fn from(info: net::dnet::OutboundSlotSleeping) -> JsonValue {
  47. json_map([("slot", JsonNum(info.slot.into()))])
  48. }
  49. }
  50. #[cfg(feature = "net")]
  51. impl From<net::dnet::OutboundSlotConnecting> for JsonValue {
  52. fn from(info: net::dnet::OutboundSlotConnecting) -> JsonValue {
  53. json_map([("slot", JsonNum(info.slot.into())), ("addr", JsonStr(info.addr.to_string()))])
  54. }
  55. }
  56. #[cfg(feature = "net")]
  57. impl From<net::dnet::OutboundSlotConnected> for JsonValue {
  58. fn from(info: net::dnet::OutboundSlotConnected) -> JsonValue {
  59. json_map([
  60. ("slot", JsonNum(info.slot.into())),
  61. ("addr", JsonStr(info.addr.to_string())),
  62. ("channel_id", JsonNum(info.channel_id.into())),
  63. ])
  64. }
  65. }
  66. #[cfg(feature = "net")]
  67. impl From<net::dnet::OutboundSlotDisconnected> for JsonValue {
  68. fn from(info: net::dnet::OutboundSlotDisconnected) -> JsonValue {
  69. json_map([("slot", JsonNum(info.slot.into())), ("err", JsonStr(info.err))])
  70. }
  71. }
  72. #[cfg(feature = "net")]
  73. impl From<net::dnet::OutboundPeerDiscovery> for JsonValue {
  74. fn from(info: net::dnet::OutboundPeerDiscovery) -> JsonValue {
  75. json_map([
  76. ("attempt", JsonNum(info.attempt.into())),
  77. ("state", JsonStr(info.state.to_string())),
  78. ])
  79. }
  80. }
  81. #[cfg(feature = "net")]
  82. impl From<net::dnet::DnetEvent> for JsonValue {
  83. fn from(event: net::dnet::DnetEvent) -> JsonValue {
  84. match event {
  85. net::dnet::DnetEvent::SendMessage(info) => {
  86. json_map([("event", json_str("send")), ("info", info.into())])
  87. }
  88. net::dnet::DnetEvent::RecvMessage(info) => {
  89. json_map([("event", json_str("recv")), ("info", info.into())])
  90. }
  91. net::dnet::DnetEvent::OutboundSlotSleeping(info) => {
  92. json_map([("event", json_str("outbound_slot_sleeping")), ("info", info.into())])
  93. }
  94. net::dnet::DnetEvent::OutboundSlotConnecting(info) => {
  95. json_map([("event", json_str("outbound_slot_connecting")), ("info", info.into())])
  96. }
  97. net::dnet::DnetEvent::OutboundSlotConnected(info) => {
  98. json_map([("event", json_str("outbound_slot_connected")), ("info", info.into())])
  99. }
  100. net::dnet::DnetEvent::OutboundSlotDisconnected(info) => {
  101. json_map([("event", json_str("outbound_slot_disconnected")), ("info", info.into())])
  102. }
  103. net::dnet::DnetEvent::OutboundPeerDiscovery(info) => {
  104. json_map([("event", json_str("outbound_peer_discovery")), ("info", info.into())])
  105. }
  106. }
  107. }
  108. }