repository.rs 3.0 KB

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