Skip to main content

apimock_config/workspace/
snapshot.rs

1//! `Workspace::snapshot()` and the per-file view builders it composes.
2//!
3//! # Why this is a separate module
4//!
5//! Snapshot is read-only; it owns no mutating logic. Keeping it apart
6//! from the edit / save modules makes the read path easy to reason
7//! about — there's no chance a snapshot helper accidentally mutates
8//! the model because the `&self` receiver here can't.
9//!
10//! # Per-node validation runs here too
11//!
12//! `rule_set_file_view` calls into [`crate::workspace::validate`]
13//! to attach `NodeValidation` to each respond node. That means
14//! snapshot rendering and `validate()` walk the same checks; a node
15//! marked invalid in the snapshot will also appear in
16//! `ValidationReport::diagnostics`.
17
18use std::path::PathBuf;
19
20use apimock_routing::RuleSet;
21
22use crate::view::{
23    ConfigFileKind, ConfigFileView, ConfigNodeView, NodeKind, NodeValidation, WorkspaceSnapshot,
24};
25
26use super::Workspace;
27use super::id_index::NodeAddress;
28use super::path_helpers::file_basename;
29use super::validate::respond_node_validation;
30
31impl Workspace {
32    /// Build a snapshot for GUI rendering.
33    ///
34    /// # Allocation cost
35    ///
36    /// A snapshot fully owns its data (no borrows into the workspace)
37    /// so the GUI can serialise / send / store it without lifetime
38    /// gymnastics. This is O(total editable nodes) allocation per
39    /// call; the GUI should call it once per edit, not once per
40    /// render frame.
41    pub fn snapshot(&self) -> WorkspaceSnapshot {
42        let mut files: Vec<ConfigFileView> = Vec::new();
43
44        // Root file.
45        if let Some(root_nodes) = self.root_file_nodes() {
46            files.push(root_nodes);
47        }
48
49        // Rule-set files.
50        for (rs_idx, rule_set) in self.config.service.rule_sets.iter().enumerate() {
51            files.push(self.rule_set_file_view(rs_idx, rule_set));
52        }
53
54        // Middleware files. We don't introspect them beyond their path
55        // existence; the Rhai AST is a server-side concern.
56        if let Some(paths) = self.config.service.middlewares_file_paths.as_ref() {
57            for (mw_idx, mw_path) in paths.iter().enumerate() {
58                let abs = self.resolve_relative(mw_path);
59                let id = self
60                    .ids
61                    .id_for(NodeAddress::Middleware { middleware: mw_idx })
62                    .expect("middleware id seeded at load");
63                let node = ConfigNodeView {
64                    id,
65                    source_file: abs.clone(),
66                    toml_path: format!("service.middlewares[{}]", mw_idx),
67                    display_name: mw_path.clone(),
68                    kind: NodeKind::Script,
69                    validation: NodeValidation::ok(),
70                };
71                files.push(ConfigFileView {
72                    path: abs.clone(),
73                    display_name: file_basename(&abs),
74                    kind: ConfigFileKind::Middleware,
75                    nodes: vec![node],
76                });
77            }
78        }
79
80        // Route catalog — assemble from rule sets, fallback dir,
81        // file tree (depth-1 eager), and middleware script routes.
82        // Builders live in `apimock_routing::view::build`; the config
83        // crate just feeds them the data they need.
84        let fallback_dir = self.config.service.fallback_respond_dir.as_str();
85        let fallback_abs = self.resolve_relative(fallback_dir);
86        let file_tree = apimock_routing::view::build::build_file_tree(&fallback_abs);
87
88        let script_routes: Vec<apimock_routing::view::ScriptRouteView> = self
89            .config
90            .service
91            .middlewares_file_paths
92            .as_ref()
93            .map(|paths| {
94                paths
95                    .iter()
96                    .enumerate()
97                    .map(|(idx, p)| apimock_routing::view::build::build_script_route_view(idx, p))
98                    .collect()
99            })
100            .unwrap_or_default();
101
102        let routes = apimock_routing::view::build::build_route_catalog(
103            &self.config.service.rule_sets,
104            Some(fallback_dir),
105            file_tree,
106            script_routes,
107        );
108
109        WorkspaceSnapshot {
110            files,
111            routes,
112            diagnostics: self.diagnostics.clone(),
113        }
114    }
115    /// Root config file as a `ConfigFileView`, if it can be rendered.
116    fn root_file_nodes(&self) -> Option<ConfigFileView> {
117        let mut nodes = Vec::new();
118
119        if let Some(root_id) = self.ids.id_for(NodeAddress::Root) {
120            nodes.push(ConfigNodeView {
121                id: root_id,
122                source_file: self.root_path.clone(),
123                toml_path: String::new(),
124                display_name: "apimock.toml".to_owned(),
125                kind: NodeKind::RootSetting,
126                validation: NodeValidation::ok(),
127            });
128        }
129
130        if let Some(fb_id) = self.ids.id_for(NodeAddress::FallbackRespondDir) {
131            nodes.push(ConfigNodeView {
132                id: fb_id,
133                source_file: self.root_path.clone(),
134                toml_path: "service.fallback_respond_dir".to_owned(),
135                display_name: self.config.service.fallback_respond_dir.clone(),
136                kind: NodeKind::FileNode,
137                validation: NodeValidation::ok(),
138            });
139        }
140
141        Some(ConfigFileView {
142            path: self.root_path.clone(),
143            display_name: file_basename(&self.root_path),
144            kind: ConfigFileKind::Root,
145            nodes,
146        })
147    }
148
149    fn rule_set_file_view(&self, rs_idx: usize, rule_set: &RuleSet) -> ConfigFileView {
150        let file_path = PathBuf::from(rule_set.file_path.as_str());
151        let mut nodes: Vec<ConfigNodeView> = Vec::new();
152
153        // Rule-set itself.
154        if let Some(rs_id) = self
155            .ids
156            .id_for(NodeAddress::RuleSet { rule_set: rs_idx })
157        {
158            nodes.push(ConfigNodeView {
159                id: rs_id,
160                source_file: file_path.clone(),
161                toml_path: String::new(),
162                display_name: file_basename(&file_path),
163                kind: NodeKind::RuleSet,
164                validation: NodeValidation::ok(),
165            });
166        }
167
168        // Rules inside.
169        for (rule_idx, rule) in rule_set.rules.iter().enumerate() {
170            if let Some(rule_id) = self.ids.id_for(NodeAddress::Rule {
171                rule_set: rs_idx,
172                rule: rule_idx,
173            }) {
174                let url_path_label = rule
175                    .when
176                    .request
177                    .url_path
178                    .as_ref()
179                    .map(|u| u.value.as_str())
180                    .unwrap_or_default();
181                let display = if url_path_label.is_empty() {
182                    format!("Rule #{}", rule_idx + 1)
183                } else {
184                    url_path_label.to_owned()
185                };
186                nodes.push(ConfigNodeView {
187                    id: rule_id,
188                    source_file: file_path.clone(),
189                    toml_path: format!("rules[{}]", rule_idx),
190                    display_name: display,
191                    kind: NodeKind::Rule,
192                    validation: NodeValidation::ok(),
193                });
194            }
195
196            if let Some(resp_id) = self.ids.id_for(NodeAddress::Respond {
197                rule_set: rs_idx,
198                rule: rule_idx,
199            }) {
200                nodes.push(ConfigNodeView {
201                    id: resp_id,
202                    source_file: file_path.clone(),
203                    toml_path: format!("rules[{}].respond", rule_idx),
204                    display_name: summarise_respond(&rule.respond),
205                    kind: NodeKind::Respond,
206                    validation: respond_node_validation(&rule.respond, rule_set, rule_idx, rs_idx),
207                });
208            }
209        }
210
211        ConfigFileView {
212            path: file_path.clone(),
213            display_name: file_basename(&file_path),
214            kind: ConfigFileKind::RuleSet,
215            nodes,
216        }
217    }
218}
219
220fn summarise_respond(respond: &apimock_routing::Respond) -> String {
221    if let Some(p) = respond.file_path.as_ref() {
222        return format!("file: {}", p);
223    }
224    if let Some(t) = respond.text.as_ref() {
225        const LIMIT: usize = 40;
226        if t.chars().count() > LIMIT {
227            let truncated: String = t.chars().take(LIMIT).collect();
228            return format!("text: {}…", truncated);
229        }
230        return format!("text: {}", t);
231    }
232    if let Some(s) = respond.status.as_ref() {
233        return format!("status: {}", s);
234    }
235    "(empty)".to_owned()
236}