aless 0.1.0

Terminal viewer and JSON CLI for JSON, YAML, TOML, CSV, XML and more
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
//! Text forms of keys, values, paths and subtrees: what the tree pane
//! shows, what search runs over, and what yank puts on the clipboard.

use crate::doc::{Doc, Key, Kind, NodeId};

/// A number the way JavaScript would print it: integers without a
/// fraction, everything else in the shortest round-trip form.
pub fn number(n: f64) -> String {
    if n.is_nan() {
        return "NaN".to_string();
    }
    if n.is_infinite() {
        return if n > 0.0 { "Infinity" } else { "-Infinity" }.to_string();
    }
    if n.fract() == 0.0 && n.abs() < 1e16 {
        return format!("{}", n as i64);
    }
    match serde_json::Number::from_f64(n) {
        Some(x) => x.to_string(),
        None => n.to_string(),
    }
}

/// A JSON string literal.
pub fn quote(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            '\u{8}' => out.push_str("\\b"),
            '\u{c}' => out.push_str("\\f"),
            c if (c as u32) < 0x20 || c == '\u{7f}' => {
                out.push_str(&format!("\\u{:04x}", c as u32));
            }
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// Would this key be a valid JavaScript identifier? (jless shows such keys
/// unquoted in data mode.)
pub fn is_identifier(s: &str) -> bool {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) if c.is_alphabetic() || c == '_' || c == '$' => {}
        _ => return false,
    }
    chars.all(|c| c.is_alphanumeric() || c == '_' || c == '$')
}

/// The display form of a key. Object keys are bare in data mode when they
/// are identifiers and quoted otherwise; line mode always quotes. Array
/// indices and the root have no key text.
pub fn key_text(key: &Key, line_mode: bool) -> Option<String> {
    match key {
        Key::Name(n) => Some(if !line_mode && is_identifier(n) {
            n.to_string()
        } else {
            quote(n)
        }),
        _ => None,
    }
}

/// The display form of a scalar.
pub fn leaf_text(kind: &Kind) -> String {
    match kind {
        Kind::Null => "null".to_string(),
        Kind::Bool(b) => b.to_string(),
        Kind::Number(n) => number(*n),
        Kind::Str(s) => quote(s),
        Kind::Object => "{".to_string(),
        Kind::Array => "[".to_string(),
    }
}

pub fn open_bracket(kind: &Kind) -> &'static str {
    match kind {
        Kind::Array => "[",
        _ => "{",
    }
}

pub fn close_bracket(kind: &Kind) -> &'static str {
    match kind {
        Kind::Array => "]",
        _ => "}",
    }
}

/// The jless-style preview of a collapsed container: its item count, then
/// as many leading items as fit in `budget` characters, then `…`.
pub fn preview(doc: &Doc, id: NodeId, budget: usize) -> String {
    let node = doc.node(id);
    let (open, close) = (open_bracket(&node.kind), close_bracket(&node.kind));
    if node.children == 0 {
        return format!("{open}{close}");
    }
    let mut out = format!("({}) {}", node.children, open);
    let budget = budget.max(out.len() + 3);
    let mut first = true;
    for c in doc.children(id) {
        let child = doc.node(c);
        let mut item = String::new();
        if let Key::Name(n) = &child.key {
            if is_identifier(n) {
                item.push_str(n);
            } else {
                item.push_str(&quote(n));
            }
            item.push_str(": ");
        }
        match &child.kind {
            Kind::Object => item.push_str(if child.children == 0 { "{}" } else { "{…}" }),
            Kind::Array => item.push_str(if child.children == 0 { "[]" } else { "[…]" }),
            k => item.push_str(&leaf_text(k)),
        }
        let sep = if first { "" } else { ", " };
        // Keep room for ", …" and the closing bracket.
        if out.chars().count() + sep.len() + item.chars().count() + 3 > budget {
            out.push_str(if first { "…" } else { ", …" });
            out.push_str(close);
            return out;
        }
        out.push_str(sep);
        out.push_str(&item);
        first = false;
    }
    out.push_str(close);
    out
}

/// The text search runs over for one node: the line-mode rendering of its
/// own row (`"key": value`, or `"key": {` for a container).
pub fn search_text(doc: &Doc, id: NodeId) -> String {
    let node = doc.node(id);
    let mut out = String::new();
    if let Some(k) = key_text(&node.key, true) {
        out.push_str(&k);
        out.push_str(": ");
    }
    match &node.kind {
        Kind::Object => out.push_str(if node.children == 0 { "{}" } else { "{" }),
        Kind::Array => out.push_str(if node.children == 0 { "[]" } else { "[" }),
        k => out.push_str(&leaf_text(k)),
    }
    out
}

/// Serialise the subtree at `id` as pretty-printed JSON.
pub fn to_json_pretty(doc: &Doc, id: NodeId, indent: usize) -> String {
    serialize(doc, id, Layout::Pretty(indent))
}

/// Serialise the subtree at `id` as one line of JSON, `, ` and `: ` spaced.
pub fn to_json_line(doc: &Doc, id: NodeId) -> String {
    serialize(doc, id, Layout::Line)
}

/// Serialise the subtree at `id` as one line of JSON with no spaces.
pub fn to_json_compact(doc: &Doc, id: NodeId) -> String {
    serialize(doc, id, Layout::Compact)
}

#[derive(Clone, Copy, PartialEq)]
enum Layout {
    /// One item per line, indented this many spaces a level.
    Pretty(usize),
    /// One line, `, ` and `: ` spaced.
    Line,
    /// One line, no spaces.
    Compact,
}

fn serialize(doc: &Doc, root: NodeId, layout: Layout) -> String {
    enum Step {
        Open(NodeId),
        Close(NodeId),
    }
    let indent = match layout {
        Layout::Pretty(w) => Some(w),
        _ => None,
    };
    let colon = if layout == Layout::Compact { ":" } else { ": " };
    let comma = if layout == Layout::Line { ", " } else { "," };
    let base_depth = doc.node(root).depth;
    let mut out = String::new();
    let mut stack = vec![Step::Open(root)];
    let pad = |out: &mut String, depth: u32| {
        if let Some(w) = indent {
            for _ in 0..((depth - base_depth) as usize * w) {
                out.push(' ');
            }
        }
    };
    let nl = |out: &mut String| {
        if indent.is_some() {
            out.push('\n');
        }
    };
    while let Some(step) = stack.pop() {
        match step {
            Step::Open(id) => {
                let node = doc.node(id);
                pad(&mut out, node.depth);
                if id != root {
                    if let Key::Name(n) = &node.key {
                        out.push_str(&quote(n));
                        out.push_str(colon);
                    }
                }
                let more = id != root && doc.next_sibling(id).is_some();
                match &node.kind {
                    Kind::Object | Kind::Array if node.children > 0 => {
                        out.push_str(open_bracket(&node.kind));
                        nl(&mut out);
                        stack.push(Step::Close(id));
                        let kids: Vec<NodeId> = doc.children(id).collect();
                        for c in kids.into_iter().rev() {
                            stack.push(Step::Open(c));
                        }
                    }
                    Kind::Object | Kind::Array => {
                        out.push_str(open_bracket(&node.kind));
                        out.push_str(close_bracket(&node.kind));
                        if more {
                            out.push_str(comma);
                        }
                        nl(&mut out);
                    }
                    k => {
                        match k {
                            Kind::Number(n) if !n.is_finite() => out.push_str("null"),
                            k => out.push_str(&leaf_text(k)),
                        }
                        if more {
                            out.push_str(comma);
                        }
                        nl(&mut out);
                    }
                }
            }
            Step::Close(id) => {
                let node = doc.node(id);
                pad(&mut out, node.depth);
                out.push_str(close_bracket(&node.kind));
                if id != root && doc.next_sibling(id).is_some() {
                    out.push_str(comma);
                }
                nl(&mut out);
            }
        }
    }
    if indent.is_some() && out.ends_with('\n') {
        out.pop();
    }
    out
}

/// `.foo[3].bar` — jless's dot path. Keys that are not identifiers are
/// written as `["weird key"]`.
pub fn path_dot(path: &[Key]) -> String {
    let mut out = String::new();
    for k in path {
        match k {
            Key::Root => {}
            Key::Index(i) => out.push_str(&format!("[{i}]")),
            Key::Name(n) => {
                if is_identifier(n) {
                    out.push('.');
                    out.push_str(n);
                } else {
                    out.push('[');
                    out.push_str(&quote(n));
                    out.push(']');
                }
            }
        }
    }
    out
}

/// `["foo"][3]["bar"]` — every segment in brackets.
pub fn path_bracket(path: &[Key]) -> String {
    let mut out = String::new();
    for k in path {
        match k {
            Key::Root => {}
            Key::Index(i) => out.push_str(&format!("[{i}]")),
            Key::Name(n) => {
                out.push('[');
                out.push_str(&quote(n));
                out.push(']');
            }
        }
    }
    out
}

/// Can jq's `.field` shorthand take this key bare? jq is stricter than
/// JavaScript: ASCII letters, digits and underscores only.
pub fn is_jq_identifier(s: &str) -> bool {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) if c.is_ascii_alphabetic() || c == '_' => {}
        _ => return false,
    }
    chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}

/// `.foo[3].bar` in jq's syntax; keys jq cannot take bare become
/// `."weird key"`, and an index first in the path follows a dot, `.[0]`,
/// since jq reads a bare `[0]` as an array of its own.
pub fn path_jq(path: &[Key]) -> String {
    let mut out = String::new();
    for k in path {
        match k {
            Key::Root => {}
            Key::Index(i) => push_jq_index(&mut out, i64::from(*i)),
            Key::Name(n) => push_jq_name(&mut out, n),
        }
    }
    if out.is_empty() {
        out.push('.');
    }
    out
}

/// Add an index step to a jq path being written: `[3]`, or `.[3]` when it
/// is the path's first step. A negative index counts from the end, as jq's
/// does: `.[-1]`.
pub fn push_jq_index(out: &mut String, i: i64) {
    if out.is_empty() {
        out.push('.');
    }
    out.push_str(&format!("[{i}]"));
}

/// Add a key step to a jq path being written: `.foo`, or `."weird key"`
/// for a key jq cannot take bare.
pub fn push_jq_name(out: &mut String, n: &str) {
    out.push('.');
    if is_jq_identifier(n) {
        out.push_str(n);
    } else {
        out.push_str(&quote(n));
    }
}

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

    fn doc(src: &str) -> Doc {
        Doc::from_value(&tabnas_json::parse(src).unwrap())
    }

    #[test]
    fn numbers() {
        assert_eq!(number(3.0), "3");
        assert_eq!(number(-0.0), "0");
        assert_eq!(number(42.5), "42.5");
        assert_eq!(number(1e21), "1e+21");
        assert_eq!(number(1.5e-7), "1.5e-7");
        assert_eq!(number(f64::NAN), "NaN");
        assert_eq!(number(f64::NEG_INFINITY), "-Infinity");
    }

    #[test]
    fn quoting_and_identifiers() {
        assert_eq!(quote("a\"b\\c\n\u{1}"), r#""a\"b\\c\n\u0001""#);
        assert!(is_identifier("foo_bar$1"));
        assert!(!is_identifier("1abc"));
        assert!(!is_identifier("a-b"));
        assert!(!is_identifier(""));
        assert_eq!(
            key_text(&Key::Name("abc".into()), false),
            Some("abc".into())
        );
        assert_eq!(
            key_text(&Key::Name("a b".into()), false),
            Some("\"a b\"".into())
        );
        assert_eq!(
            key_text(&Key::Name("abc".into()), true),
            Some("\"abc\"".into())
        );
        assert_eq!(key_text(&Key::Index(2), false), None);
    }

    #[test]
    fn previews() {
        let d = doc(r#"{"a": 1, "b": "x", "c": [1, 2], "d": {}}"#);
        assert_eq!(preview(&d, 0, 200), r#"(4) {a: 1, b: "x", c: […], d: {}}"#);
        assert_eq!(preview(&d, 0, 22), r#"(4) {a: 1, b: "x", …}"#);
        assert_eq!(preview(&d, 0, 18), r#"(4) {a: 1, …}"#);
        assert_eq!(preview(&d, 0, 1), "(4) {…}");
        assert_eq!(preview(&d, 6, 50), "{}");
        assert_eq!(preview(&d, 3, 50), "(2) [1, 2]");
    }

    #[test]
    fn serialisation() {
        let d = doc(r#"{"a": 1, "b": [true, {"c": "x"}], "d": {}, "e": []}"#);
        let pretty = to_json_pretty(&d, 0, 2);
        let expect = "{\n  \"a\": 1,\n  \"b\": [\n    true,\n    {\n      \"c\": \"x\"\n    }\n  ],\n  \"d\": {},\n  \"e\": []\n}";
        assert_eq!(pretty, expect);
        assert_eq!(
            to_json_line(&d, 0),
            r#"{"a": 1, "b": [true, {"c": "x"}], "d": {}, "e": []}"#
        );
        assert_eq!(
            to_json_compact(&d, 0),
            r#"{"a":1,"b":[true,{"c":"x"}],"d":{},"e":[]}"#
        );
        // A subtree root takes no trailing comma even with a next sibling.
        assert_eq!(to_json_line(&d, 2), r#"[true, {"c": "x"}]"#);
        assert_eq!(to_json_pretty(&d, 1, 2), "1");
        let round: serde_json::Value = serde_json::from_str(&pretty).unwrap();
        assert_eq!(round["b"][1]["c"], "x");
    }

    #[test]
    fn paths() {
        let p = vec![
            Key::Name("foo".into()),
            Key::Index(3),
            Key::Name("bar baz".into()),
        ];
        assert_eq!(path_dot(&p), r#".foo[3]["bar baz"]"#);
        assert_eq!(path_bracket(&p), r#"["foo"][3]["bar baz"]"#);
        assert_eq!(path_jq(&p), r#".foo[3]."bar baz""#);
        assert_eq!(path_jq(&[]), ".");
        // A root array's items: jq takes `.[0]`, and reads `[0]` as an
        // array literal. Only the first step needs the dot.
        assert_eq!(path_jq(&[Key::Index(0)]), ".[0]");
        assert_eq!(
            path_jq(&[Key::Index(1), Key::Name("name".into())]),
            ".[1].name"
        );
        assert_eq!(path_jq(&[Key::Index(1), Key::Index(2)]), ".[1][2]");
        // jless's dot path keeps its own form.
        assert_eq!(path_dot(&[Key::Index(0)]), "[0]");
        let mut negative = String::new();
        push_jq_index(&mut negative, -1);
        assert_eq!(negative, ".[-1]");
        // jq takes fewer keys bare than JavaScript does.
        let jq = |k: &str| path_jq(&[Key::Name(k.into())]);
        assert_eq!(jq("a_b1"), ".a_b1");
        assert_eq!(jq("$foo"), r#"."$foo""#);
        assert_eq!(jq("é"), r#"."é""#);
        assert_eq!(jq("1a"), r#"."1a""#);
        assert_eq!(path_dot(&[Key::Name("$foo".into())]), ".$foo");
        assert_eq!(path_dot(&[]), "");
    }

    #[test]
    fn search_texts() {
        let d = doc(r#"{"a": 1, "b": [true], "c d": {}}"#);
        assert_eq!(search_text(&d, 0), "{");
        assert_eq!(search_text(&d, 1), "\"a\": 1");
        assert_eq!(search_text(&d, 2), "\"b\": [");
        assert_eq!(search_text(&d, 3), "true");
        assert_eq!(search_text(&d, 4), "\"c d\": {}");
    }
}