Skip to main content

apimock_routing/
view.rs

1//! Read-only views on routing state for GUI tooling.
2//!
3//! # Stage-1 scope (5.0.0)
4//!
5//! Per the 5.0.0 brief, this module defines the *shape* of the read-only
6//! API that a future GUI will depend on. The types are declared with
7//! their fields and rustdoc-annotated responsibilities; populating them
8//! from a live `RuleSet` is stage-2 work. We deliberately ship the
9//! signatures first so that:
10//!
11//! 1. the GUI work can start against a frozen type surface,
12//! 2. any downstream code (docs site, dashboards) can begin modelling
13//!    against stable identifiers, and
14//! 3. field additions in later stages are additive rather than
15//!    reshaping.
16//!
17//! Every type here is `#[non_exhaustive]` so adding fields later is not
18//! a breaking change.
19//!
20//! # What these types deliberately hide
21//!
22//! A `RouteCatalogSnapshot` does NOT include execution state (compiled
23//! Rhai AST, open file handles, etc.). It is a photograph of the
24//! declarative routing configuration at one moment — the kind of
25//! information a GUI shows in a "routes" panel, not the live runtime.
26
27use serde::Serialize;
28use serde_json;
29
30pub mod build;
31
32/// A complete snapshot of the server's routing configuration at one moment.
33///
34/// # Why a snapshot rather than a live reference
35///
36/// GUIs navigate, filter, and diff. A borrowed live reference would
37/// require the GUI to hold a lock on the running server's state — not
38/// feasible across an async boundary or an IPC channel. Snapshots are
39/// cheap to clone, cheap to send, and never block the server.
40#[derive(Clone, Debug, Serialize)]
41#[non_exhaustive]
42pub struct RouteCatalogSnapshot {
43    /// Rule sets in the same order as they would be evaluated at request time.
44    pub rule_sets: Vec<RuleSetView>,
45    /// Fallback respond dir (file-based zero-config responder).
46    pub fallback_respond_dir: Option<String>,
47    /// Top-level entries of the fallback respond dir, depth-1 eager.
48    /// `None` when no fallback dir is configured or it doesn't exist
49    /// on disk. Subdirectory contents are not pre-populated; the
50    /// embedder calls `Workspace::list_directory(parent_id)` to expand
51    /// nodes on demand.
52    pub file_tree: Option<FileTreeView>,
53    /// Middleware-script routes, keyed by `service.middlewares` order.
54    pub script_routes: Vec<ScriptRouteView>,
55}
56
57impl RouteCatalogSnapshot {
58    /// Return a snapshot with no content.
59    pub fn empty() -> Self {
60        Self {
61            rule_sets: Vec::new(),
62            fallback_respond_dir: None,
63            file_tree: None,
64            script_routes: Vec::new(),
65        }
66    }
67}
68
69/// GUI-facing view of one rule set.
70#[derive(Clone, Debug, Serialize)]
71#[non_exhaustive]
72pub struct RuleSetView {
73    /// Index within the parent [`RouteCatalogSnapshot::rule_sets`] list.
74    /// Identifies the rule set across edit commands.
75    pub index: usize,
76    /// Source file this rule set was loaded from (relative to the project).
77    pub source_path: String,
78    /// Optional URL path prefix shared across every rule in this set.
79    pub url_path_prefix: Option<String>,
80    /// Optional respond-dir prefix shared across every rule in this set.
81    pub respond_dir_prefix: Option<String>,
82    /// The rules, in evaluation order.
83    pub rules: Vec<RuleView>,
84}
85
86/// GUI-facing view of one rule.
87#[derive(Clone, Debug, Serialize)]
88#[non_exhaustive]
89pub struct RuleView {
90    /// Zero-based index within the parent rule set.
91    pub index: usize,
92    /// Structured match conditions. Spec §5.3 — URL / method /
93    /// headers / JSON conditions. Each field is `Option`-typed so a
94    /// rule that only matches on URL doesn't carry stub values in the
95    /// other slots.
96    pub when: WhenView,
97    /// The declarative response shape.
98    pub respond: RespondView,
99}
100
101impl RuleView {
102    /// One-line text label for list-row rendering. Backwards-compat
103    /// helper that produces the same shape as 5.0–5.2's
104    /// `when_summary: String` field.
105    pub fn summary(&self) -> String {
106        self.when.summary()
107    }
108}
109
110/// Structured representation of a rule's `when` clause (spec §5.3).
111///
112/// # RFC 004 — Structured WhenView
113///
114/// The boolean `has_header_conditions` / `has_body_conditions` flags
115/// from 5.3.0 are replaced with typed `Vec` fields. The GUI can now
116/// render the full list of conditions in a rule list without a second
117/// query. Use `headers.is_empty()` / `body.is_empty()` wherever the
118/// old boolean flags were checked.
119///
120/// # Why each field is `Option`
121///
122/// A rule with `when.request.url_path = "/api"` and no other clauses
123/// matches every request whose URL is `/api`, regardless of method or
124/// headers. Carrying explicit `None`s for unset fields keeps the
125/// distinction between "not constrained" and "constrained to empty".
126#[derive(Clone, Debug, Default, Serialize)]
127#[non_exhaustive]
128pub struct WhenView {
129    /// URL-path predicate. `None` when the rule has no URL-path constraint.
130    pub url_path: Option<UrlPathView>,
131    /// HTTP method — uppercase string like `"GET"`.
132    pub method: Option<String>,
133    /// Structured header conditions (RFC 004). Empty when none.
134    pub headers: Vec<HeaderConditionView>,
135    /// Structured body conditions (RFC 004). Empty when none.
136    pub body: Vec<BodyConditionView>,
137}
138
139impl WhenView {
140    /// Compact human-readable summary.
141    pub fn summary(&self) -> String {
142        let mut parts: Vec<String> = Vec::new();
143        if let Some(method) = self.method.as_deref() {
144            parts.push(method.to_owned());
145        }
146        if let Some(url) = self.url_path.as_ref() {
147            parts.push(url.summary());
148        }
149        if !self.headers.is_empty() {
150            parts.push(format!("+headers({})", self.headers.len()));
151        }
152        if !self.body.is_empty() {
153            parts.push(format!("+body({})", self.body.len()));
154        }
155        if parts.is_empty() {
156            "(matches everything)".to_owned()
157        } else {
158            parts.join(" ")
159        }
160    }
161}
162
163/// One header condition in a `WhenView`.
164///
165/// # RFC 016 — per-condition identity
166///
167/// A `NodeId` is **not** stored in this routing-crate type; the
168/// routing crate doesn't depend on `apimock-config` or its `NodeId`
169/// type. Instead, the config crate's snapshot layer wraps each view
170/// in a `ConditionWithId` that pairs the routing view with a `NodeId`.
171/// GUI code that needs to issue granular edit commands should use
172/// those wrapped types via the snapshot API.
173#[derive(Clone, Debug, Serialize)]
174#[non_exhaustive]
175pub struct HeaderConditionView {
176    /// Header name as stored (lower-cased at parse time).
177    pub name: String,
178    /// Operator in `snake_case` TOML form, e.g. `"equal"`, `"contains"`.
179    pub op: String,
180    /// Configured value. `None` when the operator implies no value.
181    pub value: Option<String>,
182}
183
184/// One body condition in a `WhenView`.
185#[derive(Clone, Debug, Serialize)]
186#[non_exhaustive]
187pub struct BodyConditionView {
188    /// Body kind — currently always `"json"`.
189    pub kind: String,
190    /// Dotted path into the JSON body.
191    pub path: String,
192    /// Operator in `snake_case` form.
193    pub op: String,
194    /// Configured value as a JSON value.
195    pub value: serde_json::Value,
196}
197
198/// URL-path predicate detail.
199#[derive(Clone, Debug, Serialize)]
200#[non_exhaustive]
201pub struct UrlPathView {
202    /// The path string from the rule, e.g. `"/api/v1/users"`.
203    pub value: String,
204    /// Matching operator name in lowercase TOML form, e.g.
205    /// `"equals"` or `"starts_with"`.
206    pub op: String,
207}
208
209impl UrlPathView {
210    pub fn summary(&self) -> String {
211        format!("{} {}", self.op, self.value)
212    }
213}
214
215/// GUI-facing view of one response shape.
216#[derive(Clone, Debug, Serialize)]
217#[non_exhaustive]
218pub enum RespondView {
219    /// Serve a file. The path is resolved against the rule set's
220    /// `respond_dir_prefix` at request time.
221    File { path: String, csv_records_key: Option<String> },
222    /// Return a literal text body. `status` is the response code to use
223    /// (defaults to 200 when absent).
224    Text { text: String, status: Option<u16> },
225    /// Return an empty body with just this status code.
226    Status { code: u16 },
227}
228
229/// Shown to the user when they ask "what rule would match *this* request?".
230///
231/// # Why we carry both the match and the non-matches
232///
233/// A GUI debugger doesn't just answer "which rule matched" — it answers
234/// "why didn't the rule I expected match?". Surfacing the mismatches
235/// lets the UI highlight the first failing predicate on each rule.
236#[derive(Clone, Debug, Serialize)]
237#[non_exhaustive]
238pub struct RouteMatchView {
239    /// Matching rule, if any. `None` means the request would fall through
240    /// to the dynamic-route fallback.
241    pub matched: Option<MatchedRule>,
242    /// Every rule the matcher considered before deciding, with the
243    /// reason it was skipped. Order matches evaluation order.
244    pub considered: Vec<MatchConsidered>,
245}
246
247#[derive(Clone, Debug, Serialize)]
248#[non_exhaustive]
249pub struct MatchedRule {
250    pub rule_set_index: usize,
251    pub rule_index: usize,
252}
253
254#[derive(Clone, Debug, Serialize)]
255#[non_exhaustive]
256pub struct MatchConsidered {
257    pub rule_set_index: usize,
258    pub rule_index: usize,
259    /// Free-form text describing why this rule was skipped
260    /// (e.g. `"url_path mismatch"`, `"header 'authorization' missing"`).
261    pub reason: String,
262}
263
264/// Summary of every validation issue found in a [`RouteCatalogSnapshot`].
265///
266/// `ok` iff `issues` is empty; a GUI can render `ok = true` as a green
267/// banner and iterate `issues` otherwise.
268#[derive(Clone, Debug, Serialize)]
269#[non_exhaustive]
270pub struct RouteValidation {
271    pub ok: bool,
272    pub issues: Vec<RouteValidationIssue>,
273}
274
275impl RouteValidation {
276    pub fn ok() -> Self {
277        Self {
278            ok: true,
279            issues: Vec::new(),
280        }
281    }
282}
283
284#[derive(Clone, Debug, Serialize)]
285#[non_exhaustive]
286pub struct RouteValidationIssue {
287    /// Which rule-set this issue came from.
288    pub rule_set_index: usize,
289    /// Which rule within the set, if the issue is rule-scoped.
290    pub rule_index: Option<usize>,
291    /// Severity as the GUI should render it.
292    pub severity: ValidationSeverity,
293    /// Human-readable description.
294    pub message: String,
295}
296
297#[derive(Clone, Copy, Debug, Serialize)]
298pub enum ValidationSeverity {
299    Error,
300    Warning,
301}
302
303// ---------------------------------------------------------------------
304// File-tree view (spec §5.5)
305// ---------------------------------------------------------------------
306
307/// Top-level view of the fallback respond directory, depth-1 eager.
308///
309/// # Why depth-1 and not full recursion
310///
311/// Fallback dirs in real projects can hold thousands of files; full
312/// recursive enumeration would make `snapshot()` expensive. The
313/// `Workspace` provides a separate `list_directory(parent_id)` API
314/// the GUI calls when a user clicks to expand a subdirectory.
315#[derive(Clone, Debug, Serialize)]
316#[non_exhaustive]
317pub struct FileTreeView {
318    /// Absolute path to the fallback respond directory.
319    pub root_path: String,
320    /// Direct children of `root_path`. Subdirectories carry no
321    /// children (`children: None`) — the embedder loads them on demand.
322    pub entries: Vec<FileNodeView>,
323}
324
325#[derive(Clone, Debug, Serialize)]
326#[non_exhaustive]
327pub struct FileNodeView {
328    /// Display name (just the last path component, e.g. `"users.json"`).
329    pub name: String,
330    /// Absolute path on disk.
331    pub path: String,
332    /// What kind of filesystem node this is.
333    pub kind: FileNodeKind,
334    /// For files only — the URL path that would serve this file under
335    /// the dyn-route fallback (e.g. `"/users"` for `users.json`).
336    /// `None` for directories.
337    pub route_hint: Option<String>,
338    /// `Some(empty)` for an unexpanded subdirectory, populated when
339    /// the embedder calls `list_directory` to expand. Always `None`
340    /// for files.
341    pub children: Option<Vec<FileNodeView>>,
342}
343
344#[derive(Clone, Copy, Debug, Serialize)]
345pub enum FileNodeKind {
346    File,
347    Directory,
348}
349
350// ---------------------------------------------------------------------
351// Script-route view (spec §5)
352// ---------------------------------------------------------------------
353
354/// Minimal display info for a Rhai middleware script route.
355///
356/// # Why fields are intentionally minimal
357///
358/// A Rhai middleware can run arbitrary logic to decide whether to
359/// match. Static analysis of "what URLs does this script handle" isn't
360/// feasible without parsing Rhai (and would be unreliable even then).
361/// The view reports only what we *do* know statically — file path and
362/// display label — and leaves any deeper inspection to a hypothetical
363/// future editor feature.
364#[derive(Clone, Debug, Serialize)]
365#[non_exhaustive]
366pub struct ScriptRouteView {
367    /// Index within `service.middlewares_file_paths`.
368    pub index: usize,
369    /// Source file path as recorded in `service.middlewares`.
370    pub source_file: String,
371    /// Human-readable label (typically the file's basename).
372    pub display_name: String,
373}