quillmark-core 0.101.0

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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Schema-bound typed reader: the read twin of
//! [`TypedWriter`](crate::TypedWriter).
//!
//! The read surface's verbs split by read-vs-write, but the deeper fault line is
//! **interpret-vs-transport**. The verbatim [`payload().get`](crate::Card::payload)
//! (the WASM binding's `Document.getStored`) is *transport*: it returns the stored
//! value verbatim, schema-free and round-trippable, the disambiguation / debug
//! read. Projecting a field to
//! markdown is *interpretation*: a schema-shaped question ("this field's
//! richtext, as markdown") that a schema-free `Document` cannot answer without
//! guessing which fields are even richtext. So the projection has one door, and
//! it binds the schema.
//!
//! [`Quill::reader`](crate::Quill::reader) binds the schema (where the authority
//! already lives, the writer's twin) so a single verb interprets by the field's
//! declared type:
//!
//! ```ignore
//! let v = quill.reader(&doc);
//! v.get("subject")?;            // richtext → Some(Markdown(..))
//! v.get("qty")?;                // integer  → Some(Value(3))
//! v.get("absent")?;             // absent   → None
//! v.get("nope");                // unknown name → Err(UnknownField)
//! v.card(2)?.get("body")?;      // card field, kind resolves its schema
//! ```
//!
//! **absence returns; mismatch raises; an unknown name is a typo.** A `richtext`
//! field projects to markdown ([`ReadValue::Markdown`]) and a `plaintext` field
//! to its literal text ([`ReadValue::Plaintext`]); every other declared type
//! returns its canonical value verbatim ([`ReadValue::Value`]): the same
//! transport `Document` reads, now reached with schema authority. A present value
//! that does not decode under a content field raises
//! [`EditError::FieldRichtextDecode`]. A name the schema does not declare raises
//! [`EditError::UnknownField`], exactly as [`TypedWriter::set`](crate::TypedWriter::set)
//! rejects it on the write side.
//!
//! [`get_content`](TypedReader::get_content) is the same read at the other end of
//! the codec: the corpus rather than the projection. A document that came
//! through the bound door ([`Quill::parse`](crate::Quill::parse) /
//! [`Quill::conform`](crate::Quill::conform)) rests at one form per codec, but
//! one the transport door left rests as authored, so the verbatim payload read
//! still answers "corpus or string?" with "depends where this document came
//! from" and this one does not. Decoding needs the
//! schema, not the payload: a `richtext` string is markdown and a `plaintext`
//! string is literal text, so the same bytes decode two ways and only the
//! declared type says which. That is why the corpus read binds the quill and
//! `Document` carries none.
//!
//! The body read stays quill-free: a body's type is a format fact, not a schema
//! fact, so [`get_body`](TypedReader::get_body) mirrors
//! [`Card::body_markdown`](crate::Card::body_markdown) rather than consulting the
//! schema.
//!
//! Like [`TypedWriter`](crate::TypedWriter), a bound reader holds `&Document`
//! and `&QuillConfig`, so
//! it cannot cross a binding boundary that carries no lifetimes (wasm-bindgen /
//! pyo3); those surfaces construct one per call from the quill handle.

use indexmap::IndexMap;
use quillmark_content::Content;

use crate::document::{Card, Document, EditError, RichtextDecodeError};
use crate::quill::{CardSchema, FieldSchema, FieldType, QuillConfig};
use crate::value::QuillValue;

/// The interpreted value at a field address: the output of [`TypedReader::get`].
/// A content field decodes to its codec's projection (`richtext` to markdown,
/// `plaintext` to literal text); every other declared type carries its canonical
/// value verbatim (the transport read, reached through the schema). Absence is
/// the `None` of the enclosing `Option`, not a variant here.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum ReadValue {
    /// A `richtext` field projected to markdown (`export ∘ decode`): the lossy,
    /// on-demand view (content-only marks do not survive markdown).
    Markdown(String),
    /// A `plaintext` field projected through its literal codec (`to_plaintext ∘
    /// decode`): verbatim text, marks never interpreted (`*hi*` is four
    /// characters, not emphasis).
    Plaintext(String),
    /// A non-content field's canonical value, verbatim: the schema-free
    /// transport read a `Document` returns, delivered here with schema authority.
    Value(QuillValue),
}

/// A [`Document`] bound to its [`QuillConfig`] for typed reads. Construct with
/// [`Quill::reader`](crate::Quill::reader). Reads target the main card; use
/// [`card`](Self::card) for a composable card. The read twin of
/// [`TypedWriter`](crate::TypedWriter).
pub struct TypedReader<'a> {
    config: &'a QuillConfig,
    doc: &'a Document,
}

impl<'a> TypedReader<'a> {
    /// Bind `doc` to `config`. Prefer [`Quill::reader`](crate::Quill::reader).
    pub fn new(config: &'a QuillConfig, doc: &'a Document) -> Self {
        Self { config, doc }
    }

    /// Read a main-card field, interpreted by its declared type: `richtext` to
    /// markdown ([`ReadValue::Markdown`]), `plaintext` to literal text
    /// ([`ReadValue::Plaintext`]), every other type verbatim
    /// ([`ReadValue::Value`]). `Ok(None)` when the field is absent;
    /// [`EditError::UnknownField`] for a name the schema does not declare (a typo,
    /// as on the write side); [`EditError::FieldRichtextDecode`] when a content
    /// field holds a value that does not decode (a scalar an opaque
    /// [`store_field`](crate::Card::store_field) wrote).
    pub fn get(&self, name: &str) -> Result<Option<ReadValue>, EditError> {
        read_field(self.doc.main(), Some(&self.config.main.fields), name)
    }

    /// Read a main-card content field as its [`Content`] corpus, decoded through
    /// the codec its declared type names: the corpus twin of [`get`](Self::get),
    /// which returns a projection. Total over the storage form, so a field the
    /// writer committed as a canonical corpus and one a markdown parse left as an
    /// authored string both read back as a corpus, and which lane built the
    /// document stops being the caller's business.
    ///
    /// `Ok(None)` when the field is absent;
    /// [`EditError::UnknownField`] for a name the schema does not declare;
    /// [`EditError::FieldNotContent`] for a declared type that is not a content
    /// leaf (an `integer` has no corpus even when it holds a string, and an
    /// `array<richtext>` carries content without having one corpus);
    /// [`EditError::FieldRichtextDecode`] when the stored value decodes under
    /// neither encoding.
    ///
    /// The codec is the schema's to name, which is why this read is here and not
    /// on `Document`: a `richtext` string is markdown, a `plaintext` string is
    /// literal text, and a quill-free read would have to guess.
    pub fn get_content(&self, name: &str) -> Result<Option<Content>, EditError> {
        read_content(self.doc.main(), Some(&self.config.main.fields), name)
    }

    /// The main body's markdown projection, the quill-free body read
    /// ([`Card::body_markdown`](crate::Card::body_markdown)). A body's type is a
    /// format fact, not a schema fact, so this consults no schema and never
    /// raises; the body is never absent.
    pub fn get_body(&self) -> String {
        self.doc.main().body_markdown()
    }

    /// A schema-bound reader for the composable card at `index`. The card's
    /// `$kind` resolves its [`CardSchema`]; an unknown kind carries no schema, so
    /// every field name on it is undeclared and reads with
    /// [`EditError::UnknownField`] (read such a card verbatim through
    /// [`Card::payload`]). [`EditError::IndexOutOfRange`] when `index` is out of
    /// range: a boundary error, not an absent field, as the card write verbs
    /// treat it.
    pub fn card(&self, index: usize) -> Result<CardReader<'_>, EditError> {
        let len = self.doc.cards().len();
        let card = self
            .doc
            .card(index)
            .ok_or(EditError::IndexOutOfRange { index, len })?;
        let schema = card.kind().and_then(|k| self.config.card_kind(k));
        Ok(CardReader { schema, card })
    }
}

/// A single composable card bound to its [`CardSchema`], from
/// [`TypedReader::card`]. Same `get` / `get_body` verbs as [`TypedReader`],
/// reading the card at its bound index.
pub struct CardReader<'a> {
    schema: Option<&'a CardSchema>,
    card: &'a Card,
}

impl CardReader<'_> {
    /// The card's `$kind`, if any.
    pub fn kind(&self) -> Option<&str> {
        self.card.kind()
    }

    /// Read a field on this card, interpreted by its declared type: the card
    /// twin of [`TypedReader::get`]. Resolves the field against the card's
    /// [`CardSchema`]; a name the schema does not declare (or any name when the
    /// card kind is unknown) reads with [`EditError::UnknownField`].
    pub fn get(&self, name: &str) -> Result<Option<ReadValue>, EditError> {
        read_field(self.card, self.schema.map(|s| &s.fields), name)
    }

    /// Read a content field on this card as its [`Content`] corpus: the card twin
    /// of [`TypedReader::get_content`], carrying the same outcomes.
    pub fn get_content(&self, name: &str) -> Result<Option<Content>, EditError> {
        read_content(self.card, self.schema.map(|s| &s.fields), name)
    }

    /// This card's body markdown: the card twin of [`TypedReader::get_body`],
    /// quill-free and never raising.
    pub fn get_body(&self) -> String {
        self.card.body_markdown()
    }
}

/// The shared read dispatch behind [`TypedReader::get`] and [`CardReader::get`]:
/// resolve `name` against `fields_schema` (an unknown name, or every name when
/// the whole schema is `None` (an unknown card kind) is
/// [`EditError::UnknownField`]), then interpret by the field's declared type. A
/// content field projects through its codec (`richtext` via
/// [`Card::field_markdown`], `plaintext` via [`Card::field_plaintext`]) each
/// carrying the projection's absent (`None`) / mismatch
/// ([`EditError::FieldRichtextDecode`]) outcomes; every other type returns its
/// canonical value verbatim, `None` when absent.
fn read_field(
    card: &Card,
    fields_schema: Option<&IndexMap<String, FieldSchema>>,
    name: &str,
) -> Result<Option<ReadValue>, EditError> {
    let schema = fields_schema
        .and_then(|m| m.get(name))
        .ok_or_else(|| EditError::UnknownField(name.to_string()))?;
    match schema.r#type {
        FieldType::RichText { .. } => project(card.field_markdown(name), name, ReadValue::Markdown),
        FieldType::PlainText { .. } => {
            project(card.field_plaintext(name), name, ReadValue::Plaintext)
        }
        _ => Ok(card
            .payload()
            .get(name)
            .map(|v| ReadValue::Value(v.clone()))),
    }
}

/// The shared corpus dispatch behind [`TypedReader::get_content`] and
/// [`CardReader::get_content`]: resolve `name` against `fields_schema`, then
/// decode through the codec the declared type names ([`Card::field_richtext`] for
/// `richtext`, [`Card::field_plaintext_content`] for `plaintext`). Every other
/// type is [`EditError::FieldNotContent`], answered from the schema before the
/// payload is read: whether a field has a corpus is a declared-type fact, so a
/// `string` field holding markdown-looking text is still not content. The two
/// content leaves are the whole domain, so an `array<richtext>` lands here as
/// well: it carries content and still has no single corpus.
fn read_content(
    card: &Card,
    fields_schema: Option<&IndexMap<String, FieldSchema>>,
    name: &str,
) -> Result<Option<Content>, EditError> {
    let schema = fields_schema
        .and_then(|m| m.get(name))
        .ok_or_else(|| EditError::UnknownField(name.to_string()))?;
    let decoded = match schema.r#type {
        FieldType::RichText { .. } => card.field_richtext(name),
        FieldType::PlainText { .. } => card.field_plaintext_content(name),
        ref other => {
            return Err(EditError::FieldNotContent {
                field: name.to_string(),
                declared: other.as_str().to_string(),
            })
        }
    };
    match decoded {
        None => Ok(None),
        Some(Ok(content)) => Ok(Some(content)),
        Some(Err(e)) => Err(EditError::FieldRichtextDecode {
            field: name.to_string(),
            message: e.into_message(),
        }),
    }
}

/// Lift a codec projection ([`Card::field_markdown`] / [`Card::field_plaintext`])
/// into a [`ReadValue`]: `None` absent, `Some(Ok)` wrapped by `wrap`, `Some(Err)`
/// the [`EditError::FieldRichtextDecode`] mismatch naming `name`.
fn project(
    projection: Option<Result<String, RichtextDecodeError>>,
    name: &str,
    wrap: fn(String) -> ReadValue,
) -> Result<Option<ReadValue>, EditError> {
    match projection {
        None => Ok(None),
        Some(Ok(text)) => Ok(Some(wrap(text))),
        Some(Err(e)) => Err(EditError::FieldRichtextDecode {
            field: name.to_string(),
            message: e.into_message(),
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::document::Document;
    use crate::version::QuillReference;
    use std::str::FromStr;

    const QUILL_YAML: &str = "\
quill:
  name: memo
  backend: typst
  version: 1.0.0
  description: Reader test quill
main:
  fields:
    subject:
      type: richtext
      inline: true
    note:
      type: plaintext
    qty:
      type: integer
card_kinds:
  note:
    fields:
      body:
        type: richtext
";

    fn config() -> QuillConfig {
        QuillConfig::from_yaml(QUILL_YAML).expect("valid quill")
    }

    fn blank_doc() -> Document {
        Document::new(QuillReference::from_str("memo@1.0.0").unwrap())
    }

    // Build a document through the writer, then read it back through the view.
    fn seeded_doc(config: &QuillConfig) -> Document {
        let mut doc = blank_doc();
        {
            let mut w = crate::TypedWriter::new(config, &mut doc);
            w.set("subject", "Hello **world**").unwrap();
            w.set("qty", "3").unwrap();
            w.add_card("note", [("body", "a *card*")], None, None).unwrap();
        }
        doc
    }

    #[test]
    fn richtext_field_projects_to_markdown() {
        let config = config();
        let doc = seeded_doc(&config);
        let view = TypedReader::new(&config, &doc);
        assert_eq!(
            view.get("subject").unwrap(),
            Some(ReadValue::Markdown("Hello **world**".to_string()))
        );
    }

    #[test]
    fn plaintext_field_projects_to_literal_text() {
        let config = config();
        let mut doc = blank_doc();
        {
            let mut w = crate::TypedWriter::new(&config, &mut doc);
            // Marks are literal under plaintext: `*hi*` is verbatim, not emphasis.
            w.set("note", "a *literal* line").unwrap();
        }
        let view = TypedReader::new(&config, &doc);
        assert_eq!(
            view.get("note").unwrap(),
            Some(ReadValue::Plaintext("a *literal* line".to_string()))
        );
    }

    #[test]
    fn scalar_field_returns_canonical_value() {
        let config = config();
        let doc = seeded_doc(&config);
        let view = TypedReader::new(&config, &doc);
        assert_eq!(
            view.get("qty").unwrap(),
            Some(ReadValue::Value(QuillValue::from_json(serde_json::json!(3))))
        );
    }

    #[test]
    fn absent_field_returns_none() {
        let config = config();
        let doc = blank_doc();
        let view = TypedReader::new(&config, &doc);
        assert_eq!(view.get("subject").unwrap(), None);
        assert_eq!(view.get("qty").unwrap(), None);
    }

    #[test]
    fn unknown_field_name_raises() {
        let config = config();
        let doc = blank_doc();
        let view = TypedReader::new(&config, &doc);
        assert!(matches!(
            view.get("nope"),
            Err(EditError::UnknownField(name)) if name == "nope"
        ));
    }

    #[test]
    fn richtext_field_holding_scalar_raises_mismatch() {
        let config = config();
        let mut doc = blank_doc();
        // An opaque write puts a bare number under the `subject` richtext field.
        doc.main_mut()
            .store_field("subject", QuillValue::from_json(serde_json::json!(3)))
            .unwrap();
        let view = TypedReader::new(&config, &doc);
        assert!(matches!(
            view.get("subject"),
            Err(EditError::FieldRichtextDecode { field, .. }) if field == "subject"
        ));
    }

    // A `plaintext` field parsed from markdown rests as an authored string, and
    // its codec is literal: `*literal*` is nine characters, not emphasis. The
    // object lane is covered above; this is the string lane, which decoded
    // through the markdown codec until it read its own declared type.
    #[test]
    fn parse_lane_plaintext_reads_through_the_literal_codec() {
        let config = config();
        let mut doc = blank_doc();
        doc.main_mut()
            .store_field(
                "note",
                QuillValue::from_json(serde_json::json!("a *literal* line")),
            )
            .unwrap();
        let view = TypedReader::new(&config, &doc);
        assert_eq!(
            view.get("note").unwrap(),
            Some(ReadValue::Plaintext("a *literal* line".to_string()))
        );
    }

    // The corpus read is total over the storage form: the seeded (committed)
    // lane and the parsed (authored-string) lane return the same corpus.
    #[test]
    fn content_read_spans_both_storage_forms() {
        let config = config();
        let committed = seeded_doc(&config);
        let mut authored = blank_doc();
        authored
            .main_mut()
            .store_field(
                "subject",
                QuillValue::from_json(serde_json::json!("Hello **world**")),
            )
            .unwrap();

        let from_corpus = TypedReader::new(&config, &committed)
            .get_content("subject")
            .unwrap()
            .unwrap();
        let from_string = TypedReader::new(&config, &authored)
            .get_content("subject")
            .unwrap()
            .unwrap();
        assert_eq!(from_corpus.text, "Hello world");
        assert_eq!(from_corpus.text, from_string.text);
        assert_eq!(from_corpus.marks, from_string.marks);
    }

    // The codec follows the declared type, so the same stored bytes decode two
    // ways: markdown under `richtext`, literal under `plaintext`.
    #[test]
    fn content_read_decodes_by_declared_type() {
        let config = config();
        let mut doc = blank_doc();
        let text = serde_json::json!("a *literal* line");
        {
            let card = doc.main_mut();
            card.store_field("subject", QuillValue::from_json(text.clone())).unwrap();
            card.store_field("note", QuillValue::from_json(text)).unwrap();
        }
        let view = TypedReader::new(&config, &doc);
        // `richtext`: the asterisks are emphasis, so they leave the text.
        assert_eq!(view.get_content("subject").unwrap().unwrap().text, "a literal line");
        // `plaintext`: the asterisks are characters.
        assert_eq!(view.get_content("note").unwrap().unwrap().text, "a *literal* line");
    }

    #[test]
    fn content_read_absent_unknown_and_non_content() {
        let config = config();
        let doc = seeded_doc(&config);
        let view = TypedReader::new(&config, &doc);
        assert_eq!(view.get_content("note").unwrap(), None);
        assert!(matches!(
            view.get_content("nope"),
            Err(EditError::UnknownField(n)) if n == "nope"
        ));
        // A non-leaf declared type answers from the schema, not the payload:
        // `qty` holds 3 and is still not a content field.
        assert!(matches!(
            view.get_content("qty"),
            Err(EditError::FieldNotContent { field, declared })
                if field == "qty" && declared == "integer"
        ));
    }

    #[test]
    fn content_read_undecodable_value_raises() {
        let config = config();
        let mut doc = blank_doc();
        doc.main_mut()
            .store_field("subject", QuillValue::from_json(serde_json::json!(3)))
            .unwrap();
        let view = TypedReader::new(&config, &doc);
        assert!(matches!(
            view.get_content("subject"),
            Err(EditError::FieldRichtextDecode { field, .. }) if field == "subject"
        ));
    }

    #[test]
    fn card_content_reads_through_kind_schema() {
        let config = config();
        let doc = seeded_doc(&config);
        let view = TypedReader::new(&config, &doc);
        let card = view.card(0).unwrap();
        assert_eq!(card.get_content("body").unwrap().unwrap().text, "a card");
        assert!(matches!(
            card.get_content("nope"),
            Err(EditError::UnknownField(_))
        ));
    }

    #[test]
    fn card_field_reads_through_kind_schema() {
        let config = config();
        let doc = seeded_doc(&config);
        let view = TypedReader::new(&config, &doc);
        let card = view.card(0).unwrap();
        assert_eq!(card.kind(), Some("note"));
        assert_eq!(
            card.get("body").unwrap(),
            Some(ReadValue::Markdown("a *card*".to_string()))
        );
        assert!(matches!(card.get("nope"), Err(EditError::UnknownField(_))));
    }

    #[test]
    fn card_out_of_range_raises() {
        let config = config();
        let doc = blank_doc();
        let view = TypedReader::new(&config, &doc);
        assert!(matches!(
            view.card(9),
            Err(EditError::IndexOutOfRange { index: 9, len: 0 })
        ));
    }

    #[test]
    fn body_read_is_quill_free() {
        let config = config();
        let mut doc = blank_doc();
        doc.main_mut().revise_body("A **body**.").unwrap();
        let view = TypedReader::new(&config, &doc);
        assert_eq!(view.get_body(), "A **body**.");
    }
}