kopitiam_document/list.rs
1#[derive(Debug, Clone, PartialEq)]
2pub struct List {
3 pub ordered: bool,
4 pub items: Vec<String>,
5 /// Nesting depth of each item, **parallel to `items`** (`depths[i]` is the
6 /// indent level of `items[i]`, `0` = top level). Marker-style list nesting
7 /// (`kopitiam_token_max.md` task #16): reconstruction infers depth from the
8 /// left-edge x-indentation of each item's line and the renderer indents
9 /// accordingly.
10 ///
11 /// # Why a parallel `Vec`, not `Vec<ListItem>`
12 ///
13 /// `items` stays `Vec<String>` so existing readers that iterate the item
14 /// text unchanged (e.g. `kopitiam-insurance`'s clause ingest, which appends
15 /// each item as a line) keep compiling and behaving exactly as before. A
16 /// flat list built by hand can leave `depths` **empty**, which
17 /// [`List::depth`] reads as "every item is top level" — so a `Default`/
18 /// hand-constructed list needs no depth bookkeeping.
19 ///
20 /// Invariant: `depths` is either empty (all top level) or exactly
21 /// `items.len()` long. [`List::depth`] upholds the "empty means flat"
22 /// reading regardless.
23 pub depths: Vec<usize>,
24}
25
26impl List {
27 /// A flat list (every item at top level), the shape hand-built call sites
28 /// and pre-nesting reconstruction produce. `depths` is left empty, which
29 /// [`List::depth`] reads as all-zero.
30 pub fn flat(ordered: bool, items: Vec<String>) -> Self {
31 Self {
32 ordered,
33 items,
34 depths: Vec::new(),
35 }
36 }
37
38 /// Nesting depth of item `i` (`0` = top level). Returns `0` for any index
39 /// when `depths` is empty (a flat list) or out of range, so callers never
40 /// have to special-case the flat shape.
41 pub fn depth(&self, i: usize) -> usize {
42 self.depths.get(i).copied().unwrap_or(0)
43 }
44}