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
//! Conversion between JSON text and techy's serialized values and wire
//! segments.
//!
//! A techy serialized value ([`SerialValue`]) has at most one JSON form (a value
//! holding a map key that begins with `$` has none), and two values have the same
//! JSON text exactly when they are equal. Comparing two
//! [projections](crate::dump::project) as values and comparing their JSON text
//! therefore always agree, so a [golden file](crate::dump::golden) can hold a
//! projection as readable JSON that a person may edit, without weakening the
//! comparison.
//!
//! A wire segment ([`Segment`]) is one unit of techy's serialized stream, as a
//! [serialization session](crate::dump::session) emits it. Its JSON form is
//! written on one line, so that a stream is one segment per line; `flm dump
//! --view wire` prints it that way.
//!
//! [`from_serde`] converts any value `serde` can serialize, such as a
//! `serde_json::json!` literal, into a serialized value, which is how a test
//! writes an expected value.
use Serialize;
use ;
/// The error type of the JSON functions of this module: `serde_json`'s error.
///
/// Writing JSON fails for a value holding a map key that begins with `$`, a
/// prefix techy reserves; reading JSON fails for text that is not the JSON form
/// of a serialized value.
pub type JsonError = Error;
/// Writes a serialized value as indented JSON: two spaces per level, no
/// trailing newline.
///
/// This is the form a JSON golden file holds (followed by a newline) and the
/// form a failing comparison prints.
///
/// # Errors
///
/// [`JsonError`] if the value holds a map key beginning with `$`.
///
/// # Examples
///
/// ```
/// use flm_core::dump::to_json_pretty;
/// use flm_core::techy::serialize::SerialValue;
///
/// let value = SerialValue::List(vec![SerialValue::Int(1), SerialValue::Str("a".into())]);
/// assert_eq!(to_json_pretty(&value)?, "[\n 1,\n \"a\"\n]");
/// # Ok::<(), flm_core::dump::JsonError>(())
/// ```
/// Writes a serialized value as JSON on one line, with no space between the
/// tokens.
///
/// # Errors
///
/// [`JsonError`] if the value holds a map key beginning with `$`.
/// Reads a serialized value from JSON text.
///
/// # Errors
///
/// [`JsonError`] if the text is not JSON; if it holds a floating-point number
/// or an integer outside the range of `i64` (serialized values have neither);
/// if it holds an object key beginning with `$` other than the two keys techy
/// reserves for bytes and table references (`$bytes`, `$index`); or if it nests more lists and
/// maps than [`SerialValue::MAX_NESTING_DEPTH`] allows.
/// Converts any value that `serde` can serialize into a techy serialized value.
///
/// This is how a test's `serde_json::json!({ ... })` literal becomes the
/// expected value of a comparison.
///
/// # Errors
///
/// [`SerialValueError`] if the value holds a floating-point number or an integer
/// outside the range of `i64`, which serialized values cannot represent; if it
/// holds a map key that is not a string or that begins with `$` (struct field
/// names and the names of enum variants with data count as map keys); or if its
/// own `Serialize` implementation reports an error.
Sized>
/// Writes a wire segment as one line of JSON, with no line break inside it.
///
/// A stream written this way holds one segment per line; `flm dump --view
/// wire` prints this form. [`segment_from_line`] reads it back.
///
/// # Errors
///
/// [`JsonError`] if the segment holds a map key beginning with `$`.
/// Reads a wire segment from one line of JSON written by [`segment_line`].
///
/// # Errors
///
/// [`JsonError`] if the line is not a JSON rendering of a segment.