repotype.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #[derive(Serialize, Deserialize)]
  9. pub struct RepoType {
  10. #[serde(default)]
  11. name: String,
  12. #[serde(default)]
  13. description: String,
  14. #[serde(default)]
  15. pub create: String,
  16. #[serde(default)]
  17. pub inward: String,
  18. #[serde(default)]
  19. pub outward: String,
  20. #[serde(default)]
  21. status: String,
  22. #[serde(default)]
  23. pub pre_inward: String,
  24. #[serde(default)]
  25. pub post_inward: String,
  26. #[serde(default)]
  27. pub post_outward: String,
  28. #[serde(default)]
  29. pub commands: BTreeMap<String,String>
  30. }
  31. pub fn add(config: &mut Config, name: &String, description: &String, create: &String, inward: &String, outward: &String, status: &String, pre_inward: &String, post_inward: &String, post_outward: &String) {
  32. let repo_type = RepoType {
  33. name: name.to_string(),
  34. description: description.to_string(),
  35. create: create.to_string(),
  36. inward: inward.to_string(),
  37. outward: outward.to_string(),
  38. status: status.to_string(),
  39. pre_inward: pre_inward.to_string(),
  40. post_inward: post_inward.to_string(),
  41. post_outward: post_outward.to_string(),
  42. commands: BTreeMap::new()
  43. };
  44. config.repo_types.insert(name.to_string(), repo_type);
  45. config.is_changed = true;
  46. }
  47. pub fn update_description(config: &mut Config, name: &String, description: &String) {
  48. match config.repo_types.get_mut(&name.to_string()) {
  49. Some(repo_type) => {
  50. repo_type.description = description.to_string();
  51. config.is_changed = true;
  52. },
  53. None => panic!("No known repository type named \"{}\".", name)
  54. }
  55. }
  56. pub fn update_create(config: &mut Config, name: &String, create: &String) {
  57. match config.repo_types.get_mut(&name.to_string()) {
  58. Some(repo_type) => {
  59. repo_type.create = create.to_string();
  60. config.is_changed = true;
  61. },
  62. None => panic!("No known repository type named \"{}\".", name)
  63. }
  64. }
  65. pub fn update_inward(config: &mut Config, name: &String, inward: &String) {
  66. match config.repo_types.get_mut(&name.to_string()) {
  67. Some(repo_type) => {
  68. repo_type.inward = inward.to_string();
  69. config.is_changed = true;
  70. },
  71. None => panic!("No known repository type named \"{}\".", name)
  72. }
  73. }
  74. pub fn update_outward(config: &mut Config, name: &String, outward: &String) {
  75. match config.repo_types.get_mut(&name.to_string()) {
  76. Some(repo_type) => {
  77. repo_type.outward = outward.to_string();
  78. config.is_changed = true;
  79. },
  80. None => panic!("No known repository type named \"{}\".", name)
  81. }
  82. }
  83. pub fn update_status(config: &mut Config, name: &String, status: &String) {
  84. match config.repo_types.get_mut(&name.to_string()) {
  85. Some(repo_type) => {
  86. repo_type.status = status.to_string();
  87. config.is_changed = true;
  88. },
  89. None => panic!("No known repository type named \"{}\".", name)
  90. }
  91. }
  92. pub fn update_pre_inward(config: &mut Config, name: &String, pre_inward: &String) {
  93. match config.repo_types.get_mut(&name.to_string()) {
  94. Some(repo_type) => {
  95. repo_type.pre_inward = pre_inward.to_string();
  96. config.is_changed = true;
  97. },
  98. None => panic!("No known repository type named \"{}\".", name)
  99. }
  100. }
  101. pub fn update_post_inward(config: &mut Config, name: &String, post_inward: &String) {
  102. match config.repo_types.get_mut(&name.to_string()) {
  103. Some(repo_type) => {
  104. repo_type.post_inward = post_inward.to_string();
  105. config.is_changed = true;
  106. },
  107. None => panic!("No known repository type named \"{}\".", name)
  108. }
  109. }
  110. pub fn update_post_outward(config: &mut Config, name: &String, post_outward: &String) {
  111. match config.repo_types.get_mut(&name.to_string()) {
  112. Some(repo_type) => {
  113. repo_type.post_outward = post_outward.to_string();
  114. config.is_changed = true;
  115. },
  116. None => panic!("No known repository type named \"{}\".", name)
  117. }
  118. }
  119. pub fn add_command(config: &mut Config, type_name: &String, name: &String, command: &String) {
  120. match config.repo_types.get_mut(&type_name.to_string()) {
  121. Some(repo_type) => {
  122. repo_type.commands.insert(name.to_string(), command.to_string());
  123. config.is_changed = true;
  124. },
  125. None => panic!("No known repository type named \"{}\".", type_name)
  126. }
  127. }
  128. pub fn change_command(config: &mut Config, type_name: &String, name: &String, command: &String) {
  129. match config.repo_types.get_mut(&type_name.to_string()) {
  130. Some(repo_type) => {
  131. repo_type.commands.insert(name.to_string(), command.to_string());
  132. config.is_changed = true;
  133. },
  134. None => panic!("No known repository type named \"{}\".", type_name)
  135. }
  136. }
  137. pub fn remove_command(config: &mut Config, type_name: &String, name: &String) {
  138. match config.repo_types.get_mut(&type_name.to_string()) {
  139. Some(repo_type) => {
  140. repo_type.commands.remove(&name.to_string());
  141. config.is_changed = true;
  142. },
  143. None => panic!("No known repository type named \"{}\".", type_name)
  144. }
  145. }
  146. impl fmt::Display for RepoType {
  147. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  148. write!(f, "Repository type {}:\n\t\"{}\"\n\tCreation Command: {}\n\tInward Sync: {}\n\tOutward Sync: {}\n\tStatus: {}\n\tPre-inward: {}\n\tPost-inward: {}\n\tPost-outward: {}",
  149. self.name,
  150. self.description,
  151. self.create,
  152. self.inward,
  153. self.outward,
  154. self.status,
  155. self.pre_inward,
  156. self.post_inward,
  157. self.post_outward)?;
  158. write!(f, "Additional Commands:\n")?;
  159. for command in self.commands.keys() {
  160. write!(f, "\t - {}: {}\n", command, &self.commands.get(command).unwrap())?;
  161. }
  162. write!(f, "")
  163. }
  164. }