lino-objects-codec 0.5.0

A library to encode/decode objects to/from links notation
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
//! Readable, indented Links Notation representation.
//!
//! This module implements the default output of [`crate::encode`]: a plain-text,
//! indented projection where keys and values are written as they are, so the file
//! can be read, grepped and reviewed without decoding anything.
//!
//! # Shape
//!
//! One construct — `( )` — is used for both objects and arrays, at every level
//! including the root. What distinguishes them is the content of the lines:
//! `key value` pairs make an object, bare values make an array.
//!
//! ```text
//! (
//!   type "RouterState"
//!   server (
//!     host "127.0.0.1"
//!     port 18878
//!   )
//!   models (
//!     "claude-haiku"
//!     "claude-opus"
//!   )
//! )
//! ```
//!
//! # Value mapping
//!
//! | `LinoValue`                | Readable form                                  |
//! |----------------------------|------------------------------------------------|
//! | `Object`                   | `( )` with one `key value` pair per line        |
//! | `Array`                    | `( )` with one value per line                   |
//! | `String`                   | quoted, never encoded                           |
//! | `Int` / `Float` / `Bool` / `Null` | bare, so the type survives the round trip |
//!
//! Empty containers keep their type: an empty array is `()` on one line, while an
//! empty object is written as `(` and `)` on two lines.
//!
//! Only values that cannot be written as plain text are encoded: strings holding
//! control characters (including newlines and tabs, which line-based tooling and
//! CRLF normalisation would corrupt) are marked individually as
//! `(base64 "…")` instead of encoding the whole document.
//!
//! # Single-line form
//!
//! [`encode_line`] writes the same document on one line, so one record is one
//! line and an append-only log stays greppable, tailable and countable by
//! `wc -l`. Rows can no longer be told apart by line breaks there, so an object
//! names itself with the `o` link id the notation already has, and its pairs are
//! written as their own links:
//!
//! ```text
//! (o: (type "RouterState") (server (o: (host "127.0.0.1") (port 18878))) (models ("claude-haiku" "claude-opus")))
//! ```
//!
//! | Value            | Single-line form              |
//! |------------------|-------------------------------|
//! | `Object`         | `(o: (key value) …)`          |
//! | empty `Object`   | `(o:)`                        |
//! | `Array`          | `(value …)`                   |
//! | empty `Array`    | `()`                          |
//! | scalars          | exactly as in the indented form |
//!
//! The marker is what answers the ambiguity a flat layout otherwise has: without
//! it `((key value))` reads both as the one-pair object and as the array holding
//! the two-element array, and an empty key makes it worse. With it, a bare `( )`
//! is always an array and a marked one is always an object, so every value —
//! empty key included — survives the round trip. Consequently a *hand-written*
//! one-line link such as `(a 1)` is the two-element array, not the one-pair
//! object: on one line, objects say so.

use crate::debug::trace;
use crate::{CodecError, LinoValue};
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};

/// Default indentation used by [`encode`].
pub const DEFAULT_INDENT: &str = "  ";

/// Marker used for values that cannot be represented as plain text.
pub const BASE64_MARKER: &str = "base64";

/// Link id naming an object in the single-line form, written as `(o: …)`.
pub const OBJECT_MARKER: &str = "o";

/// Encode a value into the readable, indented Links Notation form.
pub fn encode(value: &LinoValue, indent: &str) -> String {
    let mut out = String::new();
    write_value(value, indent, 0, &mut out);
    out
}

/// Encode a value into the readable, single-line Links Notation form.
///
/// The result never contains a newline, so one value is one line of an
/// append-only log. See the module documentation for the shape.
pub fn encode_line(value: &LinoValue) -> String {
    let mut out = String::new();
    write_line_value(value, &mut out);
    out
}

/// Decode the readable, single-line Links Notation form back into a value.
///
/// This is the exact inverse of [`encode_line`]. Input spanning more than one
/// line is rejected: a line-based reader hands over one record at a time, and
/// silently accepting several would merge two records into one value.
pub fn decode_line(text: &str) -> Result<LinoValue, CodecError> {
    let line = text.trim_matches(|c: char| c == '\n' || c == '\r');
    if line.contains('\n') || line.contains('\r') {
        return Err(CodecError::ParseError(
            "a single-line document cannot contain a line break".to_string(),
        ));
    }
    decode(line)
}

/// Decode the readable, indented Links Notation form back into a value.
pub fn decode(text: &str) -> Result<LinoValue, CodecError> {
    let tokens = tokenize(text)?;
    trace("readable.decode", || format!("{} tokens", tokens.len()));
    let mut cursor = Cursor { tokens, pos: 0 };
    let rows = cursor.parse_rows(true)?;

    if cursor.pos < cursor.tokens.len() {
        return Err(CodecError::ParseError(
            "unexpected ')' in readable notation".to_string(),
        ));
    }

    // A document holding a single value (for example `42`) is that value.
    if rows.len() == 1 && rows[0].len() == 1 {
        return node_to_value(&rows[0][0]);
    }

    rows_to_value(&rows, true, false)
}

// === Encoding ===

fn write_value(value: &LinoValue, indent: &str, level: usize, out: &mut String) {
    match value {
        LinoValue::Object(pairs) => {
            if pairs.is_empty() {
                // An empty object spans two lines; `()` on one line is an empty array.
                out.push_str("(\n");
                push_indent(indent, level, out);
                out.push(')');
                return;
            }

            out.push('(');
            for (key, child) in pairs {
                out.push('\n');
                push_indent(indent, level + 1, out);
                out.push_str(&format_key(key));
                out.push(' ');
                write_value(child, indent, level + 1, out);
            }
            out.push('\n');
            push_indent(indent, level, out);
            out.push(')');
        }

        LinoValue::Array(items) => {
            if items.is_empty() {
                out.push_str("()");
                return;
            }

            out.push('(');
            for item in items {
                out.push('\n');
                push_indent(indent, level + 1, out);
                write_value(item, indent, level + 1, out);
            }
            out.push('\n');
            push_indent(indent, level, out);
            out.push(')');
        }

        scalar => out.push_str(&format_scalar(scalar)),
    }
}

/// Write a value on one line. Objects name themselves with the `o` link id and
/// write each pair as its own link, so nothing depends on where lines break.
fn write_line_value(value: &LinoValue, out: &mut String) {
    match value {
        LinoValue::Object(pairs) => {
            if pairs.is_empty() {
                // `()` is the empty array, so the empty object keeps its marker.
                out.push('(');
                out.push_str(OBJECT_MARKER);
                out.push_str(":)");
                return;
            }

            out.push('(');
            out.push_str(OBJECT_MARKER);
            out.push(':');
            for (key, child) in pairs {
                out.push_str(" (");
                out.push_str(&format_key(key));
                out.push(' ');
                write_line_value(child, out);
                out.push(')');
            }
            out.push(')');
        }

        LinoValue::Array(items) => {
            out.push('(');
            for (index, item) in items.iter().enumerate() {
                if index > 0 {
                    out.push(' ');
                }
                write_line_value(item, out);
            }
            out.push(')');
        }

        scalar => out.push_str(&format_scalar(scalar)),
    }
}

fn push_indent(indent: &str, level: usize, out: &mut String) {
    for _ in 0..level {
        out.push_str(indent);
    }
}

/// Format a scalar value. Strings are quoted, everything else stays bare so that
/// its type is recoverable when reading the document back.
fn format_scalar(value: &LinoValue) -> String {
    match value {
        LinoValue::Null => "null".to_string(),
        LinoValue::Bool(b) => b.to_string(),
        LinoValue::Int(i) => i.to_string(),
        LinoValue::Float(f) => format_float(*f),
        LinoValue::String(s) => format_string(s),
        // Containers are handled by write_value.
        LinoValue::Array(_) | LinoValue::Object(_) => String::new(),
    }
}

fn format_float(f: f64) -> String {
    if f.is_nan() {
        "NaN".to_string()
    } else if f.is_infinite() {
        if f.is_sign_positive() {
            "Infinity".to_string()
        } else {
            "-Infinity".to_string()
        }
    } else {
        // `{:?}` keeps the decimal point for whole floats (`1.0`), which is what
        // tells a float apart from an integer when reading the document back.
        format!("{:?}", f)
    }
}

/// Format a string value: quoted plain text, or an individually marked
/// base64 payload when the text cannot be written literally.
fn format_string(value: &str) -> String {
    if needs_encoding(value) {
        return format!(
            "({} {})",
            BASE64_MARKER,
            quote(&BASE64.encode(value.as_bytes()))
        );
    }
    quote(value)
}

/// A value can be written as text unless it contains control characters:
/// newlines break the line structure and CRLF normalisation would rewrite them.
fn needs_encoding(value: &str) -> bool {
    value.chars().any(char::is_control)
}

fn quote(value: &str) -> String {
    let has_double = value.contains('"');
    let has_single = value.contains('\'');

    if !has_double {
        return format!("\"{}\"", value);
    }
    if !has_single {
        return format!("'{}'", value);
    }
    // Both quote styles are present: double the double quotes, as the parser expects.
    format!("\"{}\"", value.replace('"', "\"\""))
}

/// Format an object key. Keys are bare when they read as plain identifiers.
fn format_key(key: &str) -> String {
    let plain = !key.is_empty()
        && key != BASE64_MARKER
        && !needs_encoding(key)
        && !key
            .chars()
            .any(|c| c.is_whitespace() || matches!(c, '(' | ')' | '\'' | '"' | ':' | '`'));

    if plain {
        key.to_string()
    } else {
        format_string(key)
    }
}

// === Decoding ===

#[derive(Debug, Clone, PartialEq, Eq)]
enum Token {
    Open,
    Close,
    Newline,
    Ref { value: String, quoted: bool },
}

/// A parsed element of the readable form: either a reference (remembering whether
/// it was quoted, which is what distinguishes a string from a number) or a link.
#[derive(Debug, Clone)]
enum Node {
    Ref {
        value: String,
        quoted: bool,
    },
    Link {
        rows: Vec<Vec<Node>>,
        multiline: bool,
        /// Whether the link named itself an object with the `o:` marker.
        object: bool,
    },
}

fn tokenize(text: &str) -> Result<Vec<Token>, CodecError> {
    let chars: Vec<char> = text.chars().collect();
    let mut tokens = Vec::new();
    let mut i = 0;

    while i < chars.len() {
        let c = chars[i];

        if c == '\n' {
            tokens.push(Token::Newline);
            i += 1;
        } else if c.is_whitespace() {
            i += 1;
        } else if c == '(' {
            tokens.push(Token::Open);
            i += 1;
        } else if c == ')' {
            tokens.push(Token::Close);
            i += 1;
        } else if matches!(c, '"' | '\'' | '`') {
            let (value, next) = read_quoted(&chars, i, c)?;
            tokens.push(Token::Ref {
                value,
                quoted: true,
            });
            i = next;
        } else {
            let start = i;
            while i < chars.len()
                && !chars[i].is_whitespace()
                && !matches!(chars[i], '(' | ')' | '"' | '\'' | '`')
            {
                i += 1;
            }
            tokens.push(Token::Ref {
                value: chars[start..i].iter().collect(),
                quoted: false,
            });
        }
    }

    Ok(tokens)
}

/// Read a quoted reference, where a doubled quote character means a literal one.
fn read_quoted(
    chars: &[char],
    start: usize,
    quote_char: char,
) -> Result<(String, usize), CodecError> {
    let mut value = String::new();
    let mut i = start + 1;

    while i < chars.len() {
        if chars[i] == quote_char {
            if chars.get(i + 1) == Some(&quote_char) {
                value.push(quote_char);
                i += 2;
                continue;
            }
            return Ok((value, i + 1));
        }
        value.push(chars[i]);
        i += 1;
    }

    Err(CodecError::ParseError(format!(
        "unterminated quoted value starting at character {}",
        start
    )))
}

struct Cursor {
    tokens: Vec<Token>,
    pos: usize,
}

impl Cursor {
    /// Parse rows until the matching `)` (or the end of input at the top level).
    /// A row is one line: the values written between two newlines.
    fn parse_rows(&mut self, top_level: bool) -> Result<Vec<Vec<Node>>, CodecError> {
        let mut rows: Vec<Vec<Node>> = Vec::new();
        let mut row: Vec<Node> = Vec::new();

        while self.pos < self.tokens.len() {
            match &self.tokens[self.pos] {
                Token::Close => {
                    if top_level {
                        break;
                    }
                    self.pos += 1;
                    if !row.is_empty() {
                        rows.push(row);
                    }
                    return Ok(rows);
                }
                Token::Newline => {
                    self.pos += 1;
                    if !row.is_empty() {
                        rows.push(std::mem::take(&mut row));
                    }
                }
                _ => row.push(self.parse_node()?),
            }
        }

        if !top_level {
            return Err(CodecError::ParseError(
                "unterminated '(' in readable notation".to_string(),
            ));
        }

        if !row.is_empty() {
            rows.push(row);
        }
        Ok(rows)
    }

    fn parse_node(&mut self) -> Result<Node, CodecError> {
        match self.tokens[self.pos].clone() {
            Token::Ref { value, quoted } => {
                self.pos += 1;
                Ok(Node::Ref { value, quoted })
            }
            Token::Open => {
                self.pos += 1;
                let object = self.take_object_marker();
                let multiline = self.link_is_multiline();
                let rows = self.parse_rows(false)?;
                Ok(Node::Link {
                    rows,
                    multiline,
                    object,
                })
            }
            Token::Close | Token::Newline => Err(CodecError::ParseError(
                "unexpected token in readable notation".to_string(),
            )),
        }
    }

    /// Consume the `o:` marker if the link that just opened carries one, which is
    /// how the single-line form says "this link is an object, not an array".
    fn take_object_marker(&mut self) -> bool {
        let marker = format!("{}:", OBJECT_MARKER);
        let is_marker = matches!(
            self.tokens.get(self.pos),
            Some(Token::Ref { value, quoted: false }) if *value == marker
        );
        if is_marker {
            self.pos += 1;
        }
        is_marker
    }

    /// Whether the link that just opened spans more than one line, which is what
    /// tells an empty object (`(\n)`) from an empty array (`()`).
    fn link_is_multiline(&self) -> bool {
        self.tokens[self.pos..]
            .iter()
            .take_while(|t| **t != Token::Close)
            .any(|t| *t == Token::Newline)
    }
}

fn node_to_value(node: &Node) -> Result<LinoValue, CodecError> {
    match node {
        Node::Ref { value, quoted } => Ok(ref_to_value(value, *quoted)),
        Node::Link {
            rows,
            multiline,
            object,
        } => rows_to_value(rows, *multiline, *object),
    }
}

fn rows_to_value(
    rows: &[Vec<Node>],
    multiline: bool,
    object_marker: bool,
) -> Result<LinoValue, CodecError> {
    if object_marker {
        return marked_object_to_value(rows);
    }

    if rows.is_empty() {
        return Ok(if multiline {
            LinoValue::Object(vec![])
        } else {
            LinoValue::Array(vec![])
        });
    }

    if let Some(marked) = decode_marked_value(rows) {
        return marked;
    }

    // Written on one line, a link is a list of values: an object on one line says
    // so with the `o:` marker, which is what keeps `(key value)` unambiguous.
    if !multiline {
        let mut items = Vec::new();
        for row in rows {
            for node in row {
                items.push(node_to_value(node)?);
            }
        }
        return Ok(LinoValue::Array(items));
    }

    // `key value` on every line makes an object; anything else is a list of values.
    let is_object = rows
        .iter()
        .all(|row| row.len() == 2 && matches!(row[0], Node::Ref { .. }));

    if is_object {
        let mut pairs = Vec::with_capacity(rows.len());
        for row in rows {
            let Node::Ref { value: key, .. } = &row[0] else {
                unreachable!("checked by is_object")
            };
            pairs.push((key.clone(), node_to_value(&row[1])?));
        }
        return Ok(LinoValue::Object(pairs));
    }

    let mut items = Vec::new();
    for row in rows {
        for node in row {
            items.push(node_to_value(node)?);
        }
    }
    Ok(LinoValue::Array(items))
}

/// Build the object a `(o: (key value) …)` link describes. Every value in it is
/// a pair, so anything else is a malformed document rather than a silent array.
fn marked_object_to_value(rows: &[Vec<Node>]) -> Result<LinoValue, CodecError> {
    let mut pairs = Vec::new();

    for node in rows.iter().flatten() {
        let Node::Link {
            rows: pair,
            object: false,
            ..
        } = node
        else {
            return Err(CodecError::ParseError(format!(
                "an object marked '{}:' holds (key value) pairs, found a value that is not a pair",
                OBJECT_MARKER
            )));
        };

        let [row] = pair.as_slice() else {
            return Err(CodecError::ParseError(format!(
                "an object marked '{}:' holds (key value) pairs, found a link of {} lines",
                OBJECT_MARKER,
                pair.len()
            )));
        };

        let [Node::Ref { value: key, .. }, value] = row.as_slice() else {
            return Err(CodecError::ParseError(format!(
                "an object marked '{}:' holds (key value) pairs, found a link of {} values",
                OBJECT_MARKER,
                row.len()
            )));
        };

        pairs.push((key.clone(), node_to_value(value)?));
    }

    Ok(LinoValue::Object(pairs))
}

/// Recognise `(base64 "…")`, the individual marker for values that could not be
/// written as text. A quoted `base64` key is an ordinary object key, not a marker.
fn decode_marked_value(rows: &[Vec<Node>]) -> Option<Result<LinoValue, CodecError>> {
    if rows.len() != 1 || rows[0].len() != 2 {
        return None;
    }

    let Node::Ref {
        value: marker,
        quoted: false,
    } = &rows[0][0]
    else {
        return None;
    };
    if marker != BASE64_MARKER {
        return None;
    }

    let Node::Ref {
        value: payload,
        quoted: true,
    } = &rows[0][1]
    else {
        return None;
    };

    Some(
        BASE64
            .decode(payload)
            .map_err(|e| CodecError::DecodeError(format!("invalid base64 value: {}", e)))
            .and_then(|bytes| {
                String::from_utf8(bytes)
                    .map(LinoValue::String)
                    .map_err(|e| CodecError::DecodeError(format!("invalid UTF-8 value: {}", e)))
            }),
    )
}

/// Convert a reference to a value. Quoted references are always strings; bare
/// references keep the type they were written with.
fn ref_to_value(value: &str, quoted: bool) -> LinoValue {
    if quoted {
        return LinoValue::String(value.to_string());
    }

    match value {
        "null" => return LinoValue::Null,
        "true" => return LinoValue::Bool(true),
        "false" => return LinoValue::Bool(false),
        "NaN" => return LinoValue::Float(f64::NAN),
        "Infinity" => return LinoValue::Float(f64::INFINITY),
        "-Infinity" => return LinoValue::Float(f64::NEG_INFINITY),
        _ => {}
    }

    if let Ok(i) = value.parse::<i64>() {
        return LinoValue::Int(i);
    }
    if value.contains(['.', 'e', 'E']) {
        if let Ok(f) = value.parse::<f64>() {
            return LinoValue::Float(f);
        }
    }

    LinoValue::String(value.to_string())
}

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

    fn roundtrip(value: &LinoValue) -> LinoValue {
        let text = encode(value, DEFAULT_INDENT);
        decode(&text).unwrap_or_else(|e| panic!("failed to decode {:?}: {}", text, e))
    }

    #[test]
    fn empty_containers_keep_their_type() {
        assert_eq!(encode(&LinoValue::Array(vec![]), DEFAULT_INDENT), "()");
        assert_eq!(encode(&LinoValue::Object(vec![]), DEFAULT_INDENT), "(\n)");
        assert_eq!(
            roundtrip(&LinoValue::Array(vec![])),
            LinoValue::Array(vec![])
        );
        assert_eq!(
            roundtrip(&LinoValue::Object(vec![])),
            LinoValue::Object(vec![])
        );
    }

    #[test]
    fn single_pair_object_is_not_an_array() {
        let value = LinoValue::object([("a", LinoValue::Int(1))]);
        assert_eq!(encode(&value, DEFAULT_INDENT), "(\n  a 1\n)");
        assert_eq!(roundtrip(&value), value);
    }

    #[test]
    fn numeric_looking_strings_stay_strings() {
        let value = LinoValue::object([
            ("count", LinoValue::Int(42)),
            ("zip", LinoValue::String("10001".to_string())),
            ("flag", LinoValue::String("true".to_string())),
        ]);
        assert_eq!(roundtrip(&value), value);
    }

    #[test]
    fn whole_floats_stay_floats() {
        let value = LinoValue::Float(1.0);
        assert_eq!(encode(&value, DEFAULT_INDENT), "1.0");
        assert!(matches!(roundtrip(&value), LinoValue::Float(f) if (f - 1.0).abs() < f64::EPSILON));
    }

    #[test]
    fn base64_key_is_quoted_so_it_is_not_a_marker() {
        let value = LinoValue::object([("base64", LinoValue::String("plain".to_string()))]);
        assert_eq!(
            encode(&value, DEFAULT_INDENT),
            "(\n  \"base64\" \"plain\"\n)"
        );
        assert_eq!(roundtrip(&value), value);
    }

    #[test]
    fn control_characters_are_marked_individually() {
        let value = LinoValue::object([
            ("plain", LinoValue::String("visible".to_string())),
            ("raw", LinoValue::String("line1\nline2".to_string())),
        ]);
        let text = encode(&value, DEFAULT_INDENT);
        assert!(text.contains("plain \"visible\""), "{}", text);
        assert!(
            text.contains("raw (base64 \"bGluZTEKbGluZTI=\")"),
            "{}",
            text
        );
        assert_eq!(roundtrip(&value), value);
    }

    #[test]
    fn custom_indent_is_used() {
        let value = LinoValue::object([("a", LinoValue::Int(1))]);
        assert_eq!(encode(&value, "    "), "(\n    a 1\n)");
    }

    #[test]
    fn handwritten_document_without_root_parentheses_decodes() {
        let decoded = decode("a 1\nb \"two\"").unwrap();
        assert_eq!(
            decoded,
            LinoValue::object([
                ("a", LinoValue::Int(1)),
                ("b", LinoValue::String("two".into()))
            ])
        );
    }

    fn line_roundtrip(value: &LinoValue) -> LinoValue {
        let text = encode_line(value);
        assert!(
            !text.contains('\n'),
            "line form must hold no newline: {:?}",
            text
        );
        decode_line(&text).unwrap_or_else(|e| panic!("failed to decode {:?}: {}", text, e))
    }

    #[test]
    fn line_form_marks_objects_and_leaves_arrays_bare() {
        assert_eq!(encode_line(&LinoValue::Array(vec![])), "()");
        assert_eq!(encode_line(&LinoValue::Object(vec![])), "(o:)");
        assert_eq!(
            encode_line(&LinoValue::object([("a", LinoValue::Int(1))])),
            "(o: (a 1))"
        );
        assert_eq!(
            encode_line(&LinoValue::array([
                LinoValue::String("key".into()),
                LinoValue::String("value".into()),
            ])),
            "(\"key\" \"value\")"
        );
    }

    #[test]
    fn line_form_tells_a_one_pair_object_from_a_two_element_array() {
        let object = LinoValue::object([("key", LinoValue::String("value".into()))]);
        let array = LinoValue::array([
            LinoValue::String("key".into()),
            LinoValue::String("value".into()),
        ]);
        assert_ne!(encode_line(&object), encode_line(&array));
        assert_eq!(line_roundtrip(&object), object);
        assert_eq!(line_roundtrip(&array), array);
    }

    #[test]
    fn line_form_roundtrips_nested_and_empty_containers() {
        let value = LinoValue::object([
            ("empty_array", LinoValue::Array(vec![])),
            ("empty_object", LinoValue::Object(vec![])),
            (
                "records",
                LinoValue::array([
                    LinoValue::object([("id", LinoValue::Int(1))]),
                    LinoValue::object([("id", LinoValue::Int(2))]),
                ]),
            ),
        ]);
        assert_eq!(
            encode_line(&value),
            "(o: (empty_array ()) (empty_object (o:)) (records ((o: (id 1)) (o: (id 2)))))"
        );
        assert_eq!(line_roundtrip(&value), value);
    }

    #[test]
    fn line_form_keeps_the_empty_key() {
        let value = LinoValue::object([("", LinoValue::Int(2))]);
        assert_eq!(encode_line(&value), "(o: (\"\" 2))");
        assert_eq!(line_roundtrip(&value), value);
    }

    #[test]
    fn line_form_keeps_strings_on_one_line() {
        let value = LinoValue::object([
            (
                "quotes",
                LinoValue::String("both \"kinds\" of 'quotes'".into()),
            ),
            ("unicode", LinoValue::String("héllo 世界 🌍".into())),
            ("multiline", LinoValue::String("line1\nline2".into())),
        ]);
        let text = encode_line(&value);
        assert!(!text.contains('\n'), "{}", text);
        assert_eq!(line_roundtrip(&value), value);
    }

    #[test]
    fn a_line_form_object_holds_pairs_only() {
        assert!(decode_line("(o: 1 2)").is_err());
        assert!(decode_line("(o: (a 1 2))").is_err());
        assert!(decode_line("(o: (a))").is_err());
    }

    #[test]
    fn decode_line_rejects_a_document_of_several_lines() {
        assert!(decode_line("(o: (a 1))\n(o: (a 2))").is_err());
        // A trailing line break is what a line-based reader hands over, so it is
        // stripped rather than rejected.
        assert_eq!(
            decode_line("(o: (a 1))\n").unwrap(),
            LinoValue::object([("a", LinoValue::Int(1))])
        );
    }

    #[test]
    fn unterminated_input_is_an_error() {
        assert!(decode("(\n  a 1\n").is_err());
        assert!(decode("(\n  a \"unterminated\n").is_err());
        assert!(decode("a 1)").is_err());
    }
}