tread 0.4.3

A terminal reader for markdown, CSV, JSON and source code. Zero dependencies.
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
//! The lazy structural index (SPEC.md §JSON, "Structural indexing").
//!
//! A container is indexed by *byte range*, never parsed. Finding the boundaries
//! of a container's immediate members is a linear byte walk with a depth
//! counter, an in-string flag and an escape flag: it builds no values, and it
//! allocates nothing per member beyond three offsets. That is what lets a 900MB
//! JSON open as fast as a 900MB CSV — the same discipline as
//! [`crate::csv::index`], one level down.
//!
//! # Resumable
//!
//! [`Scan`] is fed one chunk at a time and remembers where it stopped, exactly
//! as [`crate::csv::parse::Scanner`] does, including mid-string and mid-escape.
//! A caller therefore spends a bounded number of bytes per frame and comes back
//! for the rest, which is what keeps `q` from ever waiting on a scan. Nothing
//! here reads a file: chunks come from above, so this module is pure and
//! host-tested against byte slices.
//!
//! # Every level, not just the root
//!
//! One [`Scan`] indexes *one* container. Expanding a node runs another over
//! that node's bytes, so laziness is not limited to the top level: a document
//! that is one object holding one enormous array stays instant, because the
//! array is only walked when it is opened.
//!
//! # Nothing recurses
//!
//! The walk is a `for` loop over bytes with an integer depth. Ten thousand
//! levels of `[[[[` cost ten thousand increments of a `u32` and no stack at
//! all.
#![deny(unsafe_code)]

/// One immediate member of a container, as byte ranges into the document.
///
/// `key` is the *quoted* key of an object member, including both quotes;
/// `None` for an array element. `start..end` is the member's value, with
/// surrounding whitespace already trimmed off.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Member {
    pub key: Option<(u64, u64)>,
    pub start: u64,
    pub end: u64,
}

impl Member {
    /// Bytes the value occupies. The cap on parsing a member is applied to
    /// this, never to the document.
    pub fn len(&self) -> u64 {
        self.end.saturating_sub(self.start)
    }
}

/// What a value is, from its first byte alone — no parsing, no allocation.
///
/// This is how a row knows whether it is a container (and so has a count and a
/// fold) before anything reads it. A byte that starts nothing valid is
/// [`Shape::Bad`], which renders as an error row rather than stopping the file.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Shape {
    Array,
    Object,
    Str,
    Number,
    Bool,
    Null,
    Bad,
}

impl Shape {
    pub fn of(first: u8) -> Shape {
        match first {
            b'[' => Shape::Array,
            b'{' => Shape::Object,
            b'"' => Shape::Str,
            b'-' | b'0'..=b'9' => Shape::Number,
            b't' | b'f' => Shape::Bool,
            b'n' => Shape::Null,
            _ => Shape::Bad,
        }
    }

    pub fn is_container(self) -> bool {
        matches!(self, Shape::Array | Shape::Object)
    }

    /// The pair of brackets a container is written with.
    pub fn brackets(self) -> (&'static str, &'static str) {
        match self {
            Shape::Object => ("{", "}"),
            _ => ("[", "]"),
        }
    }

    /// What a collapsed container counts, singular and plural.
    pub fn unit(self, n: usize) -> &'static str {
        match (self, n) {
            (Shape::Object, 1) => "key",
            (Shape::Object, _) => "keys",
            (_, 1) => "item",
            (_, _) => "items",
        }
    }
}

/// The document's root: where the first value starts, and what it is.
///
/// Reads only the head of the file — a BOM and whatever whitespace precedes the
/// first value. An empty document has no root.
pub fn root(head: &[u8], base: u64) -> Option<(u64, Shape)> {
    let bom: &[u8] = &[0xef, 0xbb, 0xbf];
    let skip = usize::from(head.starts_with(bom)) * bom.len();
    let at = head[skip..]
        .iter()
        .position(|b| !matches!(b, b' ' | b'\t' | b'\n' | b'\r'))?;
    let i = skip + at;
    Some((base + i as u64, Shape::of(head[i])))
}

/// A resumable structural walk over one container's immediate members.
///
/// Created just after the opening `[` or `{`; fed chunks in file order; emits a
/// [`Member`] per immediate member and stops on the matching close, recording
/// where the container ends.
#[derive(Clone, Debug)]
pub struct Scan {
    /// Absolute offset of the next byte to consume.
    pos: u64,
    /// The container is an object, so a member starts with a key.
    obj: bool,
    /// Nesting inside the current member. Members are found at depth 0.
    depth: u32,
    in_str: bool,
    esc: bool,
    /// Where the key string began, while it is being read.
    key_open: Option<u64>,
    key: Option<(u64, u64)>,
    colon: bool,
    val: Option<u64>,
    /// One past the last non-whitespace byte of the value so far.
    val_end: u64,
    /// One past the container's closing bracket, once it has been seen.
    end: Option<u64>,
    /// The container ran off the end of the document.
    truncated: bool,
    count: usize,
}

impl Scan {
    /// A scan of the container whose opening bracket is at `open_at`.
    pub fn new(open_at: u64, obj: bool) -> Scan {
        Scan {
            pos: open_at + 1,
            obj,
            depth: 0,
            in_str: false,
            esc: false,
            key_open: None,
            key: None,
            colon: false,
            val: None,
            val_end: 0,
            end: None,
            truncated: false,
            count: 0,
        }
    }

    /// Absolute offset the next chunk must start at.
    pub fn pos(&self) -> u64 {
        self.pos
    }

    /// The container's close has been found (or the file ended).
    pub fn done(&self) -> bool {
        self.end.is_some()
    }

    /// One past the container's last byte, once known.
    pub fn end(&self) -> Option<u64> {
        self.end
    }

    /// Members emitted so far. The count a collapsed row shows is kept by the
    /// caller, which knows which container it belongs to; this is the tests'
    /// spelling of the same fact.
    #[cfg(test)]
    pub fn count(&self) -> usize {
        self.count
    }

    /// The container was cut off by the end of the document. Nothing above
    /// changes its behaviour for that — a half-written container still shows
    /// the members it has — so only the tests ask.
    #[cfg(test)]
    pub fn truncated(&self) -> bool {
        self.truncated
    }

    /// Consume `chunk`, which must start at [`Scan::pos`], reporting every
    /// member that ends inside it. Stops at the container's close, leaving the
    /// rest of the chunk — it belongs to the parent, not to us.
    pub fn feed(&mut self, chunk: &[u8], sink: &mut dyn FnMut(Member)) {
        let base = self.pos;
        for (k, &b) in chunk.iter().enumerate() {
            let p = base + k as u64;
            if self.byte(p, b, sink) {
                self.pos = p + 1;
                return;
            }
        }
        self.pos = base + chunk.len() as u64;
    }

    /// One byte. Returns true when it closed the container.
    fn byte(&mut self, p: u64, b: u8, sink: &mut dyn FnMut(Member)) -> bool {
        if self.in_str {
            self.in_string(p, b);
            return false;
        }
        match b {
            b'"' => self.quote(p),
            b'[' | b'{' => {
                self.start_value(p);
                self.depth += 1;
                self.val_end = p + 1;
            }
            b']' | b'}' if self.depth == 0 => {
                self.flush(sink);
                self.end = Some(p + 1);
                return true;
            }
            b']' | b'}' => {
                self.depth -= 1;
                self.val_end = p + 1;
            }
            b',' if self.depth == 0 => self.flush(sink),
            b':' if self.depth == 0 && self.obj && !self.colon && self.val.is_none() => {
                self.colon = true;
            }
            b' ' | b'\t' | b'\n' | b'\r' => {}
            _ => {
                self.start_value(p);
                self.val_end = p + 1;
            }
        }
        false
    }

    /// A byte inside a string: only the escape rules matter, since a `]` or a
    /// `,` in there is data and not structure.
    fn in_string(&mut self, p: u64, b: u8) {
        if self.esc {
            self.esc = false;
        } else if b == b'\\' {
            self.esc = true;
        } else if b == b'"' {
            self.in_str = false;
            if let Some(s) = self.key_open.take() {
                self.key = Some((s, p + 1));
            }
        }
        if self.val.is_some() {
            self.val_end = p + 1;
        }
    }

    /// An opening quote outside a string: the member's key, or its value.
    fn quote(&mut self, p: u64) {
        self.in_str = true;
        if self.obj && self.val.is_none() && !self.colon && self.key.is_none() {
            self.key_open = Some(p);
            return;
        }
        self.start_value(p);
        self.val_end = p + 1;
    }

    fn start_value(&mut self, p: u64) {
        if self.val.is_none() {
            self.val = Some(p);
        }
    }

    /// Emit the member just ended, if there was one, and reset for the next.
    ///
    /// A trailing comma or an empty container flushes nothing. A key with no
    /// value — malformed input, a truncated file — still emits, keyed, with an
    /// **empty** value range starting where the value would have begun. The row
    /// then renders as a parse error naming that offset, which is more use than
    /// the member silently disappearing. It must not borrow the key's own bytes
    /// for the value: those are valid JSON, so `{"beta":` would display as
    /// `"beta": "beta"` — text the document does not contain.
    fn flush(&mut self, sink: &mut dyn FnMut(Member)) {
        let key = self.key.take();
        let val = self.val.take();
        let (start, end) = match (val, key) {
            (Some(s), _) => (s, self.val_end.max(s)),
            (None, Some((_, e))) => (e, e),
            (None, None) => {
                self.reset();
                return;
            }
        };
        sink(Member { key, start, end });
        self.count += 1;
        self.reset();
    }

    fn reset(&mut self) {
        self.key_open = None;
        self.key = None;
        self.colon = false;
        self.val = None;
        self.val_end = 0;
        self.in_str = false;
        self.esc = false;
        self.depth = 0;
    }

    /// The document ended before the container closed. Whatever member was
    /// being read is emitted — half a document is still worth reading — and the
    /// container is marked as ending here.
    pub fn finish(&mut self, at: u64, sink: &mut dyn FnMut(Member)) {
        if self.end.is_some() {
            return;
        }
        self.val_end = self.val_end.min(at);
        self.flush(sink);
        self.end = Some(at);
        self.truncated = true;
    }
}

/// Every member of one container, in document order, compactly.
///
/// Four offsets a member — key start, key end, value start, value end — held as
/// `u32` deltas from the container's own start, so a member costs 16 bytes and
/// the index of a multi-million element array stays in tens of megabytes rather
/// than hundreds. A container whose extent passes 4GiB promotes to `u64` and
/// stays exact. An array element has no key, which is recorded as an empty key
/// range rather than as a fifth field.
#[derive(Debug)]
pub struct Members {
    base: u64,
    store: Store,
}

#[derive(Debug)]
enum Store {
    Narrow(Vec<[u32; 4]>),
    Wide(Vec<[u64; 4]>),
}

impl Members {
    pub fn new(base: u64) -> Members {
        Members { base, store: Store::Narrow(Vec::new()) }
    }

    pub fn len(&self) -> usize {
        match &self.store {
            Store::Narrow(v) => v.len(),
            Store::Wide(v) => v.len(),
        }
    }

    /// True when the container has no members yet. Kept beside
    /// [`Members::len`] because a `len` without an `is_empty` is a lint, and
    /// used by the tests.
    #[cfg(test)]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Record one member. Offsets only grow, so a delta never underflows.
    pub fn push(&mut self, m: Member) {
        let (ks, ke) = m.key.unwrap_or((m.start, m.start));
        let raw = [ks, ke, m.start, m.end].map(|o| o.saturating_sub(self.base));
        if raw.iter().any(|d| *d >= u32::MAX as u64) {
            self.promote();
        }
        match &mut self.store {
            Store::Narrow(v) => v.push(raw.map(|d| d as u32)),
            Store::Wide(v) => v.push(raw),
        }
    }

    fn promote(&mut self) {
        if let Store::Narrow(v) = &self.store {
            self.store = Store::Wide(v.iter().map(|t| t.map(u64::from)).collect());
        }
    }

    /// Member `i`, or `None` past the end.
    pub fn get(&self, i: usize) -> Option<Member> {
        let raw = match &self.store {
            Store::Narrow(v) => v.get(i)?.map(u64::from),
            Store::Wide(v) => *v.get(i)?,
        };
        let [ks, ke, start, end] = raw.map(|d| self.base + d);
        Some(Member { key: (ks != ke).then_some((ks, ke)), start, end })
    }

    /// Approximate heap cost, for the tests that pin the per-member size.
    #[cfg(test)]
    pub fn bytes(&self) -> usize {
        match &self.store {
            Store::Narrow(v) => v.capacity() * 16,
            Store::Wide(v) => v.capacity() * 32,
        }
    }
}

#[cfg(test)]
#[path = "index_tests.rs"]
mod tests;