Skip to main content

apimock_config/
view.rs

1//! Read-only views on workspace state, and the command + result types
2//! the editing API uses.
3//!
4//! # 5.1.0 — spec alignment
5//!
6//! In 5.0.0 this module carried a placeholder shape defined only by
7//! rustdoc; 5.1.0 re-aligns it with the 5.1 spec:
8//!
9//! - `WorkspaceSnapshot { files, routes, diagnostics }` (spec §4.2)
10//! - each node carries `id: NodeId` + `source_file` + `toml_path` +
11//!   `display_name` + `kind` + `validation`
12//! - `EditCommand` is eight variants covering every editable action
13//!   (spec §4.3)
14//! - `ApplyResult { changed_nodes, diagnostics, requires_reload }`
15//!   (spec §4.4)
16//! - `SaveResult { changed_files, diff_summary, requires_reload }`
17//!   (spec §4.5) — populated in Step 4
18//! - `ValidationReport { diagnostics, is_valid }` (spec §4.6)
19//! - `Diagnostic { node_id, file, severity, message }` (spec §4.7)
20//!
21//! # Why UUIDs and not positional IDs
22//!
23//! The spec's §4.3 says "すべて NodeId で対象を指定". Positional IDs
24//! (`rule_sets[0].rules[3]`) would shift on every insert / delete /
25//! move, forcing the GUI to re-index its selection set after every
26//! edit. UUIDs are stable within a `Workspace` instance regardless of
27//! reordering.
28
29use serde::{Deserialize, Serialize};
30use serde_json::Value as JsonValue;
31use uuid::Uuid;
32
33use std::path::PathBuf;
34
35use apimock_routing::view::RouteCatalogSnapshot;
36
37/// Stable identifier for an editable node.
38///
39/// # Stability contract
40///
41/// Stable within one `Workspace` instance — that is, across any
42/// sequence of `apply()` calls. IDs are reassigned on fresh `load()`,
43/// which matches spec §10 "Workspace はメモリ上に独立インスタンスを持つ".
44#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
45#[serde(transparent)]
46pub struct NodeId(pub Uuid);
47
48impl NodeId {
49    pub fn new() -> Self {
50        Self(Uuid::new_v4())
51    }
52}
53
54impl Default for NodeId {
55    fn default() -> Self {
56        Self::new()
57    }
58}
59
60impl std::fmt::Display for NodeId {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        self.0.fmt(f)
63    }
64}
65
66/// Complete snapshot of the workspace state.
67///
68/// Shape matches spec §4.2 exactly. Consumed read-only by the GUI;
69/// mutated indirectly via `Workspace::apply`.
70#[derive(Clone, Debug, Serialize)]
71#[non_exhaustive]
72pub struct WorkspaceSnapshot {
73    /// All editable TOML files in the workspace, flattened. Each file
74    /// carries its own list of editable nodes.
75    pub files: Vec<ConfigFileView>,
76    /// Route overview pulled from the routing crate.
77    pub routes: RouteCatalogSnapshot,
78    /// Workspace-scoped issues (e.g. a root file that failed to load).
79    /// Per-node diagnostics live inside each `ConfigNodeView.validation`.
80    pub diagnostics: Vec<Diagnostic>,
81}
82
83impl WorkspaceSnapshot {
84    pub fn empty() -> Self {
85        Self {
86            files: Vec::new(),
87            routes: RouteCatalogSnapshot::empty(),
88            diagnostics: Vec::new(),
89        }
90    }
91}
92
93/// One TOML file inside the workspace.
94#[derive(Clone, Debug, Serialize)]
95#[non_exhaustive]
96pub struct ConfigFileView {
97    /// Absolute path on disk.
98    pub path: PathBuf,
99    /// Display name — typically the file name. Used as a tab title in
100    /// the GUI.
101    pub display_name: String,
102    /// What kind of file this is (root config, rule set, middleware).
103    pub kind: ConfigFileKind,
104    /// Editable nodes extracted from the file.
105    pub nodes: Vec<ConfigNodeView>,
106}
107
108#[derive(Clone, Copy, Debug, Serialize)]
109pub enum ConfigFileKind {
110    Root,
111    RuleSet,
112    Middleware,
113}
114
115/// One editable value inside a `ConfigFileView`.
116///
117/// Each node carries the six fields spec §4.2 makes mandatory.
118#[derive(Clone, Debug, Serialize)]
119#[non_exhaustive]
120pub struct ConfigNodeView {
121    /// Stable identifier — survives moves / renames within a Workspace
122    /// instance.
123    pub id: NodeId,
124    /// File the node was loaded from.
125    pub source_file: PathBuf,
126    /// Dotted TOML path inside `source_file` (e.g. `"listener.port"`,
127    /// `"rules[2].respond"`).
128    pub toml_path: String,
129    /// Human-readable label for UI list rendering (e.g. the rule's
130    /// `url_path` value, or `"Rule #3"` for a rule without one).
131    pub display_name: String,
132    /// Shape of the underlying value.
133    pub kind: NodeKind,
134    /// Per-node validation results.
135    pub validation: NodeValidation,
136}
137
138/// What shape of value a node holds. The variants are what the
139/// spec-defined `EditCommand` variants act on.
140#[derive(Clone, Copy, Debug, Serialize)]
141pub enum NodeKind {
142    /// Root config node — listener / log / service fields.
143    RootSetting,
144    /// One rule set loaded from a referenced TOML file.
145    RuleSet,
146    /// One rule inside a rule set.
147    Rule,
148    /// The `respond` block of a rule.
149    Respond,
150    /// File-based response node (fallback dir entry).
151    FileNode,
152    /// Script / middleware route.
153    Script,
154}
155
156/// Per-node validation result.
157///
158/// # Why validation is a field on the node and not a separate pass
159///
160/// GUIs render validation inline ("this field has a red underline").
161/// Keeping the validation result stapled to the node the GUI is about
162/// to render avoids a second lookup step in every render frame.
163#[derive(Clone, Debug, Default, Serialize)]
164pub struct NodeValidation {
165    /// Convenience flag — true iff `issues` is empty.
166    pub ok: bool,
167    /// Human-readable issues scoped to this node.
168    pub issues: Vec<ValidationIssue>,
169}
170
171impl NodeValidation {
172    pub fn ok() -> Self {
173        Self {
174            ok: true,
175            issues: Vec::new(),
176        }
177    }
178}
179
180#[derive(Clone, Debug, Serialize)]
181pub struct ValidationIssue {
182    pub severity: Severity,
183    pub message: String,
184}
185
186/// Structured edit command applied via `Workspace::apply`.
187///
188/// # Shape comes straight from spec §4.3
189///
190/// Each variant targets a node by NodeId (never by positional index).
191/// This guarantees edits remain well-defined across previous inserts /
192/// removes in the same GUI session.
193#[derive(Clone, Debug)]
194#[non_exhaustive]
195pub enum EditCommand {
196    /// Add a rule set file to the workspace.
197    ///
198    /// `path` is relative to the root config's directory — the same
199    /// convention as the value stored in `service.rule_sets`.
200    AddRuleSet {
201        path: String,
202    },
203    /// Remove a rule set by its NodeId. The underlying TOML file is
204    /// NOT deleted from disk — the workspace only removes the reference.
205    RemoveRuleSet {
206        id: NodeId,
207    },
208    /// Add a rule to an existing rule set.
209    AddRule {
210        parent: NodeId,
211        rule: RulePayload,
212    },
213    /// Update a rule's `when` / `respond` block.
214    ///
215    /// # Preservation of unspecified fields
216    ///
217    /// `RulePayload` carries `url_path`, `method`, and `respond` —
218    /// the fields a stage-1 GUI form exposes. A rule may also carry
219    /// `headers` and `body.json` match conditions that aren't part of
220    /// the payload shape. Those clauses are **preserved** across an
221    /// `UpdateRule`: the new rule keeps whatever headers / body
222    /// conditions the previous rule had, even though the payload
223    /// doesn't mention them.
224    ///
225    /// Without this preservation, every `UpdateRule` would silently
226    /// strip the unsurfaced clauses, which is a save-time bug when a
227    /// GUI re-saves a rule it loaded from a hand-edited TOML file.
228    UpdateRule {
229        id: NodeId,
230        rule: RulePayload,
231    },
232    /// Remove a rule by NodeId.
233    DeleteRule {
234        id: NodeId,
235    },
236    /// Reorder a rule within its parent rule set.
237    MoveRule {
238        id: NodeId,
239        new_index: usize,
240    },
241    /// Update the `respond` block of a rule.
242    UpdateRespond {
243        id: NodeId,
244        respond: RespondPayload,
245    },
246    /// Update a root-level setting (listener, log, service-level flags).
247    UpdateRootSetting {
248        key: RootSettingKey,
249        value: EditValue,
250    },
251
252    // ── Per-condition commands (RFC 016) ──────────────────────────────
253
254    /// Add a single header condition to an existing rule.
255    ///
256    /// `rule_id` must be the `NodeId` of the target rule.
257    AddHeaderCondition {
258        rule_id:   NodeId,
259        condition: HeaderConditionPayload,
260    },
261    /// Replace a header condition in-place, identified by its `NodeId`.
262    ///
263    /// The header name (`condition.name`) may differ from the original —
264    /// this counts as a rename, which reassigns the condition's `NodeId`.
265    UpdateHeaderCondition {
266        id:        NodeId,
267        condition: HeaderConditionPayload,
268    },
269    /// Remove a single header condition by its `NodeId`.
270    RemoveHeaderCondition {
271        id: NodeId,
272    },
273    /// Add a single body condition to an existing rule.
274    AddBodyCondition {
275        rule_id:   NodeId,
276        condition: BodyConditionPayload,
277    },
278    /// Replace a body condition in-place, identified by its `NodeId`.
279    UpdateBodyCondition {
280        id:        NodeId,
281        condition: BodyConditionPayload,
282    },
283    /// Remove a single body condition by its `NodeId`.
284    RemoveBodyCondition {
285        id: NodeId,
286    },
287}
288
289/// Stable identity for one condition, assigned at snapshot time.
290///
291/// Returned by [`Workspace::snapshot`] alongside each condition view so
292/// GUI code can target granular edit commands without reading index
293/// positions.
294#[derive(Clone, Debug)]
295pub struct ConditionWithId<V> {
296    pub id: NodeId,
297    pub view: V,
298}
299///
300/// # Preservation of unspecified fields (5.5.0 guarantee)
301///
302/// Fields set to `None` are preserved from the existing rule when this
303/// is an `UpdateRule` call. The `headers` and `body` fields use
304/// `Option<Vec<_>>` to distinguish three states:
305/// - `None` — preserve existing conditions.
306/// - `Some(vec![])` — clear all conditions.
307/// - `Some(vec![…])` — replace with the given set.
308///
309/// # URL path operator (RFC 001)
310///
311/// `url_path_op` controls which operator the routing crate uses to
312/// match the given `url_path` value. When `url_path_op` is `None` and
313/// `url_path` is `Some(_)`, the operator defaults to `Equal` (5.7.0
314/// behaviour). When `url_path` is `None`, both fields are ignored.
315///
316/// # Header and body conditions (RFC 002)
317///
318/// `headers` and `body` are optional lists of conditions. Each `None`
319/// preserves the existing rule's conditions; each `Some(_)` replaces
320/// them wholesale (an empty `Vec` clears them).
321#[derive(Clone, Debug, Default)]
322pub struct RulePayload {
323    pub url_path: Option<String>,
324    /// URL path match operator (RFC 001). `None` defaults to `Equal`.
325    pub url_path_op: Option<UrlPathOp>,
326    pub method: Option<String>,
327    /// Header conditions (RFC 002). `None` = preserve; `Some([])` = clear.
328    pub headers: Option<Vec<HeaderConditionPayload>>,
329    /// Body conditions (RFC 002). `None` = preserve; `Some([])` = clear.
330    pub body: Option<Vec<BodyConditionPayload>>,
331    pub respond: RespondPayload,
332}
333
334// ── RFC 001 — URL path operator ───────────────────────────────────────
335
336/// Operator for the URL path match in [`RulePayload`].
337///
338/// Mirrors the routing crate's internal operator set but lives in
339/// `apimock-config` to decouple the GUI-facing payload type from
340/// routing-internal types.
341#[derive(Clone, Copy, Debug, PartialEq, Eq)]
342pub enum UrlPathOp {
343    Equal,
344    StartsWith,
345    Contains,
346    EndsWith,
347    /// Glob wildcard match.
348    WildCard,
349    /// Regular expression match.
350    NotEqual,
351}
352
353// ── RFC 002 — Header and body condition payloads ──────────────────────
354
355/// One header condition in a [`RulePayload`].
356#[derive(Clone, Debug)]
357pub struct HeaderConditionPayload {
358    /// Header name (case-insensitive at match time).
359    pub name: String,
360    pub op: HeaderOp,
361    /// Required for all operators except `Exists` / `Absent`.
362    pub value: Option<String>,
363}
364
365/// Operator for a header condition.
366#[derive(Clone, Copy, Debug, PartialEq, Eq)]
367pub enum HeaderOp {
368    Equal,
369    Contains,
370    StartsWith,
371    EndsWith,
372    Regex,
373    /// Header must be present (any value).
374    Exists,
375    /// Header must be absent.
376    Absent,
377    NotEqual,
378    WildCard,
379}
380
381/// One body condition in a [`RulePayload`].
382#[derive(Clone, Debug)]
383pub struct BodyConditionPayload {
384    /// Currently only `Json`.
385    pub kind: BodyConditionKind,
386    /// Dotted path into the JSON body (not canonical JSONPath).
387    pub path: String,
388    pub op: BodyOp,
389    /// Configured comparison value.
390    pub value: serde_json::Value,
391}
392
393/// Body condition kind — currently only JSON.
394#[derive(Clone, Copy, Debug, PartialEq, Eq)]
395pub enum BodyConditionKind {
396    Json,
397}
398
399/// Operator for a body condition (RFC 002 / RFC 008 combined set).
400#[derive(Clone, Copy, Debug, PartialEq, Eq)]
401pub enum BodyOp {
402    // string-style
403    Equal,
404    EqualString,
405    Contains,
406    StartsWith,
407    EndsWith,
408    Regex,
409    // type-aware
410    EqualTyped,
411    // numeric
412    EqualNumber,
413    GreaterThan,
414    LessThan,
415    GreaterOrEqual,
416    LessOrEqual,
417    // presence
418    Exists,
419    Absent,
420    // array
421    ArrayLengthEqual,
422    ArrayLengthAtLeast,
423    ArrayContains,
424    // exact integer (RFC 010)
425    EqualInteger,
426}
427
428/// Payload for `UpdateRespond`.
429///
430/// The three fields are mutually specialised: exactly one of
431/// `file_path` / `text` / `status` should be populated. Validation
432/// catches cases that violate this.
433#[derive(Clone, Debug, Default)]
434pub struct RespondPayload {
435    pub file_path: Option<String>,
436    pub text: Option<String>,
437    pub status: Option<u16>,
438    pub delay_milliseconds: Option<u32>,
439}
440
441/// Enumerated root-level setting. Typed enum rather than free-form
442/// path so the apply-layer can exhaustively match without parsing.
443///
444/// # RFC 003 — TLS and Log variants
445///
446/// Seven new variants cover TLS configuration and log settings.
447/// Changes to TLS and listener fields require a full process restart
448/// (`HardRestart`); log-level and strategy changes only need a soft
449/// config reload (`SoftReload`).
450#[derive(Clone, Copy, Debug)]
451#[non_exhaustive]
452pub enum RootSettingKey {
453    // ── listener ──────────────────────────────────────────────────────
454    ListenerIpAddress,
455    ListenerPort,
456    // ── service ───────────────────────────────────────────────────────
457    ServiceFallbackRespondDir,
458    ServiceStrategy,
459    // ── TLS (RFC 003) ─────────────────────────────────────────────────
460    TlsEnabled,
461    TlsCertFile,
462    TlsKeyFile,
463    // ── log (RFC 003) ─────────────────────────────────────────────────
464    LogLevel,
465    LogFile,
466    LogFormat,
467    // ── file tree view (RFC 012) ──────────────────────────────────────
468    FileTreeShowHidden,
469    FileTreeBuiltinExcludes,
470    /// Value: `EditValue::StringList`
471    FileTreeExtraExcludes,
472    /// Value: `EditValue::StringList`
473    FileTreeInclude,
474}
475
476/// Value provided with an edit command.
477#[derive(Clone, Debug)]
478#[non_exhaustive]
479pub enum EditValue {
480    String(String),
481    Integer(i64),
482    Boolean(bool),
483    StringList(Vec<String>),
484    /// For settings whose domain is a small enum value (e.g.
485    /// `ServiceStrategy` → `"first_match"`).
486    Enum(String),
487    /// For completeness — callers can pass a raw JSON value when the
488    /// spec-defined key set is extended by stage-3 tooling. Currently
489    /// reserved; no stage-1 setting uses it.
490    Json(JsonValue),
491}
492
493/// Outcome of a successful `apply`.
494#[derive(Clone, Debug, Serialize)]
495#[non_exhaustive]
496pub struct ApplyResult {
497    /// Node IDs whose content (or position) changed.
498    pub changed_nodes: Vec<NodeId>,
499    /// Issues surfaced by applying the command (validation during apply
500    /// may add diagnostics — e.g. a new rule pointing at a missing file).
501    pub diagnostics: Vec<Diagnostic>,
502    /// `true` iff the server should reload to see this change. An edit
503    /// that changes the listener port needs a restart, not just a
504    /// reload — see `Workspace::save` for the richer `ReloadHint`.
505    pub requires_reload: bool,
506}
507
508/// Outcome of `Workspace::save`.
509#[derive(Clone, Debug, Serialize)]
510#[non_exhaustive]
511pub struct SaveResult {
512    /// TOML files actually written to disk.
513    pub changed_files: Vec<PathBuf>,
514    /// One entry per node that changed since last load.
515    pub diff_summary: Vec<DiffItem>,
516    pub requires_reload: bool,
517}
518
519/// One summary row in a `SaveResult::diff_summary`.
520#[derive(Clone, Debug, Serialize)]
521pub struct DiffItem {
522    pub kind: DiffKind,
523    pub target: NodeId,
524    pub summary: String,
525}
526
527#[derive(Clone, Copy, Debug, Serialize)]
528pub enum DiffKind {
529    Added,
530    Updated,
531    Removed,
532}
533
534/// Workspace-wide validation result. Mirrors spec §4.6.
535#[derive(Clone, Debug, Serialize)]
536pub struct ValidationReport {
537    pub diagnostics: Vec<Diagnostic>,
538    pub is_valid: bool,
539}
540
541impl ValidationReport {
542    pub fn ok() -> Self {
543        Self {
544            diagnostics: Vec::new(),
545            is_valid: true,
546        }
547    }
548}
549
550/// Human-readable notice about the workspace.
551#[derive(Clone, Debug, Serialize)]
552pub struct Diagnostic {
553    /// Target node, if any. `None` means "workspace-wide".
554    pub node_id: Option<NodeId>,
555    /// Target file, if the diagnostic is best reported at file level
556    /// (e.g. "could not read apimock-rule-set.toml"). May be `None` for
557    /// purely in-memory errors.
558    pub file: Option<PathBuf>,
559    pub severity: Severity,
560    pub message: String,
561}
562
563#[derive(Clone, Copy, Debug, Serialize)]
564pub enum Severity {
565    Error,
566    Warning,
567    Info,
568}
569
570// ---------------------------------------------------------------------------
571// Reload hint — spec §9. The same enum shape was defined in 5.0.0;
572// 5.1 reuses it unchanged so existing consumers keep working.
573// ---------------------------------------------------------------------------
574
575/// Advisory indicating what, if anything, the server needs to do in
576/// response to a config change.
577///
578/// # RFC 003 — Reload semantics
579///
580/// | Key group                     | Hint           |
581/// |-------------------------------|----------------|
582/// | `ListenerIpAddress/Port`      | `HardRestart`  |
583/// | `TlsEnabled`, `TlsCert*`      | `HardRestart`  |
584/// | `LogFile`                     | `HardRestart`  |
585/// | `ServiceFallbackRespondDir`   | `SoftReload`   |
586/// | `ServiceStrategy`             | `SoftReload`   |
587/// | `LogLevel`, `LogFormat`       | `SoftReload`   |
588///
589/// The hint is advisory — the server does not auto-restart. The GUI
590/// surfaces it to the user.
591#[derive(Clone, Copy, Debug, Default, Serialize)]
592pub struct ReloadHint {
593    /// Server can re-read config without rebinding the listener.
594    pub requires_reload: bool,
595    /// Process must restart (rebind socket, reload TLS, reopen log file).
596    pub requires_restart: bool,
597}
598
599impl ReloadHint {
600    pub fn none() -> Self {
601        Self::default()
602    }
603
604    /// Config can be hot-reloaded without a process restart.
605    pub fn reload() -> Self {
606        Self {
607            requires_reload: true,
608            requires_restart: false,
609        }
610    }
611
612    /// Process must fully restart for this change to take effect.
613    pub fn restart() -> Self {
614        Self {
615            requires_reload: false,
616            requires_restart: true,
617        }
618    }
619
620    /// Return the hint appropriate for the given [`RootSettingKey`].
621    pub fn for_key(key: RootSettingKey) -> Self {
622        use RootSettingKey::*;
623        match key {
624            ListenerIpAddress | ListenerPort | TlsEnabled | TlsCertFile | TlsKeyFile
625            | LogFile => Self::restart(),
626            ServiceFallbackRespondDir | ServiceStrategy | LogLevel | LogFormat
627            | FileTreeShowHidden | FileTreeBuiltinExcludes | FileTreeExtraExcludes
628            | FileTreeInclude => Self::reload(),
629        }
630    }
631}