model.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. use async_std::sync::Mutex;
  2. use fxhash::{FxHashMap, FxHashSet};
  3. use serde::Deserialize;
  4. use tui::widgets::ListState;
  5. pub struct Model {
  6. pub id_list: IdList,
  7. pub info_list: InfoList,
  8. pub addr_list: AddrList,
  9. }
  10. impl Model {
  11. pub fn new(id_list: IdList, info_list: InfoList, addr_list: AddrList) -> Model {
  12. Model { id_list, info_list, addr_list }
  13. }
  14. }
  15. pub struct IdList {
  16. pub state: Mutex<ListState>,
  17. pub node_id: Mutex<FxHashSet<String>>,
  18. }
  19. impl IdList {
  20. pub fn new(node_id: FxHashSet<String>) -> IdList {
  21. let node_id = Mutex::new(node_id);
  22. IdList { state: Mutex::new(ListState::default()), node_id }
  23. }
  24. }
  25. pub struct InfoList {
  26. pub index: Mutex<usize>,
  27. pub infos: Mutex<FxHashMap<String, NodeInfo>>,
  28. }
  29. impl InfoList {
  30. pub fn new() -> InfoList {
  31. let index = 0;
  32. let index = Mutex::new(index);
  33. let infos = Mutex::new(FxHashMap::default());
  34. InfoList { index, infos }
  35. }
  36. }
  37. impl Default for InfoList {
  38. fn default() -> Self {
  39. Self::new()
  40. }
  41. }
  42. pub struct AddrList {
  43. pub index: Mutex<usize>,
  44. pub infos: Mutex<FxHashMap<String, AddrInfo>>,
  45. }
  46. impl AddrList {
  47. pub fn new() -> AddrList {
  48. let index = 0;
  49. let index = Mutex::new(index);
  50. let infos = Mutex::new(FxHashMap::default());
  51. AddrList { index, infos }
  52. }
  53. }
  54. #[derive(Clone, Debug, PartialEq, Eq, Hash)]
  55. pub struct NodeInfo {
  56. pub outbound: Vec<OutboundInfo>,
  57. pub manual: Vec<ManualInfo>,
  58. pub inbound: Vec<InboundInfo>,
  59. }
  60. impl NodeInfo {
  61. pub fn new() -> NodeInfo {
  62. NodeInfo { outbound: Vec::new(), manual: Vec::new(), inbound: Vec::new() }
  63. }
  64. }
  65. impl Default for NodeInfo {
  66. fn default() -> Self {
  67. Self::new()
  68. }
  69. }
  70. #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)]
  71. pub struct ManualInfo {
  72. pub key: u64,
  73. }
  74. impl ManualInfo {
  75. pub fn new(key: u64) -> ManualInfo {
  76. ManualInfo { key }
  77. }
  78. }
  79. #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)]
  80. pub struct OutboundInfo {
  81. pub is_empty: bool,
  82. pub slots: Vec<Slot>,
  83. }
  84. impl OutboundInfo {
  85. pub fn new(is_empty: bool, slots: Vec<Slot>) -> OutboundInfo {
  86. OutboundInfo { is_empty, slots }
  87. }
  88. }
  89. #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)]
  90. pub struct Slot {
  91. pub is_empty: bool,
  92. pub addr: String,
  93. pub channel: Channel,
  94. pub state: String,
  95. }
  96. impl Slot {
  97. pub fn new(is_empty: bool, addr: String, channel: Channel, state: String) -> Slot {
  98. Slot { is_empty, addr, channel, state }
  99. }
  100. }
  101. #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)]
  102. pub struct Channel {
  103. pub last_msg: String,
  104. pub last_status: String,
  105. }
  106. impl Channel {
  107. pub fn new(last_msg: String, last_status: String) -> Channel {
  108. Channel { last_msg, last_status }
  109. }
  110. }
  111. #[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)]
  112. pub struct InboundInfo {
  113. pub is_empty: bool,
  114. pub connected: String,
  115. pub channel: Channel,
  116. }
  117. impl InboundInfo {
  118. pub fn new(is_empty: bool, connected: String, channel: Channel) -> InboundInfo {
  119. InboundInfo { is_empty, connected, channel }
  120. }
  121. }
  122. #[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)]
  123. pub struct AddrInfo {
  124. // TODO: this will be a message log
  125. pub msgs: Vec<String>,
  126. }
  127. impl AddrInfo {
  128. pub fn new(msgs: Vec<String>) -> AddrInfo {
  129. AddrInfo { msgs }
  130. }
  131. }