repository.rs 3.1 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::collections::BTreeMap;
  6. use std::fmt;
  7. use crate::lib::config::Config;
  8. use crate::lib::group;
  9. #[derive(Serialize, Deserialize)]
  10. pub struct Repository {
  11. #[serde(default)]
  12. name: String,
  13. #[serde(default)]
  14. pub location: String,
  15. #[serde(default)]
  16. pub repo_type: String,
  17. #[serde(default)]
  18. pub auto_create: bool,
  19. #[serde(default)]
  20. pub disabled: bool,
  21. #[serde(default)]
  22. pub options: BTreeMap<String, String>,
  23. }
  24. pub fn register(config: &mut Config, name: &String, location: String, repo_type: String, options_strings: Vec<String>) {
  25. let mut options_map: BTreeMap<String, String> = BTreeMap::new();
  26. for option in options_strings {
  27. let option_pair: Vec<&str> = option.split("=").collect();
  28. options_map.insert(option_pair[0].to_string(), option_pair[1].to_string());
  29. }
  30. let repo = Repository {
  31. name: name.to_string(),
  32. location: location,
  33. repo_type: repo_type,
  34. auto_create: true,
  35. disabled: false,
  36. options: options_map
  37. };
  38. config.repositories.insert(name.to_string(), repo);
  39. config.is_changed = true;
  40. }
  41. pub fn update_disabled(config: &mut Config, name: &String, value: bool) {
  42. let repo = config.repositories.get_mut(&name.to_string());
  43. match repo {
  44. Some(repo) => {
  45. repo.disabled = value;
  46. config.is_changed = true;
  47. },
  48. None => panic!("No known repository named \"{}\".", name)
  49. }
  50. }
  51. pub fn update_autocreate(config: &mut Config, name: &String, value: bool) {
  52. let repo = config.repositories.get_mut(&name.to_string());
  53. match repo {
  54. Some(repo) => {
  55. repo.auto_create = value;
  56. config.is_changed = true;
  57. }
  58. None => panic!("No known repository named \"{}\".", name)
  59. }
  60. }
  61. pub fn update_options(config: &mut Config, name: &String, options_strings: Vec<String>) {
  62. let repo: Option<&mut Repository> = config.repositories.get_mut(&name.to_string());
  63. match repo {
  64. Some(repo) => for option in options_strings {
  65. let option_pair: Vec<&str> = option.split("=").collect();
  66. repo.options.insert(option_pair[0].to_string(), option_pair[1].to_string());
  67. config.is_changed = true;
  68. }
  69. None => panic!("No known repository named \"{}\".", name)
  70. }
  71. }
  72. pub fn remove_repo(mut config: &mut Config, name: &String) {
  73. config.repositories.remove(&name.to_string());
  74. config.is_changed = true;
  75. group::remove_repo_from_groups(&mut config, name);
  76. }
  77. impl fmt::Display for Repository {
  78. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  79. write!(f, "Repository {}:\n\tPath: {}\n\tType: {}\n\tDisabled: {}\n\tOptions:\n",
  80. self.name,
  81. self.location,
  82. self.repo_type,
  83. self.disabled)?;
  84. for (key, value) in &self.options {
  85. write!(f, "\t\t{}: {}\n", key, value)?;
  86. }
  87. write!(f, "")
  88. }
  89. }