model.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. }
  9. impl Model {
  10. pub fn new(id_list: IdList, info_list: InfoList) -> Model {
  11. Model { id_list, info_list }
  12. }
  13. }
  14. pub struct IdList {
  15. pub state: Mutex<ListState>,
  16. pub node_id: Mutex<FxHashSet<String>>,
  17. }
  18. impl IdList {
  19. pub fn new(node_id: FxHashSet<String>) -> IdList {
  20. let node_id = Mutex::new(node_id);
  21. IdList { state: Mutex::new(ListState::default()), node_id }
  22. }
  23. }
  24. pub struct InfoList {
  25. pub index: Mutex<usize>,
  26. pub infos: Mutex<FxHashMap<String, NodeInfo>>,
  27. }
  28. impl InfoList {
  29. pub fn new() -> InfoList {
  30. let index = 0;
  31. let index = Mutex::new(index);
  32. let infos = Mutex::new(FxHashMap::default());
  33. InfoList { index, infos }
  34. }
  35. }
  36. impl Default for InfoList {
  37. fn default() -> Self {
  38. Self::new()
  39. }
  40. }
  41. #[derive(Clone, Debug, PartialEq, Eq, Hash)]
  42. pub struct NodeInfo {
  43. pub outbound: Vec<OutboundInfo>,
  44. pub manual: Vec<ManualInfo>,
  45. pub inbound: Vec<InboundInfo>,
  46. }
  47. impl NodeInfo {
  48. pub fn new() -> NodeInfo {
  49. NodeInfo { outbound: Vec::new(), manual: Vec::new(), inbound: Vec::new() }
  50. }
  51. }
  52. impl Default for NodeInfo {
  53. fn default() -> Self {
  54. Self::new()
  55. }
  56. }
  57. #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)]
  58. pub struct ManualInfo {
  59. pub key: u64,
  60. }
  61. impl ManualInfo {
  62. pub fn new(key: u64) -> ManualInfo {
  63. ManualInfo { key }
  64. }
  65. }
  66. #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)]
  67. pub struct OutboundInfo {
  68. pub is_empty: bool,
  69. pub slots: Vec<Slot>,
  70. }
  71. impl OutboundInfo {
  72. pub fn new(is_empty: bool, slots: Vec<Slot>) -> OutboundInfo {
  73. OutboundInfo { is_empty, slots }
  74. }
  75. }
  76. #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)]
  77. pub struct Slot {
  78. pub is_empty: bool,
  79. pub addr: String,
  80. pub channel: Channel,
  81. pub state: String,
  82. }
  83. impl Slot {
  84. pub fn new(is_empty: bool, addr: String, channel: Channel, state: String) -> Slot {
  85. Slot { is_empty, addr, channel, state }
  86. }
  87. }
  88. #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)]
  89. pub struct Channel {
  90. pub last_msg: String,
  91. pub last_status: String,
  92. }
  93. impl Channel {
  94. pub fn new(last_msg: String, last_status: String) -> Channel {
  95. Channel { last_msg, last_status }
  96. }
  97. }
  98. #[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)]
  99. pub struct InboundInfo {
  100. pub is_empty: bool,
  101. pub connected: String,
  102. pub channel: Channel,
  103. }
  104. impl InboundInfo {
  105. pub fn new(is_empty: bool, connected: String, channel: Channel) -> InboundInfo {
  106. InboundInfo { is_empty, connected, channel }
  107. }
  108. }