Skip to main content

flm_core/dump/
json.rs

1//! Conversion between JSON text and techy's serialized values and wire
2//! segments.
3//!
4//! A techy serialized value ([`SerialValue`]) has at most one JSON form (a value
5//! holding a map key that begins with `$` has none), and two values have the same
6//! JSON text exactly when they are equal. Comparing two
7//! [projections](crate::dump::project) as values and comparing their JSON text
8//! therefore always agree, so a [golden file](crate::dump::golden) can hold a
9//! projection as readable JSON that a person may edit, without weakening the
10//! comparison.
11//!
12//! A wire segment ([`Segment`]) is one unit of techy's serialized stream, as a
13//! [serialization session](crate::dump::session) emits it. Its JSON form is
14//! written on one line, so that a stream is one segment per line; `flm dump
15//! --view wire` prints it that way.
16//!
17//! [`from_serde`] converts any value `serde` can serialize, such as a
18//! `serde_json::json!` literal, into a serialized value, which is how a test
19//! writes an expected value.
20
21use serde::Serialize;
22use techy::serialize::{Segment, SerialValue, SerialValueError};
23
24/// The error type of the JSON functions of this module: `serde_json`'s error.
25///
26/// Writing JSON fails for a value holding a map key that begins with `$`, a
27/// prefix techy reserves; reading JSON fails for text that is not the JSON form
28/// of a serialized value.
29pub type JsonError = serde_json::Error;
30
31/// Writes a serialized value as indented JSON: two spaces per level, no
32/// trailing newline.
33///
34/// This is the form a JSON golden file holds (followed by a newline) and the
35/// form a failing comparison prints.
36///
37/// # Errors
38///
39/// [`JsonError`] if the value holds a map key beginning with `$`.
40///
41/// # Examples
42///
43/// ```
44/// use flm_core::dump::to_json_pretty;
45/// use flm_core::techy::serialize::SerialValue;
46///
47/// let value = SerialValue::List(vec![SerialValue::Int(1), SerialValue::Str("a".into())]);
48/// assert_eq!(to_json_pretty(&value)?, "[\n  1,\n  \"a\"\n]");
49/// # Ok::<(), flm_core::dump::JsonError>(())
50/// ```
51pub fn to_json_pretty(value: &SerialValue) -> Result<String, JsonError> {
52    serde_json::to_string_pretty(value)
53}
54
55/// Writes a serialized value as JSON on one line, with no space between the
56/// tokens.
57///
58/// # Errors
59///
60/// [`JsonError`] if the value holds a map key beginning with `$`.
61pub fn to_json(value: &SerialValue) -> Result<String, JsonError> {
62    serde_json::to_string(value)
63}
64
65/// Reads a serialized value from JSON text.
66///
67/// # Errors
68///
69/// [`JsonError`] if the text is not JSON; if it holds a floating-point number
70/// or an integer outside the range of `i64` (serialized values have neither);
71/// if it holds an object key beginning with `$` other than the two keys techy
72/// reserves for bytes and table references (`$bytes`, `$index`); or if it nests more lists and
73/// maps than [`SerialValue::MAX_NESTING_DEPTH`] allows.
74pub fn from_json(text: &str) -> Result<SerialValue, JsonError> {
75    serde_json::from_str(text)
76}
77
78/// Converts any value that `serde` can serialize into a techy serialized value.
79///
80/// This is how a test's `serde_json::json!({ ... })` literal becomes the
81/// expected value of a comparison.
82///
83/// # Errors
84///
85/// [`SerialValueError`] if the value holds a floating-point number or an integer
86/// outside the range of `i64`, which serialized values cannot represent; if it
87/// holds a map key that is not a string or that begins with `$` (struct field
88/// names and the names of enum variants with data count as map keys); or if its
89/// own `Serialize` implementation reports an error.
90pub fn from_serde<T: Serialize + ?Sized>(value: &T) -> Result<SerialValue, SerialValueError> {
91    techy::serialize::to_value(value)
92}
93
94/// Writes a wire segment as one line of JSON, with no line break inside it.
95///
96/// A stream written this way holds one segment per line; `flm dump --view
97/// wire` prints this form. [`segment_from_line`] reads it back.
98///
99/// # Errors
100///
101/// [`JsonError`] if the segment holds a map key beginning with `$`.
102pub fn segment_line(segment: &Segment) -> Result<String, JsonError> {
103    serde_json::to_string(segment)
104}
105
106/// Reads a wire segment from one line of JSON written by [`segment_line`].
107///
108/// # Errors
109///
110/// [`JsonError`] if the line is not a JSON rendering of a segment.
111pub fn segment_from_line(line: &str) -> Result<Segment, JsonError> {
112    serde_json::from_str(line)
113}