repotype.rs 6.0 KB

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