lima 0.9.0

Lima frontmatter parser — LIMA Is Metadata Annotation
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
//! Lima Value Model — mirrors `js/src/value.ts`. Unlike JS's single `number`
//! type, Rust genuinely distinguishes `i64` from `f64`, so the int/float
//! tagging carried here is not extra ceremony — it's the representation a
//! Rust port needs from the moment a scalar is recognised.

/// UTC instant, stored as seconds since the Unix epoch. Core's valid calendar
/// range is years 0001-9999 (Appendix A) — comfortably inside `i64`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Instant {
    pub epoch_seconds: i64,
}

impl Instant {
    /// Renders as `YYYY-MM-DDTHH:MM:SSZ` — the exact form the conformance
    /// corpus fixtures use for `{ "$type": "instant", "value": "..." }`.
    pub fn to_iso_string(self) -> String {
        let days = self.epoch_seconds.div_euclid(86_400);
        let secs_of_day = self.epoch_seconds.rem_euclid(86_400);
        let (year, month, day) = civil_from_days(days);
        let hour = secs_of_day / 3600;
        let minute = (secs_of_day % 3600) / 60;
        let second = secs_of_day % 60;
        format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z")
    }
}

/// Deliberately no `serde::Serialize`/`Deserialize` (yet). Two separable
/// pieces of future work, neither started:
///
/// 1. Making `LimaValue` itself serializable (e.g. to JSON) — mechanically
///    straightforward, similar to `serde_json::Value`'s own impl.
/// 2. Deserializing straight into a caller-defined `#[derive(Deserialize)]`
///    struct (the ergonomic win `serde_yaml` offered) — needs a real
///    `serde::Deserializer` impl over a `LimaValue` wrapper; a known,
///    well-trodden pattern, but genuine work, not a one-liner.
///
/// Both are purely additive — safe to add later behind an opt-in `serde`
/// Cargo feature without breaking anything published now. Deferred for
/// 0.1.1 pending actual demand: the JS package has no equivalent (JS is
/// dynamically typed), so there is no parity pressure driving this.
#[derive(Debug, Clone, PartialEq)]
pub enum LimaValue {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    String(String),
    Instant(Instant),
    Array(Vec<LimaValue>),
    /// Insertion-ordered — Core has no concept of key sorting.
    Mapping(Vec<(String, LimaValue)>),
}

impl LimaValue {
    /// Core §9: `depth(scalar) = 0`, `depth(array | mapping) = 1 +
    /// max(depth(child))` (0 for an empty collection). Mirrors
    /// `depthOfNative`.
    pub fn depth(&self) -> u32 {
        match self {
            LimaValue::Array(items) => 1 + items.iter().map(LimaValue::depth).max().unwrap_or(0),
            LimaValue::Mapping(entries) => {
                1 + entries.iter().map(|(_, v)| v.depth()).max().unwrap_or(0)
            }
            _ => 0,
        }
    }
}

/// Inserts into an insertion-ordered `Vec<(String, V)>` mapping, updating in
/// place if the key already exists — matches JS `Map.set`'s "keep original
/// position, replace value" semantics rather than moving the key to the end.
/// Generic over `V` so both [`LimaValue`] and [`PositionedValue`] mappings
/// (identically shaped, different leaf type) share one implementation.
pub fn set_mapping<V>(entries: &mut Vec<(String, V)>, key: String, value: V) {
    if let Some(slot) = entries.iter_mut().find(|(k, _)| *k == key) {
        slot.1 = value;
    } else {
        entries.push((key, value));
    }
}

/// References §5/R-112: stamped on the root of a value copied in by a
/// successful pure-reference resolution, with the source token and line
/// that caused the insertion — never set by Core itself. Powers §5's
/// global-error attribution: when a final-result limit (nesting depth,
/// total node count) is violated, the lowest-line `inserted_at` among the
/// participating nodes identifies which reference token to blame.
#[derive(Debug, Clone, PartialEq)]
pub struct InsertedAt {
    pub line: u32,
    pub offset: usize,
    pub token: String,
}

/// Where a scalar begins in the source: physical 1-based `line` and 0-based
/// codepoint column `col` of its first character. A References 2.0 token's
/// position is this anchor plus its codepoint offset within the value
/// (§2.4). `raw` is the value text a token's position is read from, before
/// comment stripping / `\#` collapse (and for a block scalar the original
/// body lines joined with `\n`, so a token's line is not reconstructed from
/// the `^^`-merged string); `None` means it equals the decoded value.
/// `tab_adjust[i]` is the columns Core §3 added to physical line `line + i`
/// by leading-tab expansion, subtracted so the reported column is the
/// original-source column. Mirrors `js/src/reference-tokens2.ts`.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ReferenceSource {
    pub raw: Option<String>,
    pub line: u32,
    pub col: usize,
    pub tab_adjust: Option<Vec<usize>>,
}

/// The annotated tree Core produces internally and References resolves —
/// every node carries the source line it was parsed with (needed for
/// diagnostics: `UNRESOLVED_REFERENCE`, `INVALID_INTERPOLATION`, and
/// depth/node-count limit attribution all report a *specific* node's line,
/// not just "somewhere in the document"), strings additionally carry
/// whether they came from quoted syntax (quoted strings are never
/// reference sites — Core §2.3), and every node can carry `inserted_at`
/// once References has copied it in from elsewhere. Mirrors
/// `js/src/scalars.ts`'s `PositionedValue`. `parse_core`'s public
/// `LimaValue` is the position-stripped projection of this same tree
/// (`to_plain_value`) — not a separately parsed representation, so the two
/// can't drift apart the way two independently hand-written parsers could.
#[derive(Debug, Clone, PartialEq)]
pub enum PositionedValue {
    Null {
        line: u32,
        inserted_at: Option<InsertedAt>,
        prior_insertions: Vec<InsertedAt>,
    },
    Bool {
        value: bool,
        line: u32,
        inserted_at: Option<InsertedAt>,
        prior_insertions: Vec<InsertedAt>,
    },
    Int {
        value: i64,
        line: u32,
        inserted_at: Option<InsertedAt>,
        prior_insertions: Vec<InsertedAt>,
    },
    Float {
        value: f64,
        line: u32,
        inserted_at: Option<InsertedAt>,
        prior_insertions: Vec<InsertedAt>,
    },
    String {
        value: String,
        line: u32,
        quoted: bool,
        ref_source: Option<ReferenceSource>,
        inserted_at: Option<InsertedAt>,
        prior_insertions: Vec<InsertedAt>,
    },
    Instant {
        value: Instant,
        line: u32,
        inserted_at: Option<InsertedAt>,
        prior_insertions: Vec<InsertedAt>,
    },
    Array {
        items: Vec<PositionedValue>,
        line: u32,
        inserted_at: Option<InsertedAt>,
        prior_insertions: Vec<InsertedAt>,
    },
    /// Insertion-ordered — Core has no concept of key sorting.
    Mapping {
        entries: Vec<(String, PositionedValue)>,
        line: u32,
        inserted_at: Option<InsertedAt>,
        prior_insertions: Vec<InsertedAt>,
    },
}

impl PositionedValue {
    pub fn line(&self) -> u32 {
        match self {
            PositionedValue::Null { line, .. }
            | PositionedValue::Bool { line, .. }
            | PositionedValue::Int { line, .. }
            | PositionedValue::Float { line, .. }
            | PositionedValue::String { line, .. }
            | PositionedValue::Instant { line, .. }
            | PositionedValue::Array { line, .. }
            | PositionedValue::Mapping { line, .. } => *line,
        }
    }

    pub fn inserted_at(&self) -> Option<&InsertedAt> {
        match self {
            PositionedValue::Null { inserted_at, .. }
            | PositionedValue::Bool { inserted_at, .. }
            | PositionedValue::Int { inserted_at, .. }
            | PositionedValue::Float { inserted_at, .. }
            | PositionedValue::String { inserted_at, .. }
            | PositionedValue::Instant { inserted_at, .. }
            | PositionedValue::Array { inserted_at, .. }
            | PositionedValue::Mapping { inserted_at, .. } => inserted_at.as_ref(),
        }
    }

    pub fn prior_insertions(&self) -> &[InsertedAt] {
        match self {
            PositionedValue::Null {
                prior_insertions, ..
            }
            | PositionedValue::Bool {
                prior_insertions, ..
            }
            | PositionedValue::Int {
                prior_insertions, ..
            }
            | PositionedValue::Float {
                prior_insertions, ..
            }
            | PositionedValue::String {
                prior_insertions, ..
            }
            | PositionedValue::Instant {
                prior_insertions, ..
            }
            | PositionedValue::Array {
                prior_insertions, ..
            }
            | PositionedValue::Mapping {
                prior_insertions, ..
            } => prior_insertions,
        }
    }

    /// Strips position/quoted-origin annotations, recursively — the public
    /// `parse_core()` projection. Mirrors `toPlainValue`.
    pub fn to_plain_value(&self) -> LimaValue {
        match self {
            PositionedValue::Null { .. } => LimaValue::Null,
            PositionedValue::Bool { value, .. } => LimaValue::Bool(*value),
            PositionedValue::Int { value, .. } => LimaValue::Int(*value),
            PositionedValue::Float { value, .. } => LimaValue::Float(*value),
            PositionedValue::String { value, .. } => LimaValue::String(value.clone()),
            PositionedValue::Instant { value, .. } => LimaValue::Instant(*value),
            PositionedValue::Array { items, .. } => {
                LimaValue::Array(items.iter().map(PositionedValue::to_plain_value).collect())
            }
            PositionedValue::Mapping { entries, .. } => LimaValue::Mapping(
                entries
                    .iter()
                    .map(|(k, v)| (k.clone(), v.to_plain_value()))
                    .collect(),
            ),
        }
    }

    /// [`LimaValue::depth`]'s counterpart. Mirrors `depthOfPositioned`.
    pub fn depth(&self) -> u32 {
        match self {
            PositionedValue::Array { items, .. } => {
                1 + items.iter().map(PositionedValue::depth).max().unwrap_or(0)
            }
            PositionedValue::Mapping { entries, .. } => {
                1 + entries.iter().map(|(_, v)| v.depth()).max().unwrap_or(0)
            }
            _ => 0,
        }
    }
}

/// One parsing grammar (`scalars`/`flow`/`block`/`core`), two output
/// representations — mirrors `js/src/builder.ts`'s `ValueBuilder<V, M>`.
/// `Plain` fixes this to the public [`LimaValue`] (`parse_core`'s shape,
/// no line-tracking overhead when References isn't used); `Positioned`
/// fixes it to [`PositionedValue`] (what `references.rs` resolves against).
/// Every method is a plain function of its arguments (no builder state),
/// matching the TS object literals — implemented as associated functions
/// on a zero-sized marker type rather than trait methods needing `&self`.
pub trait Builder {
    /// The positioned builder tracks References 2.0 token source positions;
    /// the plain one does not, so callers skip building the anchor for it.
    const POSITIONED: bool = false;
    type Value: Clone;
    type Mapping;

    fn v_null(line: u32) -> Self::Value;
    fn v_bool(value: bool, line: u32) -> Self::Value;
    fn v_int(value: i64, line: u32) -> Self::Value;
    fn v_float(value: f64, line: u32) -> Self::Value;
    fn v_string(value: String, line: u32, quoted: bool) -> Self::Value;
    /// An unquoted string carrying its physical source, for References 2.0
    /// token attribution (§2.4). `PlainBuilder` discards the source.
    fn v_string_src(value: String, line: u32, _source: ReferenceSource) -> Self::Value {
        Self::v_string(value, line, false)
    }
    fn v_instant(value: Instant, line: u32) -> Self::Value;
    fn v_array(items: Vec<Self::Value>, line: u32) -> Self::Value;
    fn v_mapping(entries: Self::Mapping, line: u32) -> Self::Value;

    fn m_create() -> Self::Mapping;
    fn m_create_with(key: String, value: Self::Value) -> Self::Mapping;
    fn m_has_key(entries: &Self::Mapping, key: &str) -> bool;
    fn m_set(entries: &mut Self::Mapping, key: String, value: Self::Value);

    /// Core §9: the root mapping's own max depth over its values (the root
    /// itself does not count as a depth level). Mirrors `mappingMaxDepth`.
    fn m_max_depth(entries: &Self::Mapping) -> u32;
}

/// Produces the plain [`LimaValue`] tree — `parse_core`'s public shape.
pub struct PlainBuilder;

impl Builder for PlainBuilder {
    type Value = LimaValue;
    type Mapping = Vec<(String, LimaValue)>;

    fn v_null(_line: u32) -> LimaValue {
        LimaValue::Null
    }
    fn v_bool(value: bool, _line: u32) -> LimaValue {
        LimaValue::Bool(value)
    }
    fn v_int(value: i64, _line: u32) -> LimaValue {
        LimaValue::Int(value)
    }
    fn v_float(value: f64, _line: u32) -> LimaValue {
        LimaValue::Float(value)
    }
    fn v_string(value: String, _line: u32, _quoted: bool) -> LimaValue {
        LimaValue::String(value)
    }
    fn v_instant(value: Instant, _line: u32) -> LimaValue {
        LimaValue::Instant(value)
    }
    fn v_array(items: Vec<LimaValue>, _line: u32) -> LimaValue {
        LimaValue::Array(items)
    }
    fn v_mapping(entries: Self::Mapping, _line: u32) -> LimaValue {
        LimaValue::Mapping(entries)
    }

    fn m_create() -> Self::Mapping {
        Vec::new()
    }
    fn m_create_with(key: String, value: LimaValue) -> Self::Mapping {
        vec![(key, value)]
    }
    fn m_has_key(entries: &Self::Mapping, key: &str) -> bool {
        entries.iter().any(|(k, _)| k == key)
    }
    fn m_set(entries: &mut Self::Mapping, key: String, value: LimaValue) {
        set_mapping(entries, key, value)
    }
    fn m_max_depth(entries: &Self::Mapping) -> u32 {
        entries.iter().map(|(_, v)| v.depth()).max().unwrap_or(0)
    }
}

/// Produces the annotated [`PositionedValue`] tree — what `references.rs`
/// resolves against. `inserted_at` always starts `None`; only References'
/// own reference-copying (never Core) ever sets it.
pub struct PositionedBuilder;

impl Builder for PositionedBuilder {
    const POSITIONED: bool = true;
    type Value = PositionedValue;
    type Mapping = Vec<(String, PositionedValue)>;

    fn v_null(line: u32) -> PositionedValue {
        PositionedValue::Null {
            line,
            inserted_at: None,
            prior_insertions: Vec::new(),
        }
    }
    fn v_bool(value: bool, line: u32) -> PositionedValue {
        PositionedValue::Bool {
            value,
            line,
            inserted_at: None,
            prior_insertions: Vec::new(),
        }
    }
    fn v_int(value: i64, line: u32) -> PositionedValue {
        PositionedValue::Int {
            value,
            line,
            inserted_at: None,
            prior_insertions: Vec::new(),
        }
    }
    fn v_float(value: f64, line: u32) -> PositionedValue {
        PositionedValue::Float {
            value,
            line,
            inserted_at: None,
            prior_insertions: Vec::new(),
        }
    }
    fn v_string(value: String, line: u32, quoted: bool) -> PositionedValue {
        PositionedValue::String {
            value,
            line,
            quoted,
            ref_source: None,
            inserted_at: None,
            prior_insertions: Vec::new(),
        }
    }
    fn v_string_src(value: String, line: u32, source: ReferenceSource) -> PositionedValue {
        PositionedValue::String {
            value,
            line,
            quoted: false,
            ref_source: Some(source),
            inserted_at: None,
            prior_insertions: Vec::new(),
        }
    }
    fn v_instant(value: Instant, line: u32) -> PositionedValue {
        PositionedValue::Instant {
            value,
            line,
            inserted_at: None,
            prior_insertions: Vec::new(),
        }
    }
    fn v_array(items: Vec<PositionedValue>, line: u32) -> PositionedValue {
        PositionedValue::Array {
            items,
            line,
            inserted_at: None,
            prior_insertions: Vec::new(),
        }
    }
    fn v_mapping(entries: Self::Mapping, line: u32) -> PositionedValue {
        PositionedValue::Mapping {
            entries,
            line,
            inserted_at: None,
            prior_insertions: Vec::new(),
        }
    }

    fn m_create() -> Self::Mapping {
        Vec::new()
    }
    fn m_create_with(key: String, value: PositionedValue) -> Self::Mapping {
        vec![(key, value)]
    }
    fn m_has_key(entries: &Self::Mapping, key: &str) -> bool {
        entries.iter().any(|(k, _)| k == key)
    }
    fn m_set(entries: &mut Self::Mapping, key: String, value: PositionedValue) {
        set_mapping(entries, key, value)
    }
    fn m_max_depth(entries: &Self::Mapping) -> u32 {
        entries.iter().map(|(_, v)| v.depth()).max().unwrap_or(0)
    }
}

/// Days since the Unix epoch (1970-01-01) for a proleptic-Gregorian
/// (year, month, day) — Howard Hinnant's `days_from_civil`, the standard
/// dependency-free algorithm for this conversion (correct for all years,
/// including the divisible-by-400 leap rule).
pub fn days_from_civil(y: i64, m: u32, d: u32) -> i64 {
    let y = if m <= 2 { y - 1 } else { y };
    let era = if y >= 0 { y } else { y - 399 } / 400;
    let yoe = y - era * 400; // [0, 399]
    let mp = (m as i64 + 9) % 12; // [0, 11], Mar=0 .. Feb=11
    let doy = (153 * mp + 2) / 5 + d as i64 - 1; // [0, 365]
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
    era * 146_097 + doe - 719_468
}

/// Inverse of [`days_from_civil`].
pub fn civil_from_days(z: i64) -> (i64, u32, u32) {
    let z = z + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let doe = z - era * 146_097; // [0, 146096]
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399]
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
    let mp = (5 * doy + 2) / 153; // [0, 11]
    let d = (doy - (153 * mp + 2) / 5 + 1) as u32; // [1, 31]
    let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32; // [1, 12]
    (if m <= 2 { y + 1 } else { y }, m, d)
}

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

    #[test]
    fn days_from_civil_roundtrips() {
        for &(y, m, d) in &[
            (1970, 1, 1),
            (2024, 2, 29),
            (2000, 2, 29),
            (1, 1, 1),
            (9999, 12, 31),
            (1969, 12, 31),
        ] {
            let days = days_from_civil(y, m, d);
            assert_eq!(
                civil_from_days(days),
                (y, m, d),
                "roundtrip failed for {y}-{m}-{d}"
            );
        }
        assert_eq!(days_from_civil(1970, 1, 1), 0);
        assert_eq!(
            days_from_civil(2024, 3, 1),
            days_from_civil(2024, 2, 29) + 1
        );
    }
}