group.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. use serde::{Deserialize, Serialize};
  2. use std::fmt;
  3. use std::collections::BTreeSet;
  4. use crate::lib::config::Config;
  5. #[derive(Serialize, Deserialize)]
  6. pub struct Group {
  7. #[serde(default)]
  8. name: String,
  9. #[serde(default)]
  10. pub actions_after: Vec<String>,
  11. #[serde(default)]
  12. pub members: BTreeSet<String>,
  13. }
  14. pub fn add(config: &mut Config, name: &String) {
  15. let group = Group {
  16. name: name.to_string(),
  17. actions_after: Vec::new(),
  18. members: BTreeSet::new()
  19. };
  20. config.groups.insert(name.to_string(), group);
  21. config.is_changed = true;
  22. }
  23. pub fn add_repo(config: &mut Config, name: &String, repo: &String) {
  24. match config.groups.get_mut(&name.to_string()) {
  25. Some(group) => {
  26. match config.repositories.get(&repo.to_string()) {
  27. Some(_) => {
  28. group.members.insert(repo.to_string());
  29. config.is_changed = true;
  30. },
  31. None => panic!("No known repository named \"{}\".", repo)
  32. }
  33. },
  34. None => panic!("No known group named \"{}\".", name)
  35. }
  36. }
  37. pub fn add_action(config: &mut Config, name: &String, action: &String) {
  38. match config.groups.get_mut(&name.to_string()) {
  39. Some(group) => {
  40. match config.actions.get(&action.to_string()) {
  41. Some(_) => {
  42. group.actions_after.push(action.to_string());
  43. config.is_changed = true;
  44. },
  45. None => panic!("No known action named \"{}\".", action)
  46. }
  47. },
  48. None => panic!("No known group named \"{}\".", name)
  49. }
  50. }
  51. pub fn remove_repo(config: &mut Config, name: &String, repo: &String) {
  52. match config.groups.get_mut(&name.to_string()) {
  53. Some(group) => {
  54. group.members.remove(repo);
  55. config.is_changed = true;
  56. }
  57. None => panic!("No known group named \"{}\".", name)
  58. }
  59. }
  60. pub fn remove_repo_from_groups(config: &mut Config, repo: &String) {
  61. // let ref tmp_groups = &config.groups;
  62. for (_group_name, group) in &mut config.groups {
  63. group.members.remove(repo);
  64. }
  65. }
  66. pub fn remove_group(config: &mut Config, name: &String) {
  67. config.groups.remove(&name.to_string());
  68. config.is_changed = true;
  69. for (_name, group) in &mut config.groups {
  70. group.members.remove(name);
  71. }
  72. }
  73. impl fmt::Display for Group {
  74. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  75. write!(f, "Group {}:\n\tRepos:\n", self.name)?;
  76. for repo in &self.members {
  77. write!(f, "\t\t - {}\n", repo)?;
  78. }
  79. write!(f, "\tActions\n")?;
  80. for action in &self.actions_after {
  81. write!(f, "\t\t - {}\n", action)?;
  82. }
  83. write!(f, "")
  84. }
  85. }