group.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. use serde::{Deserialize, Serialize};
  2. use std::fmt;
  3. use crate::lib::config::Config;
  4. #[derive(Serialize, Deserialize)]
  5. pub struct Group {
  6. #[serde(default)]
  7. name: String,
  8. #[serde(default)]
  9. pub actions_after: Vec<String>,
  10. #[serde(default)]
  11. pub members: Vec<String>,
  12. }
  13. pub fn add(config: &mut Config, name: &String) {
  14. let group = Group {
  15. name: name.to_string(),
  16. actions_after: Vec::new(),
  17. members: Vec::new()
  18. };
  19. config.groups.insert(name.to_string(), group);
  20. config.is_changed = true;
  21. }
  22. pub fn add_repo(config: &mut Config, name: &String, repo: &String) {
  23. match config.groups.get_mut(&name.to_string()) {
  24. Some(group) => {
  25. group.members.push(repo.to_string());
  26. config.is_changed = true;
  27. },
  28. None => panic!("No known group named \"{}\".", name)
  29. }
  30. }
  31. pub fn add_action(config: &mut Config, name: &String, action: &String) {
  32. match config.groups.get_mut(&name.to_string()) {
  33. Some(group) => {
  34. group.actions_after.push(action.to_string());
  35. config.is_changed = true;
  36. },
  37. None => panic!("No known group named \"{}\".", name)
  38. }
  39. }
  40. pub fn remove_repo(config: &mut Config, name: &String, repo: &String) {
  41. match config.groups.get_mut(&name.to_string()) {
  42. Some(group) => {
  43. group.members.retain(|r| r != repo);
  44. config.is_changed = true;
  45. }
  46. None => panic!("No known group named \"{}\".", name)
  47. }
  48. }
  49. pub fn remove_group(config: &mut Config, name: &String) {
  50. config.groups.remove(&name.to_string());
  51. config.is_changed = true;
  52. }
  53. impl fmt::Display for Group {
  54. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  55. write!(f, "Group {}:\n\tRepos:\n", self.name)?;
  56. for repo in &self.members {
  57. write!(f, "\t\t - {}\n", repo)?;
  58. }
  59. write!(f, "\tActions\n")?;
  60. for action in &self.actions_after {
  61. write!(f, "\t\t - {}\n", action)?;
  62. }
  63. write!(f, "")
  64. }
  65. }