group.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. actions_after: Vec<String>,
  10. #[serde(default)]
  11. 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. }
  21. pub fn add_repo(config: &mut Config, name: &String, repo: &String) {
  22. match config.groups.get_mut(&name.to_string()) {
  23. Some(group) => group.members.push(repo.to_string()),
  24. None => panic!("No known group named \"{}\".", name)
  25. }
  26. }
  27. // TODO: add action adding
  28. // TODO: add repo removal
  29. // TODO: add group deletion
  30. impl fmt::Display for Group {
  31. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  32. write!(f, "Group {}:\n\tRepos:\n", self.name)?;
  33. for repo in &self.members {
  34. write!(f, "\t\t - {}\n", repo)?;
  35. }
  36. write!(f, "\tActions\n")?;
  37. for action in &self.actions_after {
  38. write!(f, "\t\t - {}\n", action)?;
  39. }
  40. write!(f, "")
  41. }
  42. }