model.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. use async_std::sync::Mutex;
  2. use std::collections::{HashMap, HashSet};
  3. use tui::widgets::ListState;
  4. pub struct Model {
  5. pub id_list: IdList,
  6. pub info_list: InfoList,
  7. }
  8. impl Model {
  9. pub fn new(id_list: IdList, info_list: InfoList) -> Model {
  10. Model { id_list, info_list }
  11. }
  12. }
  13. pub struct IdList {
  14. pub state: Mutex<ListState>,
  15. pub node_id: Mutex<HashSet<String>>,
  16. }
  17. impl IdList {
  18. pub fn new(node_id: HashSet<String>) -> IdList {
  19. let node_id = Mutex::new(node_id);
  20. IdList { state: Mutex::new(ListState::default()), node_id }
  21. }
  22. }
  23. pub struct InfoList {
  24. pub index: Mutex<usize>,
  25. pub infos: Mutex<HashMap<String, NodeInfo>>,
  26. }
  27. impl InfoList {
  28. pub fn new() -> InfoList {
  29. let index = 0;
  30. let index = Mutex::new(index);
  31. let infos = Mutex::new(HashMap::new());
  32. InfoList { index, infos }
  33. }
  34. }
  35. #[derive(Clone, Debug, PartialEq, Eq, Hash)]
  36. pub struct NodeInfo {
  37. pub outbound: Vec<Connection>,
  38. pub manual: Vec<Connection>,
  39. pub inbound: Vec<Connection>,
  40. }
  41. impl NodeInfo {
  42. pub fn new() -> NodeInfo {
  43. NodeInfo { outbound: Vec::new(), manual: Vec::new(), inbound: Vec::new() }
  44. }
  45. }
  46. #[derive(Clone, Debug, PartialEq, Eq, Hash)]
  47. pub struct Connection {
  48. pub id: String,
  49. pub message: String,
  50. }
  51. impl Connection {
  52. pub fn new(id: String, message: String) -> Connection {
  53. Connection { id, message }
  54. }
  55. }