group.rs 2.9 KB

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