quillmark-core 0.95.1

Core types and functionality for the Quillmark schema-driven document engine
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! Canonical **live** wire form of a [`Card`] for language-binding APIs.
//!
//! [`CardWire`] is the single, core-owned translation between a [`Card`] and
//! the flat `{ kind, payloadItems, … }` shape that the WASM and Python bindings
//! exchange with JS/Python. Bindings serialize/deserialize this type instead of
//! hand-rolling their own per-card conversion, so the field/comment/`$`-entry
//! mapping lives in exactly one place.
//!
//! ## Why this is separate from the storage DTO
//!
//! The versioned storage DTO (`document::dto`, e.g. `CardV0_92_0`) is **frozen**
//! per schema version so persisted documents keep loading forever. `CardWire`
//! is the **current** API shape and is free to evolve with the bindings. They
//! are structurally similar today, but coupling the live API to a frozen
//! storage schema would chain one to the other's change cadence — so they are
//! deliberately distinct, both built on the live [`Card`]/[`Payload`] model.
//!
//! ## Shape
//!
//! The `$` system entries are hoisted to named fields (`kind`, `quill`, `id`,
//! `ext`, `seed`); `payload_items` carries only user fields and comments, in order.
//! Field/`$ext` *nested* comments are not represented here — they survive the
//! Markdown and storage round-trips, not this editable projection.

use std::str::FromStr;

use serde::{Deserialize, Serialize};
use serde_json::{Map as JsonMap, Value as JsonValue};

use super::payload::{MetaKey, Payload, PayloadItem};
use super::Card;
use crate::value::{PathSegment, QuillValue};
use crate::version::QuillReference;
use quillmark_content::Content;

/// One entry in a [`CardWire`]'s `payload_items`: a user field or a comment.
/// The `$` system entries are hoisted onto [`CardWire`] itself, never here.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum PayloadItemWire {
    /// A user-defined field.
    Field {
        key: String,
        value: JsonValue,
        /// `true` when the field itself is `key: !must_fill <value>` in source.
        #[serde(default)]
        fill: bool,
        /// Paths to `!must_fill` markers nested *inside* `value` (e.g. a leaf
        /// property of an object, or a key within an array element). The JSON
        /// `value` projection is fill-free, so these carry the nested markers
        /// across the wire. Empty for a top-level-only or no-fill field.
        #[serde(
            default,
            rename = "nestedFills",
            alias = "nested_fills",
            skip_serializing_if = "Vec::is_empty"
        )]
        nested_fills: Vec<Vec<PathStepWire>>,
    },
    /// A YAML comment line (text excludes the leading `#`).
    Comment {
        text: String,
        /// `true` for a trailing inline comment (`field: value # text`).
        #[serde(default)]
        inline: bool,
    },
}

/// One step in a nested fill path: an object key or an array index. Serializes
/// **untagged** — a key as a JSON string, an index as a JSON number — so a path
/// is a plain JS array like `["addr", "street"]` or `["recipients", 0, "name"]`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PathStepWire {
    Index(usize),
    Key(String),
}

impl From<&PathSegment> for PathStepWire {
    fn from(seg: &PathSegment) -> Self {
        match seg {
            PathSegment::Key(k) => PathStepWire::Key(k.clone()),
            PathSegment::Index(i) => PathStepWire::Index(*i),
        }
    }
}

impl From<&PathStepWire> for PathSegment {
    fn from(seg: &PathStepWire) -> Self {
        match seg {
            PathStepWire::Key(k) => PathSegment::Key(k.clone()),
            PathStepWire::Index(i) => PathSegment::Index(*i),
        }
    }
}

/// Canonical live wire form of a [`Card`]. See the module docs.
///
/// Serializes to JS-facing camelCase (`payloadItems`); the snake_case
/// `payload_items` is also accepted on input for the Python binding.
/// `deny_unknown_fields` makes a stale flat `{ kind, fields }` shape fail
/// loudly rather than deserialize into an empty card.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct CardWire {
    /// The block's `$kind` (e.g. `"endorsement"`); empty string when the block
    /// declares no `$kind`. Kept non-optional to match the binding read shape.
    #[serde(default)]
    pub kind: String,
    /// The block's `$quill` reference string (`name@version`), present on the
    /// main card only. Omitted when absent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub quill: Option<String>,
    /// The block's `$id`, if any. Omitted when absent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// The block's opaque `$ext` map, if declared. Omitted when absent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ext: Option<JsonMap<String, JsonValue>>,
    /// The block's `$seed` map (keyed by card-kind), if declared. Present on
    /// the main card only. Omitted when absent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub seed: Option<JsonMap<String, JsonValue>>,
    /// User fields and comments, in source order.
    #[serde(default, alias = "payload_items")]
    pub payload_items: Vec<PayloadItemWire>,
    /// The card body as canonical Content-JSON — the source-of-truth content
    /// model (a content object, `{text, lines, marks, islands}`). The empty content
    /// when absent. A markdown string is also accepted on input (imported), so an
    /// LLM/markdown writer can still hand a string here.
    ///
    /// No `body_markdown` projection rides this wire: the eager `export ∘ body`
    /// precompute was dropped (the delimiter-safety fix makes `to_markdown`
    /// re-parse every rendered line, so it is no longer cheap) in favor of the
    /// on-demand `exportMarkdown(body)` codec at the binding boundary.
    #[serde(default)]
    pub body: JsonValue,
}

/// Failure converting a [`CardWire`] back into a [`Card`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WireError {
    /// The `quill` string is not a valid `name@version` reference.
    InvalidQuillReference { value: String, reason: String },
    /// A field violates the payload invariant: a name failing
    /// `[A-Za-z_][A-Za-z0-9_]*`, or a value (including `$ext`) nesting past the
    /// §8 depth limit.
    InvalidField { key: String, reason: String },
}

impl std::fmt::Display for WireError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WireError::InvalidQuillReference { value, reason } => {
                write!(f, "invalid `quill` reference {value:?}: {reason}")
            }
            WireError::InvalidField { key, reason } => {
                write!(f, "invalid field {key:?}: {reason}")
            }
        }
    }
}

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

impl From<&Card> for CardWire {
    fn from(card: &Card) -> Self {
        let mut wire = CardWire {
            kind: String::new(),
            quill: None,
            id: None,
            ext: None,
            seed: None,
            payload_items: Vec::new(),
            body: quillmark_content::serial::to_canonical_value(card.body()),
        };
        for item in card.payload().items() {
            match item {
                PayloadItem::Quill { reference } => wire.quill = Some(reference.to_string()),
                PayloadItem::Kind { value } => wire.kind = value.clone(),
                PayloadItem::Id { value } => wire.id = Some(value.clone()),
                PayloadItem::Meta {
                    key: MetaKey::Ext,
                    value,
                    ..
                } => wire.ext = Some(value.clone()),
                PayloadItem::Meta {
                    key: MetaKey::Seed,
                    value,
                    ..
                } => wire.seed = Some(value.clone()),
                PayloadItem::Field {
                    key, value, fill, ..
                } => {
                    let nested_fills = value
                        .nonroot_fill_paths()
                        .map(|p| p.iter().map(PathStepWire::from).collect())
                        .collect();
                    wire.payload_items.push(PayloadItemWire::Field {
                        key: key.clone(),
                        value: value.as_json().clone(),
                        fill: *fill,
                        nested_fills,
                    })
                }
                PayloadItem::Comment { text, inline } => {
                    wire.payload_items.push(PayloadItemWire::Comment {
                        text: text.clone(),
                        inline: *inline,
                    })
                }
            }
        }
        wire
    }
}

impl TryFrom<CardWire> for Card {
    type Error = WireError;

    fn try_from(wire: CardWire) -> Result<Self, Self::Error> {
        let items = wire
            .payload_items
            .into_iter()
            .map(|item| match item {
                PayloadItemWire::Field {
                    key,
                    value,
                    fill,
                    nested_fills,
                } => {
                    validate_wire_field(&key, &value)?;
                    let mut qv = QuillValue::from_json(value);
                    for path in &nested_fills {
                        let segs: Vec<PathSegment> = path.iter().map(PathSegment::from).collect();
                        qv.set_fill_at(&segs);
                    }
                    Ok(PayloadItem::Field {
                        key,
                        value: qv,
                        fill,
                        nested_comments: Vec::new(),
                    })
                }
                PayloadItemWire::Comment { text, inline } => {
                    Ok(PayloadItem::Comment { text, inline })
                }
            })
            .collect::<Result<Vec<_>, WireError>>()?;

        // Build the user fields/comments, then apply each `$` entry through its
        // setter so the canonical `$quill < $kind < $id < $ext < $seed` ordering
        // holds regardless of input order.
        let mut payload = Payload::from_items(items);
        if let Some(value) = wire.quill {
            let reference = QuillReference::from_str(&value)
                .map_err(|reason| WireError::InvalidQuillReference { value, reason })?;
            payload.set_quill(reference);
        }
        if !wire.kind.is_empty() {
            payload.set_kind(wire.kind);
        }
        if let Some(id) = wire.id {
            payload.set_id(id);
        }
        let too_deep = |key: &str| {
            let key = key.to_string();
            move |max| WireError::InvalidField {
                key,
                reason: format!("nests deeper than the maximum of {} levels", max),
            }
        };
        if let Some(ext) = wire.ext {
            payload.set_ext(crate::value::depth_check_meta_map(ext, too_deep("$ext"))?);
        }
        if let Some(seed) = wire.seed {
            payload.set_seed(crate::value::depth_check_meta_map(seed, too_deep("$seed"))?);
        }
        let body = body_from_wire(&wire.body)?;
        Ok(Card::from_parts(payload, body))
    }
}

/// Read a [`CardWire::body`] into a [`Content`] content. The body is the source
/// of truth in two accepted encodings: a **content object** (an editor / a
/// re-serialized card) is deserialized and validated; a **markdown string** (an
/// LLM / markdown writer) is imported. `null`/absent is the empty content; any
/// other shape is an invalid `$body`.
fn body_from_wire(body: &JsonValue) -> Result<Content, WireError> {
    let invalid = |reason: String| WireError::InvalidField {
        key: "$body".to_string(),
        reason,
    };
    match super::decode_richtext_value(body) {
        Some(result) => result.map_err(|e| invalid(e.into_message())),
        // `null`/absent is the empty content; every other non-decodable shape is
        // an invalid `$body`.
        None => match body {
            JsonValue::Null => Ok(Content::empty()),
            other => Err(invalid(format!(
                "expected a richtext content object or a markdown string, got {}",
                match other {
                    JsonValue::Bool(_) => "a boolean",
                    JsonValue::Number(_) => "a number",
                    JsonValue::Array(_) => "an array",
                    _ => "an unsupported value",
                }
            ))),
        },
    }
}

/// Validate a wire field against the payload invariant (see
/// `edit::validate_field`), mapping a violation to [`WireError::InvalidField`].
fn validate_wire_field(key: &str, value: &JsonValue) -> Result<(), WireError> {
    use super::edit::{validate_field, FieldViolation};
    validate_field(key, value).map_err(|v| WireError::InvalidField {
        key: key.to_string(),
        reason: match v {
            FieldViolation::InvalidName => {
                "field names must match [A-Za-z_][A-Za-z0-9_]*".to_string()
            }
            FieldViolation::TooDeep => format!(
                "nests deeper than the maximum of {} levels",
                crate::document::limits::MAX_YAML_DEPTH
            ),
        },
    })
}

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

    /// Nested `!must_fill` markers inside a field value survive Card → wire →
    /// Card via the `nestedFills` path list (the JSON projection is fill-free).
    #[test]
    fn card_wire_round_trips_nested_fill() {
        let mut addr = QuillValue::from_json(json!({"street": null, "city": "Anytown"}));
        assert!(addr.set_fill_at(&[PathSegment::Key("street".to_string())]));
        let payload = Payload::from_items(vec![PayloadItem::Field {
            key: "addr".to_string(),
            value: addr,
            fill: false,
            nested_comments: Vec::new(),
        }]);
        let card = Card::from_parts(payload, quillmark_content::Content::empty());

        let wire = CardWire::from(&card);
        let as_json = serde_json::to_value(&wire).unwrap();
        assert_eq!(
            as_json["payloadItems"][0]["nestedFills"],
            json!([["street"]]),
            "nested fill path rides the wire as a JS array; JSON value stays fill-free"
        );
        assert_eq!(
            as_json["payloadItems"][0]["value"],
            json!({"street": null, "city": "Anytown"})
        );

        let back = Card::try_from(wire).expect("wire → card");
        assert_eq!(back, card, "nested fill must survive Card → wire → Card");
    }

    /// A richtext field stored as a canonical content object rides the wire
    /// **structurally and losslessly** — the same opaque-JSON `Field` carrier as
    /// any object value, so identity marks (an `underline` with no markdown
    /// projection) survive Card → wire → Card. This is the lossless carrier the
    /// card-yaml markdown projection (emit) deliberately is not.
    #[test]
    fn card_wire_round_trips_content_field_losslessly() {
        use quillmark_content::model::{Mark, MarkKind};

        let mut card = Card::new("note").unwrap();
        let mut content = quillmark_content::import::from_markdown("underlined intro").unwrap();
        content.marks.push(Mark {
            start: 0,
            end: 10,
            kind: MarkKind::Underline,
        });
        content.normalize();
        let json = quillmark_content::serial::to_canonical_value(&content);
        let schema = crate::quill::FieldSchema::new(
            "intro".to_string(),
            crate::quill::FieldType::RichText { inline: false },
            None,
        );
        card.commit_field("intro", crate::QuillValue::from_json(json), &schema)
            .unwrap();

        let wire = CardWire::from(&card);
        // Carried as the content object, verbatim — not a markdown projection.
        let as_json = serde_json::to_value(&wire).unwrap();
        assert!(as_json["payloadItems"][0]["value"].is_object());

        let back = Card::try_from(wire).expect("wire → card");
        assert_eq!(back, card, "content field must survive Card → wire → Card");
        // Underline (content-only, no markdown form) is intact after the round-trip.
        let read = back.field_richtext("intro").unwrap().unwrap();
        assert!(read.marks.iter().any(|m| matches!(m.kind, MarkKind::Underline)));
    }

    /// A field-and-comment card with `$kind` round-trips Card → wire → Card.
    #[test]
    fn card_wire_round_trips_fields_and_comment() {
        let mut payload = Payload::from_items(vec![
            PayloadItem::comment("a note"),
            PayloadItem::field("title", QuillValue::from_json(json!("Hi"))),
            PayloadItem::Field {
                key: "count".to_string(),
                value: QuillValue::from_json(json!(3)),
                fill: true,
                nested_comments: Vec::new(),
            },
        ]);
        payload.set_kind("note");
        let card = Card::from_parts(payload, crate::document::import_body("body text").unwrap());

        let wire = CardWire::from(&card);
        assert_eq!(wire.kind, "note");
        assert_eq!(wire.payload_items.len(), 3);

        let back = Card::try_from(wire).expect("wire → card");
        assert_eq!(back, card, "Card → wire → Card must be identity");
    }

    /// `$quill` (main card) survives the round-trip and parses back.
    #[test]
    fn card_wire_round_trips_quill() {
        let mut payload = Payload::from_index_map(Default::default());
        payload.set_quill("memo@1.2.3".parse().unwrap());
        payload.set_kind("main");
        let card = Card::from_parts(payload, quillmark_content::Content::empty());

        let wire = CardWire::from(&card);
        assert_eq!(wire.quill.as_deref(), Some("memo@1.2.3"));

        let back = Card::try_from(wire).expect("wire → card");
        assert_eq!(back, card);
    }

    /// The wire JSON uses camelCase `payloadItems` and the `type`-tagged items.
    #[test]
    fn card_wire_json_shape() {
        let card = Card::try_from(CardWire {
            kind: "note".to_string(),
            quill: None,
            id: None,
            ext: None,
            seed: None,
            payload_items: vec![PayloadItemWire::Field {
                key: "x".to_string(),
                value: json!(1),
                fill: false,
                nested_fills: Vec::new(),
            }],
            body: JsonValue::Null,
        })
        .unwrap();
        let json = serde_json::to_value(CardWire::from(&card)).unwrap();
        assert_eq!(json["kind"], json!("note"));
        assert_eq!(json["payloadItems"][0]["type"], json!("field"));
        assert_eq!(json["payloadItems"][0]["key"], json!("x"));
        assert!(json.get("quill").is_none(), "absent quill is omitted");
    }

    /// A malformed `quill` string is a typed error, not a panic.
    #[test]
    fn card_wire_rejects_bad_quill() {
        let err = Card::try_from(CardWire {
            kind: String::new(),
            quill: Some("@nope".to_string()),
            id: None,
            ext: None,
            seed: None,
            payload_items: Vec::new(),
            body: JsonValue::Null,
        })
        .unwrap_err();
        assert!(matches!(err, WireError::InvalidQuillReference { .. }));
    }
}