tfc_toolset/filter/
workspace.rs

1use crate::{
2    error::ToolError,
3    settings::{Core, Operators},
4    workspace::{Workspace, WorkspaceVariables},
5};
6
7pub fn by_variable(
8    workspace_variables: &mut Vec<WorkspaceVariables>,
9    config: &Core,
10) -> Result<(), ToolError> {
11    // You have to return a bool from the closure.
12    // If you return true, the element is not removed;
13    // if you return false, it is removed.
14    workspace_variables.retain(|workspace| {
15        let mut keep = false;
16        if let Some(query) = config.clone().workspaces.query {
17            if let Some(variables) = query.variables {
18                for variable in variables {
19                    match variable.operator {
20                        Operators::Equals => {
21                            // Should be equal.
22                            // so if it's not equal or not present we need to remove.
23                            keep = false;
24                            for var in workspace.variables.clone() {
25                                if var.attributes.key == variable.key {
26                                    if let Some(value) = var.attributes.value {
27                                        if value == variable.value {
28                                            keep = true;
29                                        }
30                                    }
31                                }
32                            }
33                            if !keep {
34                                return keep;
35                            }
36                        }
37                        Operators::NotEquals => {
38                            // Should not be equal.
39                            // so if it's equal we need to remove.
40                            keep = true;
41                            for var in workspace.variables.clone() {
42                                if var.attributes.key == variable.key {
43                                    if let Some(value) = var.attributes.value {
44                                        if value == variable.value {
45                                            keep = false;
46                                        }
47                                    }
48                                }
49                            }
50                            if !keep {
51                                return keep;
52                            }
53                        }
54                        Operators::Contains => {
55                            // Should contain the query value.
56                            // so if it doesn't contain the value or isn't present we need to remove.
57                            keep = false;
58                            for var in workspace.variables.clone() {
59                                if var.attributes.key == variable.key {
60                                    if let Some(value) = var.attributes.value {
61                                        if value
62                                            .contains(&variable.value.clone())
63                                        {
64                                            keep = true;
65                                        }
66                                    }
67                                }
68                            }
69                            if !keep {
70                                return keep;
71                            }
72                        }
73                        Operators::NotContains => {
74                            // Should be a regex hit.
75                            // so if it's not a hit or not present we need to remove.
76                            keep = true;
77                            for var in workspace.variables.clone() {
78                                if var.attributes.key == variable.key {
79                                    if let Some(value) = var.attributes.value {
80                                        if value
81                                            .contains(&variable.value.clone())
82                                        {
83                                            keep = false;
84                                        }
85                                    }
86                                }
87                            }
88                            if !keep {
89                                return keep;
90                            }
91                        }
92                    }
93                }
94            }
95        }
96        keep
97    });
98    Ok(())
99}
100
101pub fn by_tag(
102    workspaces: &mut Vec<Workspace>,
103    config: &Core,
104) -> Result<(), ToolError> {
105    // You have to return a bool from the closure.
106    // If you return true, the element is not removed;
107    // if you return false, it is removed.
108    workspaces.retain(|workspace| {
109        let mut keep = false;
110        if let Some(query) = config.clone().workspaces.query {
111            if let Some(tags) = query.tags {
112                for q_tag in tags {
113                    match q_tag.operator {
114                        // Equals and NotEquals is now handled in the url params so could be changed
115                        Operators::Equals => {
116                            // Should be equal.
117                            // so if it's not equal or not present we need to remove.
118                            keep = false;
119                            if let Some(tag_names) =
120                                workspace.attributes.tag_names.clone()
121                            {
122                                for tag in tag_names {
123                                    if tag == q_tag.name {
124                                        keep = true;
125                                    }
126                                }
127                            }
128                            if !keep {
129                                return keep;
130                            }
131                        }
132                        Operators::NotEquals => {
133                            // Should not be equal.
134                            // so if it's equal we need to remove.
135                            keep = true;
136                            if let Some(tag_names) =
137                                workspace.attributes.tag_names.clone()
138                            {
139                                for tag in tag_names {
140                                    if tag == q_tag.name {
141                                        keep = false;
142                                    }
143                                }
144                            }
145                            if !keep {
146                                return keep;
147                            }
148                        }
149                        Operators::Contains => {
150                            // Should contain the query value.
151                            // so if it doesn't contain the value or isn't present we need to remove.
152                            keep = false;
153                            while let Some(tag) =
154                                workspace.attributes.tag_names.clone()
155                            {
156                                if tag.contains(&q_tag.name) {
157                                    keep = true;
158                                }
159                            }
160                            if !keep {
161                                return keep;
162                            }
163                        }
164                        Operators::NotContains => {
165                            // Should be a regex hit.
166                            // so if it's not a hit or not present we need to remove.
167                            keep = true;
168                            while let Some(tag) =
169                                workspace.attributes.tag_names.clone()
170                            {
171                                if tag.contains(&q_tag.name) {
172                                    keep = false;
173                                }
174                            }
175                            if !keep {
176                                return keep;
177                            }
178                        }
179                    }
180                }
181            }
182        }
183        keep
184    });
185    Ok(())
186}