1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- // SPDX-FileCopyrightText: 2021 - 2022 Samuel W. Flint <swflint@flintfam.org>
- //
- // SPDX-License-Identifier: GPL-3.0-or-later
- use serde::{Deserialize, Serialize};
- use std::fmt;
- use std::collections::BTreeSet;
- use crate::lib::config::Config;
- #[derive(Serialize, Deserialize)]
- pub struct Group {
- #[serde(default)]
- name: String,
- #[serde(default)]
- pub actions_after: Vec<String>,
- #[serde(default)]
- pub members: BTreeSet<String>,
- }
- pub fn add(config: &mut Config, name: &String) {
- let group = Group {
- name: name.to_string(),
- actions_after: Vec::new(),
- members: BTreeSet::new()
- };
- config.groups.insert(name.to_string(), group);
- config.is_changed = true;
- }
- pub fn add_repo(config: &mut Config, name: &String, repo: &String) {
- match config.groups.get_mut(&name.to_string()) {
- Some(group) => {
- match config.repositories.get(&repo.to_string()) {
- Some(_) => {
- group.members.insert(repo.to_string());
- config.is_changed = true;
- },
- None => panic!("No known repository named \"{}\".", repo)
- }
- },
- None => panic!("No known group named \"{}\".", name)
- }
- }
- pub fn add_action(config: &mut Config, name: &String, action: &String) {
- match config.groups.get_mut(&name.to_string()) {
- Some(group) => {
- match config.actions.get(&action.to_string()) {
- Some(_) => {
- group.actions_after.push(action.to_string());
- config.is_changed = true;
- },
- None => panic!("No known action named \"{}\".", action)
- }
- },
- None => panic!("No known group named \"{}\".", name)
- }
- }
- pub fn remove_repo(config: &mut Config, name: &String, repo: &String) {
- match config.groups.get_mut(&name.to_string()) {
- Some(group) => {
- group.members.remove(repo);
- config.is_changed = true;
- }
- None => panic!("No known group named \"{}\".", name)
- }
- }
- pub fn remove_repo_from_groups(config: &mut Config, repo: &String) {
- // let ref tmp_groups = &config.groups;
- for (_group_name, group) in &mut config.groups {
- group.members.remove(repo);
- }
- }
- pub fn remove_group(config: &mut Config, name: &String) {
- config.groups.remove(&name.to_string());
- config.is_changed = true;
- for (_name, group) in &mut config.groups {
- group.members.remove(name);
- }
- }
- impl fmt::Display for Group {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "Group {}:\n\tRepos:\n", self.name)?;
- for repo in &self.members {
- write!(f, "\t\t - {}\n", repo)?;
- }
- write!(f, "\tActions\n")?;
- for action in &self.actions_after {
- write!(f, "\t\t - {}\n", action)?;
- }
- write!(f, "")
- }
- }
|