group.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. group.members.insert(repo.to_string());
  27. config.is_changed = true;
  28. },
  29. None => panic!("No known group named \"{}\".", name)
  30. }
  31. }
  32. pub fn add_action(config: &mut Config, name: &String, action: &String) {
  33. match config.groups.get_mut(&name.to_string()) {
  34. Some(group) => {
  35. group.actions_after.push(action.to_string());
  36. config.is_changed = true;
  37. },
  38. None => panic!("No known group named \"{}\".", name)
  39. }
  40. }
  41. pub fn remove_repo(config: &mut Config, name: &String, repo: &String) {
  42. match config.groups.get_mut(&name.to_string()) {
  43. Some(group) => {
  44. // group.members.retain(|r| r != repo);
  45. group.members.remove(repo);
  46. config.is_changed = true;
  47. }
  48. None => panic!("No known group named \"{}\".", name)
  49. }
  50. }
  51. pub fn remove_group(config: &mut Config, name: &String) {
  52. config.groups.remove(&name.to_string());
  53. config.is_changed = true;
  54. }
  55. impl fmt::Display for Group {
  56. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  57. write!(f, "Group {}:\n\tRepos:\n", self.name)?;
  58. for repo in &self.members {
  59. write!(f, "\t\t - {}\n", repo)?;
  60. }
  61. write!(f, "\tActions\n")?;
  62. for action in &self.actions_after {
  63. write!(f, "\t\t - {}\n", action)?;
  64. }
  65. write!(f, "")
  66. }
  67. }