coman/core/
collection_manager_ops.rs1use crate::core::collection_manager::CollectionResult;
2use crate::core::errors::CollectionError;
3use crate::core::utils::merge_headers;
4use crate::{Collection, CollectionManager};
5
6impl CollectionManager {
7 pub fn add_collection(
11 mut self,
12 name: &str,
13 url: &str,
14 headers: Vec<(String, String)>,
15 ) -> CollectionResult<()> {
16 match self.get_collection(name) {
17 Ok(c) => {
18 c.url = url.to_string();
20 c.headers = merge_headers(c.headers.clone(), &headers);
21 }
22 Err(_) => {
23 let new_collection = Collection {
25 name: name.to_string(),
26 url: url.to_string(),
27 headers: headers.clone(),
28 requests: None,
29 };
30 if let Some(ref mut cols) = self.loaded_collections {
31 cols.push(new_collection);
32 }
33 }
34 };
35 self.save_collections()?;
36 Ok(())
37 }
38
39 pub fn delete_collection(mut self, name: &str) -> CollectionResult<()> {
41 let collections = self
42 .loaded_collections
43 .as_mut()
44 .ok_or_else(|| CollectionError::CollectionNotFound(name.to_string()))?;
45 let original_len = collections.len();
46 collections.retain(|c| c.name != name);
47 if collections.len() == original_len {
48 return Err(CollectionError::CollectionNotFound(name.to_string()));
49 }
50
51 self.save_collections()?;
52 Ok(())
53 }
54
55 pub fn update_collection(
57 mut self,
58 name: &str,
59 url: Option<&str>,
60 headers: Option<Vec<(String, String)>>,
61 ) -> CollectionResult<()> {
62 let collections = self
63 .loaded_collections
64 .as_mut()
65 .ok_or_else(|| CollectionError::CollectionNotFound(name.to_string()))?;
66
67 let col = collections
68 .iter_mut()
69 .find(|c| c.name == name)
70 .ok_or_else(|| CollectionError::CollectionNotFound(name.to_string()))?;
71
72 if let Some(url) = url {
73 col.url = url.to_string();
74 }
75
76 if let Some(new_headers) = headers {
77 col.headers = merge_headers(col.headers.clone(), &new_headers);
78 }
79
80 self.save_collections()?;
81 Ok(())
82 }
83
84 pub fn copy_collection(mut self, name: &str, new_name: &str) -> CollectionResult<()> {
86 let collections = self
87 .loaded_collections
88 .as_mut()
89 .ok_or_else(|| CollectionError::CollectionNotFound(name.to_string()))?;
90
91 let col = collections
92 .iter()
93 .find(|c| c.name == name)
94 .ok_or_else(|| CollectionError::CollectionNotFound(name.to_string()))?;
95
96 let mut new_col = col.clone();
97 new_col.name = new_name.to_string();
98 collections.push(new_col);
99
100 self.save_collections()?;
101 Ok(())
102 }
103}