mf2-catalog 1.0.0

The .mf2b binary catalog: a no_std client reader and (features) the writer, the model-rebuilding decoder and the manifest file.
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
//! The model-rebuilding decoder (feature `decode`, build side; conformance
//! layer L3): a message of a [`Catalog`] back to the data model.
//!
//! It walks the same views the runtime walks, so reader and decoder cannot
//! disagree on the grammar, and applies the message's COLD record
//! (`plans/02-catalog-format.md` §2.5) site by site. With COLD stripped it
//! gives the formatting-relevant model — names and keys in NFC, no
//! attributes, no catch-all values — and says so ([`Decoded::cold_dropped`]).
//! Linear in the bytes it reads; strings are borrowed from the catalog.

use alloc::borrow::Cow;
use alloc::vec::Vec;

use mf2_model::{
    Attributes, CatchAllKey, Declaration, Expression, FunctionExpression, FunctionRef,
    InputDeclaration, Key, Literal, LiteralExpression, LocalDeclaration, Markup, Message, MsgId,
    OptionValue, Options, Pattern, PatternMessage, PatternPart, SelectMessage, VariableExpression,
    VariableRef, Variant,
};

use crate::bytes::Cur;
use crate::error::DecodeError;
use crate::format::cold;
use crate::reader::{Catalog, Entry, StrRef};
use crate::view::{
    Body, DeclView, ExprView, FunctionView, KeyView, Malformed, MsgView, Names, Operand,
    OptionsView, PartView, PatternView, VarRef,
};

/// A decoded message and what a stripped catalog could not give back.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Decoded<'a> {
    /// The model.
    pub message: Message<'a>,
    /// The message has a COLD record the catalog does not carry (stripped):
    /// its attributes, catch-all values and non-NFC spellings are lost, and
    /// `message` is the formatting-relevant model.
    pub cold_dropped: bool,
}

/// Rebuilds the data model of message `id`. With COLD present (unstripped)
/// it equals the model that was written (F1).
pub fn decode(catalog: &Catalog, id: MsgId) -> Result<Message<'_>, DecodeError> {
    decode_report(catalog, id).map(|d| d.message)
}

/// [`decode`], reporting whether COLD data was dropped.
pub fn decode_report(catalog: &Catalog, id: MsgId) -> Result<Decoded<'_>, DecodeError> {
    let view = match catalog.get(id) {
        Entry::Absent => return Err(DecodeError::Absent),
        Entry::Simple(r) => {
            let text = str_of(catalog, r)?;
            return Ok(Decoded {
                message: Message::Pattern(PatternMessage {
                    declarations: Vec::new(),
                    pattern: Pattern::from_text(Cow::Borrowed(text)),
                }),
                cold_dropped: false,
            });
        }
        Entry::Pattern(v) | Entry::Select(v) => v,
    };
    Decoder::new(catalog, view)?.message(view)
}

fn str_of(cat: &Catalog, r: StrRef) -> Result<&str, DecodeError> {
    cat.text(r).ok_or(DecodeError::String)
}

impl From<Malformed> for DecodeError {
    fn from(_: Malformed) -> Self {
        DecodeError::Malformed
    }
}

/// One COLD override.
#[derive(Clone, Copy)]
struct Override {
    site: u32,
    kind: u8,
    /// Position of the payload in COLD.
    at: usize,
}

/// The overrides of one COLD record, read one at a time.
struct ColdRecord<'a> {
    c: Cur<'a>,
    left: u32,
    next: Option<Override>,
}

impl<'a> ColdRecord<'a> {
    fn new(cold: &'a [u8], at: usize) -> Result<Self, DecodeError> {
        let mut c = Cur::new(cold, at);
        let left = c.varint().ok_or(DecodeError::Cold)?;
        if left == 0 {
            return Err(DecodeError::Cold);
        }
        let mut r = ColdRecord {
            c,
            left,
            next: None,
        };
        r.advance(None)?;
        Ok(r)
    }

    /// Reads the next override's head; skips nothing (the payload is read
    /// by whoever consumes it, which leaves the cursor after it).
    fn advance(&mut self, prev: Option<u32>) -> Result<(), DecodeError> {
        if self.left == 0 {
            self.next = None;
            return Ok(());
        }
        self.left -= 1;
        let gap = self.c.varint().ok_or(DecodeError::Cold)?;
        let site = match prev {
            None => Some(gap),
            Some(p) => p.checked_add(1).and_then(|x| x.checked_add(gap)),
        }
        .ok_or(DecodeError::Cold)?;
        let kind = self.c.u8().ok_or(DecodeError::Cold)?;
        self.next = Some(Override {
            site,
            kind,
            at: self.c.pos(),
        });
        Ok(())
    }
}

/// The kind of a site (§2.5), which fixes the override kinds it accepts.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Site {
    /// An expression or markup: ATTRIBUTES.
    Node,
    /// A variable, function, option name, markup name or literal key:
    /// SPELLING.
    Name,
    /// A catch-all key: CATCH-ALL.
    CatchAll,
}

struct Decoder<'a> {
    cat: &'a Catalog,
    names: Names<'a>,
    record: Option<ColdRecord<'a>>,
    dropped: bool,
    site: u32,
}

impl<'a> Decoder<'a> {
    fn new(cat: &'a Catalog, view: MsgView<'a>) -> Result<Self, DecodeError> {
        let head = view.head()?;
        let names = view.try_names().ok_or(DecodeError::Names)?;
        let (record, dropped) = match (head.cold, cat.cold) {
            (None, _) => (None, false),
            (Some(_), None) => (None, true),
            (Some(at), Some(span)) => (
                Some(ColdRecord::new(span.of(cat.as_bytes()), at as usize)?),
                false,
            ),
        };
        Ok(Decoder {
            cat,
            names,
            record,
            dropped,
            site: 0,
        })
    }

    fn str(&self, r: StrRef) -> Result<&'a str, DecodeError> {
        str_of(self.cat, r)
    }

    /// Takes the next site; returns its override's payload cursor if it has
    /// one.
    fn site(&mut self, kind: Site) -> Result<Option<(u8, Cur<'a>)>, DecodeError> {
        let site = self.site;
        self.site = site.checked_add(1).ok_or(DecodeError::Malformed)?;
        let Some(rec) = &mut self.record else {
            return Ok(None);
        };
        let Some(o) = rec.next else {
            return Ok(None);
        };
        if o.site != site {
            return if o.site < site {
                Err(DecodeError::Cold)
            } else {
                Ok(None)
            };
        }
        let ok = match kind {
            Site::Node => o.kind == cold::ATTRIBUTES,
            Site::Name => o.kind == cold::SPELLING,
            Site::CatchAll => o.kind == cold::CATCH_ALL,
        };
        if !ok {
            return Err(DecodeError::Cold);
        }
        Ok(Some((o.kind, Cur::new(rec.c.bytes(), o.at))))
    }

    /// After consuming an override's payload with `c`: move to the next one.
    fn consumed(&mut self, c: Cur<'a>) -> Result<(), DecodeError> {
        if let Some(rec) = &mut self.record {
            let prev = rec.next.map(|o| o.site);
            rec.c = c;
            rec.advance(prev)?;
        }
        Ok(())
    }

    /// A name site: the stored (NFC) name, or its spelling override.
    fn name(&mut self, stored: &'a str) -> Result<Cow<'a, str>, DecodeError> {
        match self.site(Site::Name)? {
            None => Ok(Cow::Borrowed(stored)),
            Some((_, mut c)) => {
                let r = c.varint().ok_or(DecodeError::Cold)?;
                self.consumed(c)?;
                Ok(Cow::Borrowed(self.str(StrRef(r))?))
            }
        }
    }

    /// An expression or markup site: its attributes.
    fn attributes(&mut self) -> Result<Attributes<'a>, DecodeError> {
        let Some((_, mut c)) = self.site(Site::Node)? else {
            return Ok(Attributes::new());
        };
        let m = c.len().ok_or(DecodeError::Cold)?;
        let mut attrs = Attributes::with_capacity(m.min(c.remaining()));
        for _ in 0..m {
            let name = self.str(StrRef(c.varint().ok_or(DecodeError::Cold)?))?;
            let value = match c.varint().ok_or(DecodeError::Cold)? {
                0 => None,
                a => Some(Literal {
                    value: Cow::Borrowed(self.str(StrRef(a - 1))?),
                }),
            };
            attrs.push(Cow::Borrowed(name), value);
        }
        self.consumed(c)?;
        Ok(attrs)
    }

    fn var(&mut self, v: VarRef) -> Result<Cow<'a, str>, DecodeError> {
        let stored = self.names.var(v).ok_or(DecodeError::Names)?;
        let stored = self.str(stored)?;
        self.name(stored)
    }

    fn message(mut self, view: MsgView<'a>) -> Result<Decoded<'a>, DecodeError> {
        let mut decls = view.declarations();
        let mut declarations = Vec::with_capacity((decls.len() as usize).min(64));
        for d in &mut decls {
            declarations.push(self.declaration(d?)?);
        }
        let message = match decls.body()? {
            Body::Pattern(p) => Message::Pattern(PatternMessage {
                declarations,
                pattern: self.pattern(p)?,
            }),
            Body::Select(s) => {
                let mut selectors = Vec::new();
                for v in s.selectors() {
                    selectors.push(VariableRef {
                        name: self.var(v?)?,
                    });
                }
                let mut variants = Vec::new();
                for v in s.variants() {
                    let v = v?;
                    let mut keys = Vec::new();
                    for k in v.keys() {
                        keys.push(match k? {
                            KeyView::Literal(r) => {
                                let stored = self.str(r)?;
                                Key::Literal(Literal {
                                    value: self.name(stored)?,
                                })
                            }
                            KeyView::CatchAll => Key::CatchAll(self.catch_all()?),
                        });
                    }
                    variants.push(Variant {
                        keys,
                        value: self.pattern(v.pattern())?,
                    });
                }
                Message::Select(SelectMessage {
                    declarations,
                    selectors,
                    variants,
                })
            }
        };
        if self.record.as_ref().is_some_and(|r| r.next.is_some()) {
            return Err(DecodeError::Cold);
        }
        Ok(Decoded {
            message,
            cold_dropped: self.dropped,
        })
    }

    fn catch_all(&mut self) -> Result<CatchAllKey<'a>, DecodeError> {
        match self.site(Site::CatchAll)? {
            None => Ok(CatchAllKey { value: None }),
            Some((_, mut c)) => {
                let r = c.varint().ok_or(DecodeError::Cold)?;
                self.consumed(c)?;
                Ok(CatchAllKey {
                    value: Some(Cow::Borrowed(self.str(StrRef(r))?)),
                })
            }
        }
    }

    fn declaration(&mut self, d: DeclView<'a>) -> Result<Declaration<'a>, DecodeError> {
        match d {
            DeclView::Input(e) => {
                let attributes = self.attributes()?;
                let Some(Operand::Variable(v)) = e.operand() else {
                    return Err(DecodeError::Malformed);
                };
                let name = self.var(v)?;
                let function = e.function().map(|f| self.function(f)).transpose()?;
                Ok(Declaration::Input(InputDeclaration {
                    name: name.clone(),
                    value: VariableExpression {
                        arg: VariableRef { name },
                        function,
                        attributes,
                    },
                }))
            }
            DeclView::Local { index, expr } => {
                let value = self.expression(expr)?;
                let name = self.names.local(index).ok_or(DecodeError::Names)?;
                Ok(Declaration::Local(LocalDeclaration {
                    name: Cow::Borrowed(self.str(name)?),
                    value,
                }))
            }
        }
    }

    fn expression(&mut self, e: ExprView<'a>) -> Result<Expression<'a>, DecodeError> {
        let attributes = self.attributes()?;
        let operand = match e.operand() {
            None => None,
            Some(Operand::Literal(r)) => Some(Operand2::Literal(self.str(r)?)),
            Some(Operand::Variable(v)) => Some(Operand2::Variable(self.var(v)?)),
        };
        let function = e.function().map(|f| self.function(f)).transpose()?;
        Ok(match (operand, function) {
            (Some(Operand2::Literal(value)), function) => Expression::Literal(LiteralExpression {
                arg: Literal {
                    value: Cow::Borrowed(value),
                },
                function,
                attributes,
            }),
            (Some(Operand2::Variable(name)), function) => {
                Expression::Variable(VariableExpression {
                    arg: VariableRef { name },
                    function,
                    attributes,
                })
            }
            (None, Some(function)) => Expression::Function(FunctionExpression {
                function,
                attributes,
            }),
            (None, None) => return Err(DecodeError::Malformed),
        })
    }

    fn function(&mut self, f: FunctionView<'a>) -> Result<FunctionRef<'a>, DecodeError> {
        let stored = self.cat.function(f.index()).ok_or(DecodeError::Function)?;
        let name = self.name(stored)?;
        Ok(FunctionRef {
            name,
            options: self.options(f.options())?,
        })
    }

    fn options(&mut self, o: OptionsView<'a>) -> Result<Options<'a>, DecodeError> {
        let mut out = Options::with_capacity((o.len() as usize).min(64));
        for opt in o {
            let (name, value) = opt?;
            let stored = self.str(name)?;
            let name = self.name(stored)?;
            let value = match value {
                Operand::Literal(r) => OptionValue::Literal(Literal {
                    value: Cow::Borrowed(self.str(r)?),
                }),
                Operand::Variable(v) => OptionValue::Variable(VariableRef { name: self.var(v)? }),
            };
            out.push(name, value);
        }
        Ok(out)
    }

    /// A pattern. An empty TEXT part, or two in a row, is malformed: the
    /// model's patterns have none (`Pattern::push` would merge them, and the
    /// decoded model would silently differ from the bytes).
    fn pattern(&mut self, p: PatternView<'a>) -> Result<Pattern<'a>, DecodeError> {
        let mut out = Pattern::with_capacity((p.len() as usize).min(64));
        let mut after_text = false;
        for part in p.parts() {
            let part = part?;
            let text = matches!(part, PartView::Text(_));
            if text && after_text {
                return Err(DecodeError::Malformed);
            }
            after_text = text;
            out.push(match part {
                PartView::Text(r) => {
                    let t = self.str(r)?;
                    if t.is_empty() {
                        return Err(DecodeError::Malformed);
                    }
                    PatternPart::Text(Cow::Borrowed(t))
                }
                PartView::Expression(e) => PatternPart::Expression(self.expression(e)?),
                PartView::Markup(m) => {
                    let attributes = self.attributes()?;
                    let stored = self.str(m.name())?;
                    let name = self.name(stored)?;
                    PatternPart::Markup(Markup {
                        kind: m.kind(),
                        name,
                        options: self.options(m.options())?,
                        attributes,
                    })
                }
            });
        }
        Ok(out)
    }
}

/// An operand while its expression is being rebuilt.
enum Operand2<'a> {
    Literal(&'a str),
    Variable(Cow<'a, str>),
}