wyvern-schema 0.3.0

Wyvern JSON types, validation, and error messages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Wizard command, stack, and HTTP wire types (Phase D).

use std::fmt;
use std::ops::Deref;

use serde::{Deserialize, Serialize};

use crate::button::ButtonLabel;

/// Error when a wizard page identity field is empty.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WizardPageFieldError;

impl fmt::Display for WizardPageFieldError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("wizard page field must be a non-empty string")
    }
}

impl std::error::Error for WizardPageFieldError {}

macro_rules! wizard_page_newtype {
    ($(#[$meta:meta])* $name:ident, $doc:literal) => {
        $(#[$meta])*
        #[doc = $doc]
        ///
        /// Construct via [`Self::try_new`] at the validation boundary so downstream
        /// code can treat the value as already checked non-empty.
        #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
        #[serde(transparent)]
        pub struct $name(String);

        impl $name {
            /// Wrap a validated non-empty string.
            ///
            /// Prefer [`Self::try_new`] at trust boundaries; this constructor is for
            /// already-validated values (e.g. after [`crate::validate`]).
            pub fn new(value: impl Into<String>) -> Self {
                Self(value.into())
            }

            /// Construct from a non-empty string.
            ///
            /// # Errors
            ///
            /// Returns [`WizardPageFieldError`] when `value` is empty.
            pub fn try_new(value: impl Into<String>) -> Result<Self, WizardPageFieldError> {
                let value = value.into();
                if value.is_empty() {
                    return Err(WizardPageFieldError);
                }
                Ok(Self(value))
            }

            /// Borrow as a string slice.
            pub fn as_str(&self) -> &str {
                &self.0
            }

            /// Consume and return the inner string.
            pub fn into_inner(self) -> String {
                self.0
            }
        }

        impl Deref for $name {
            type Target = str;

            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }

        impl AsRef<str> for $name {
            fn as_ref(&self) -> &str {
                self.as_str()
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                self.0.fmt(f)
            }
        }

        // No infallible `From<String>` / `From<&str>` — those bypass `try_new`
        // non-empty validation. Use `try_new` at trust boundaries; `new` only
        // after validation (e.g. `crate::validate`).

        impl PartialEq<str> for $name {
            fn eq(&self, other: &str) -> bool {
                self.0 == other
            }
        }

        impl PartialEq<&str> for $name {
            fn eq(&self, other: &&str) -> bool {
                self.0 == *other
            }
        }
    };
}

wizard_page_newtype!(WizardPageId, "Validated wizard page id (non-empty).");
wizard_page_newtype!(WizardPageTitle, "Validated wizard page title (non-empty).");
wizard_page_newtype!(
    WizardPageHtml,
    "Validated wizard page HTML path relative to `--ui-root` (non-empty)."
);

/// Per-page layout hint (`dialog` | `workspace`).
///
/// Validated in d.1; sizing behavior lands in d.6.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WizardPageLayout {
    /// Typical form step (default when omitted).
    Dialog,
    /// HTML page requesting a viewport-sized canvas.
    Workspace,
}

impl WizardPageLayout {
    /// Parse a wire layout name (`dialog`, `workspace`).
    pub fn parse(value: &str) -> Option<Self> {
        match value {
            "dialog" => Some(Self::Dialog),
            "workspace" => Some(Self::Workspace),
            _ => None,
        }
    }

    /// All valid wire names (for error messages / suggestions).
    pub fn all_names() -> &'static [&'static str] {
        &["dialog", "workspace"]
    }

    /// Wire name for this layout.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Dialog => "dialog",
            Self::Workspace => "workspace",
        }
    }
}

/// Minimal page descriptor (REQ-0026).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WizardPageDescriptor {
    /// Stable page identity.
    pub id: WizardPageId,
    /// Display title for the page.
    pub title: WizardPageTitle,
    /// HTML path relative to `--ui-root` (no separate `page_html` field).
    pub html: WizardPageHtml,
    /// Optional per-page layout (`dialog` | `workspace`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub layout: Option<WizardPageLayout>,
}

/// One prior stack entry (REQ-0024).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WizardStackEntry {
    /// Page that was visited.
    pub page: WizardPageDescriptor,
    /// Opaque page data stored for that visit.
    pub data: serde_json::Value,
}

/// Validated wizard ingress after schema validation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WizardCommand {
    /// Initial page descriptor.
    pub page: WizardPageDescriptor,
    /// Opaque wizard-wide config (never inspected by the host).
    pub config: serde_json::Value,
    /// Optional viewer width hint.
    pub width: Option<u32>,
    /// Optional viewer height hint.
    pub height: Option<u32>,
}

/// Wizard stdout / finish body shape (REQ-0066).
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WizardResult {
    /// Terminal button (`finish` | `cancel` | `dismissed`).
    pub button: ButtonLabel,
    /// Opaque final page data (empty on cancel / dismiss).
    pub data: serde_json::Value,
    /// Visited stack (semantics finalized in d.2).
    pub stack: Vec<WizardStackEntry>,
}

impl WizardResult {
    /// REQ-0066 / REQ-0097 dismissed shape with empty stack.
    ///
    /// Prefer session-derived dismiss via wizard `finish(Dismissed, …)` (d.2 / d.8)
    /// when a live wizard session exists — that path returns the full visited stack.
    /// This helper remains for degraded fallbacks and non-session call sites.
    pub fn dismissed() -> Self {
        Self {
            button: ButtonLabel::dismissed(),
            data: serde_json::json!({}),
            stack: Vec::new(),
        }
    }
}

/// Wire DTO for `GET /api/wizard/state` (HTTP-TYPES.md).
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WizardStateResponse {
    /// Always `"wizard"`.
    #[serde(rename = "type")]
    pub type_name: &'static str,
    /// Opaque wizard-wide config.
    pub config: serde_json::Value,
    /// Current page descriptor.
    pub page: WizardPageDescriptor,
    /// Opaque data for the current page.
    pub page_data: serde_json::Value,
    /// Prior stack entries only (`entries[0..cursor]`, exclusive of current).
    pub stack: Vec<WizardStackEntry>,
    /// Optional viewer width from the command.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub width: Option<u32>,
    /// Optional viewer height from the command.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub height: Option<u32>,
}

impl WizardStateResponse {
    /// Build a state response from a session snapshot plus optional window hints.
    pub fn from_snapshot(
        config: serde_json::Value,
        page: WizardPageDescriptor,
        page_data: serde_json::Value,
        stack: Vec<WizardStackEntry>,
        width: Option<u32>,
        height: Option<u32>,
    ) -> Self {
        Self {
            type_name: "wizard",
            config,
            page,
            page_data,
            stack,
            width,
            height,
        }
    }
}

/// Wire: `"next"` | `"back"` only. Cancel/finish/dismissed use `POST /api/wizard/finish`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WizardNavAction {
    /// Advance to an explicit next page descriptor.
    Next,
    /// Move the cursor back without truncating forward history.
    Back,
}

/// Wire DTO for `POST /api/wizard/navigate` (HTTP-TYPES.md).
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct WizardNavigateRequest {
    /// `"next"` or `"back"` only — terminal actions use `/finish`.
    pub action: WizardNavAction,
    /// Opaque page payload for the current entry (whole-blob replace).
    #[serde(default)]
    pub data: serde_json::Value,
    /// Optional validation hint; when set on `next`, must equal `next.id`.
    #[serde(default)]
    pub page_id: Option<WizardPageId>,
    /// Required when `action` is `next` — destination page descriptor.
    #[serde(default)]
    pub next: Option<WizardPageDescriptor>,
}

/// Terminal finish buttons accepted by `POST /api/wizard/finish`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WizardTerminalButton {
    /// Complete successfully with full visited stack.
    Finish,
    /// Abort — empty stack and empty data on stdout.
    Cancel,
    /// Viewer dismissed — full visited stack, empty data.
    Dismissed,
}

impl WizardTerminalButton {
    /// Wire name for this button.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Finish => "finish",
            Self::Cancel => "cancel",
            Self::Dismissed => "dismissed",
        }
    }

    /// Convert to the stdout [`ButtonLabel`] shape.
    pub fn to_button_label(self) -> ButtonLabel {
        ButtonLabel::new(self.as_str())
    }
}

/// Successful navigate response — viewer reloads `url`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WizardNavigateResponse {
    /// Always `true` on HTTP 200.
    pub ok: bool,
    /// Absolute URL of the destination wizard page under `/wizard/`.
    pub url: String,
}

/// Wire DTO for `POST /api/wizard/finish` (HTTP-TYPES.md).
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct WizardFinishRequest {
    /// Terminal button (`finish` | `cancel` | `dismissed`).
    pub button: WizardTerminalButton,
    /// Opaque final page data (ignored for cancel / dismissed stdout `data`).
    pub data: serde_json::Value,
    /// Client-supplied full visited stack (validated for finish / dismissed).
    pub stack: Vec<WizardStackEntry>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn wizard_state_response_wire_shape_first_page() {
        let resp = WizardStateResponse::from_snapshot(
            serde_json::json!({"theme": "dark"}),
            WizardPageDescriptor {
                id: WizardPageId::new("start"),
                title: WizardPageTitle::new("Start"),
                html: WizardPageHtml::new("pages/start.html"),
                layout: None,
            },
            serde_json::json!({}),
            Vec::new(),
            Some(640),
            Some(480),
        );
        let value = serde_json::to_value(&resp).expect("serialize");
        assert_eq!(value["type"], "wizard");
        assert_eq!(value["config"]["theme"], "dark");
        assert_eq!(value["page"]["id"], "start");
        assert_eq!(value["page_data"], serde_json::json!({}));
        assert_eq!(value["stack"], serde_json::json!([]));
        assert_eq!(value["width"], 640);
        assert_eq!(value["height"], 480);
    }

    #[test]
    fn page_layout_omitted_when_none() {
        let page = WizardPageDescriptor {
            id: WizardPageId::new("a"),
            title: WizardPageTitle::new("A"),
            html: WizardPageHtml::new("a.html"),
            layout: None,
        };
        let value = serde_json::to_value(&page).expect("serialize");
        assert!(value.get("layout").is_none());
    }

    #[test]
    fn page_id_try_new_rejects_empty() {
        assert_eq!(WizardPageId::try_new(""), Err(WizardPageFieldError));
        assert_eq!(WizardPageId::try_new("ok").unwrap().as_str(), "ok");
    }

    #[test]
    fn navigate_request_deserializes_typed_page_id() {
        let req: WizardNavigateRequest = serde_json::from_value(serde_json::json!({
            "action": "next",
            "data": {},
            "page_id": "step-2",
            "next": {
                "id": "step-2",
                "title": "Step 2",
                "html": "pages/step-2.html"
            }
        }))
        .expect("deserialize");
        assert_eq!(
            req.page_id.as_ref().map(WizardPageId::as_str),
            Some("step-2")
        );
        assert_eq!(req.next.as_ref().unwrap().id.as_str(), "step-2");
    }

    #[test]
    fn terminal_button_wire_round_trip() {
        for (wire, expected) in [
            ("finish", WizardTerminalButton::Finish),
            ("cancel", WizardTerminalButton::Cancel),
            ("dismissed", WizardTerminalButton::Dismissed),
        ] {
            let parsed: WizardTerminalButton =
                serde_json::from_value(serde_json::json!(wire)).expect("parse");
            assert_eq!(parsed, expected);
            assert_eq!(parsed.as_str(), wire);
            assert_eq!(parsed.to_button_label().as_str(), wire);
        }
    }
}