edikt-core 0.4.0

edikt core: value model, Feature capabilities, the expression language, and the Document/Convert seams.
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
//! The value calculus domain.
//!
//! `Value` is the in-memory model the expression evaluator and format conversion
//! operate over. It is *not* how format-preserving edits round-trip (those stay
//! in the CST); it is the domain for computed values (`.count + 1`), query
//! output, and cross-format conversion.
//!
//! Numbers keep an `Int`/`Float` distinction so integer output stays clean.
//! Objects are insertion-ordered (`Vec` of pairs) because key order is
//! user-visible.

use std::cmp::Ordering;

#[derive(Debug, Clone)]
pub enum Value {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    Str(String),
    Array(Vec<Value>),
    Object(Vec<(String, Value)>),
}

// Equality follows the total order, so `Int(1) == Float(1.0)`. This lets `Expr`
// (which embeds `Value` in literals) derive `PartialEq` for tests.
impl PartialEq for Value {
    fn eq(&self, other: &Value) -> bool {
        self.value_eq(other)
    }
}

impl Value {
    /// jq-style truthiness: only `false` and `null` are falsy.
    pub fn is_truthy(&self) -> bool {
        !matches!(self, Value::Null | Value::Bool(false))
    }

    /// The type name, as reported by the `type` builtin.
    pub fn type_name(&self) -> &'static str {
        match self {
            Value::Null => "null",
            Value::Bool(_) => "boolean",
            Value::Int(_) | Value::Float(_) => "number",
            Value::Str(_) => "string",
            Value::Array(_) => "array",
            Value::Object(_) => "object",
        }
    }

    /// Numeric coercion to f64 for arithmetic and comparison.
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            Value::Int(i) => Some(*i as f64),
            Value::Float(f) => Some(*f),
            _ => None,
        }
    }

    /// Total order across types (jq's ordering:
    /// null < bool < number < string < array < object).
    pub fn order(&self, other: &Value) -> Ordering {
        fn rank(v: &Value) -> u8 {
            match v {
                Value::Null => 0,
                Value::Bool(_) => 1,
                Value::Int(_) | Value::Float(_) => 2,
                Value::Str(_) => 3,
                Value::Array(_) => 4,
                Value::Object(_) => 5,
            }
        }
        match (self, other) {
            (Value::Null, Value::Null) => Ordering::Equal,
            (Value::Bool(a), Value::Bool(b)) => a.cmp(b),
            (Value::Str(a), Value::Str(b)) => a.cmp(b),
            (Value::Array(a), Value::Array(b)) => {
                for (x, y) in a.iter().zip(b.iter()) {
                    let c = x.order(y);
                    if c != Ordering::Equal {
                        return c;
                    }
                }
                a.len().cmp(&b.len())
            }
            (Value::Object(a), Value::Object(b)) => {
                // Compare by sorted keys, then by the values at those keys.
                let mut ka: Vec<&String> = a.iter().map(|(k, _)| k).collect();
                let mut kb: Vec<&String> = b.iter().map(|(k, _)| k).collect();
                ka.sort();
                kb.sort();
                match ka.cmp(&kb) {
                    Ordering::Equal => {}
                    ne => return ne,
                }
                for k in ka {
                    let va = a.iter().find(|(kk, _)| kk == k).map(|(_, v)| v);
                    let vb = b.iter().find(|(kk, _)| kk == k).map(|(_, v)| v);
                    if let (Some(va), Some(vb)) = (va, vb) {
                        let c = va.order(vb);
                        if c != Ordering::Equal {
                            return c;
                        }
                    }
                }
                Ordering::Equal
            }
            _ => {
                // Numbers compare numerically; otherwise fall back to type rank.
                if let (Some(a), Some(b)) = (self.as_f64(), other.as_f64()) {
                    a.partial_cmp(&b).unwrap_or(Ordering::Equal)
                } else {
                    rank(self).cmp(&rank(other))
                }
            }
        }
    }

    /// Structural equality consistent with [`Value::order`].
    pub fn value_eq(&self, other: &Value) -> bool {
        self.order(other) == Ordering::Equal
    }

    /// Render as a raw scalar for query output (no surrounding quotes on
    /// strings). Composite values are rendered as JSON.
    pub fn to_raw_string(&self) -> String {
        match self {
            Value::Str(s) => s.clone(),
            Value::Null => "null".to_string(),
            Value::Bool(b) => b.to_string(),
            Value::Int(i) => i.to_string(),
            Value::Float(f) => format_f64(*f),
            Value::Array(_) | Value::Object(_) => self.to_json(),
        }
    }

    /// Render as compact JSON.
    pub fn to_json(&self) -> String {
        let mut out = String::new();
        self.write_json(&mut out);
        out
    }

    fn write_json(&self, out: &mut String) {
        match self {
            Value::Null => out.push_str("null"),
            Value::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
            Value::Int(i) => out.push_str(&i.to_string()),
            // JSON's grammar has no non-finite literal, so encoding one emits
            // `null`, matching `JSON.stringify`. Only a JSON5 source can supply
            // one, and only a conversion away from the JSON5 family reaches
            // this; an in-place JSON5 edit never re-encodes untouched bytes.
            Value::Float(f) if !f.is_finite() => out.push_str("null"),
            Value::Float(f) => out.push_str(&format_f64(*f)),
            Value::Str(s) => write_json_string(s, out),
            Value::Array(a) => {
                out.push('[');
                for (i, v) in a.iter().enumerate() {
                    if i > 0 {
                        out.push(',');
                    }
                    v.write_json(out);
                }
                out.push(']');
            }
            Value::Object(m) => {
                out.push('{');
                for (i, (k, v)) in m.iter().enumerate() {
                    if i > 0 {
                        out.push(',');
                    }
                    write_json_string(k, out);
                    out.push(':');
                    v.write_json(out);
                }
                out.push('}');
            }
        }
    }
}

impl From<bool> for Value {
    fn from(input: bool) -> Self {
        Self::Bool(input)
    }
}

macro_rules! from_int {
    ($($t:ty),+) => {$(
        impl From<$t> for Value {
            fn from(input: $t) -> Self {
                Self::Int(input as i64)
            }
        }
    )+};
}
// u64/usize are absent on purpose: they do not fit i64 without a lossy cast,
// and silently wrapping a large count into a negative Int is worse than making
// the caller choose.
from_int!(i8, i16, i32, i64, u8, u16, u32, isize);

macro_rules! from_float {
    ($($t:ty),+) => {$(
        impl From<$t> for Value {
            fn from(input: $t) -> Self {
                Self::Float(input as f64)
            }
        }
    )+};
}
from_float!(f32, f64);

/// `None` is JSON's `null`; `Some(v)` is whatever `v` converts to.
impl<T: Into<Value>> From<Option<T>> for Value {
    fn from(input: Option<T>) -> Self {
        input.map_or(Self::Null, Into::into)
    }
}

impl<T: Into<Value>> From<Vec<T>> for Value {
    fn from(input: Vec<T>) -> Self {
        Self::Array(input.into_iter().map(Into::into).collect())
    }
}

impl<T: Into<Value> + Clone> From<&[T]> for Value {
    fn from(input: &[T]) -> Self {
        Self::Array(input.iter().cloned().map(Into::into).collect())
    }
}

impl<K: Into<String>, V: Into<Value>> FromIterator<(K, V)> for Value {
    /// Collect key-value pairs into an object, preserving iteration order.
    fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
        Self::Object(
            iter.into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect(),
        )
    }
}

impl From<&str> for Value {
    fn from(input: &str) -> Self {
        Self::Str(input.to_string())
    }
}

impl From<String> for Value {
    fn from(input: String) -> Self {
        Self::Str(input)
    }
}

/// Format an f64 the way jq does: integral values print without a decimal point.
///
/// Non-finite values use the JavaScript/JSON5 spelling (`Infinity`, `-Infinity`,
/// `NaN`) rather than Rust's `inf`/`NaN`, so a JSON5 document's own literals
/// round-trip through raw output. They are only reachable from a JSON5 source;
/// arithmetic cannot produce one (division by zero is a hard error).
fn format_f64(f: f64) -> String {
    if f.is_nan() {
        return "NaN".to_string();
    }
    if f.is_infinite() {
        return if f > 0.0 { "Infinity" } else { "-Infinity" }.to_string();
    }
    if f.fract() == 0.0 && f.abs() < 1e15 {
        format!("{}", f as i64)
    } else {
        format!("{f}")
    }
}

fn write_json_string(s: &str, out: &mut String) {
    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"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
}

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

    fn arr(v: &[Value]) -> Value {
        Value::Array(v.to_vec())
    }
    fn obj(pairs: &[(&str, Value)]) -> Value {
        Value::Object(
            pairs
                .iter()
                .map(|(k, v)| (k.to_string(), v.clone()))
                .collect(),
        )
    }

    #[test]
    fn total_order_across_types() {
        // jq's ordering: null < bool < number < string < array < object.
        let ladder = [
            Value::Null,
            Value::Bool(false),
            Value::Bool(true),
            Value::Int(-1),
            Value::Float(2.5),
            Value::Str("a".into()),
            arr(&[Value::Int(1)]),
            obj(&[("k", Value::Int(1))]),
        ];
        for i in 0..ladder.len() {
            for j in 0..ladder.len() {
                let want = i.cmp(&j);
                // Equal ranks (the two bools, two numbers) compare by value, so
                // only assert the strict cross-rank orderings here.
                if want != Ordering::Equal {
                    assert_eq!(
                        ladder[i].order(&ladder[j]),
                        want,
                        "{:?} vs {:?}",
                        ladder[i],
                        ladder[j]
                    );
                }
            }
        }
    }

    #[test]
    fn numbers_compare_across_int_and_float() {
        assert!(Value::Int(1).value_eq(&Value::Float(1.0)));
        assert_eq!(Value::Int(2).order(&Value::Float(2.5)), Ordering::Less);
        assert_eq!(Value::Float(3.0).order(&Value::Int(3)), Ordering::Equal);
        assert_eq!(Value::Int(1).as_f64(), Some(1.0));
        assert_eq!(Value::Str("x".into()).as_f64(), None);
    }

    #[test]
    fn arrays_and_objects_order_structurally() {
        // Arrays compare element-wise, then by length.
        assert_eq!(
            arr(&[Value::Int(1), Value::Int(2)]).order(&arr(&[Value::Int(1), Value::Int(3)])),
            Ordering::Less
        );
        assert_eq!(
            arr(&[Value::Int(1)]).order(&arr(&[Value::Int(1), Value::Int(0)])),
            Ordering::Less
        );
        // Objects compare by sorted keys, then values at those keys.
        assert_eq!(
            obj(&[("a", Value::Int(1))]).order(&obj(&[("b", Value::Int(1))])),
            Ordering::Less
        );
        assert_eq!(
            obj(&[("a", Value::Int(1))]).order(&obj(&[("a", Value::Int(2))])),
            Ordering::Less
        );
        // Key order doesn't affect equality.
        assert!(
            obj(&[("a", Value::Int(1)), ("b", Value::Int(2))])
                .value_eq(&obj(&[("b", Value::Int(2)), ("a", Value::Int(1))]))
        );
    }

    #[test]
    fn non_finite_floats_use_the_json5_spelling_but_encode_as_json_null() {
        // Raw output round-trips a JSON5 literal...
        assert_eq!(Value::Float(f64::INFINITY).to_raw_string(), "Infinity");
        assert_eq!(Value::Float(f64::NEG_INFINITY).to_raw_string(), "-Infinity");
        assert_eq!(Value::Float(f64::NAN).to_raw_string(), "NaN");
        // ...while JSON encoding degrades to null, because `Infinity` is not
        // JSON and emitting it would produce a document nothing can parse.
        assert_eq!(Value::Float(f64::INFINITY).to_json(), "null");
        assert_eq!(Value::Float(f64::NAN).to_json(), "null");
        assert_eq!(
            obj(&[("a", Value::Float(f64::NEG_INFINITY))]).to_json(),
            "{\"a\":null}"
        );
        // Finite formatting is unchanged.
        assert_eq!(Value::Float(1e14).to_json(), "100000000000000");
    }

    #[test]
    fn raw_string_and_truthiness() {
        assert_eq!(Value::Float(1.0).to_raw_string(), "1");
        assert_eq!(Value::Float(1.5).to_raw_string(), "1.5");
        assert_eq!(Value::Null.to_raw_string(), "null");
        assert_eq!(Value::Bool(true).to_raw_string(), "true");
        assert_eq!(arr(&[Value::Int(1)]).to_raw_string(), "[1]");
        assert_eq!(obj(&[("a", Value::Int(1))]).to_raw_string(), "{\"a\":1}");
        // Only false and null are falsy (jq).
        assert!(Value::Int(0).is_truthy());
        assert!(Value::Str("".into()).is_truthy());
        assert!(!Value::Bool(false).is_truthy());
        assert!(!Value::Null.is_truthy());
    }

    #[test]
    fn json_encoding_escapes_and_floats() {
        assert_eq!(
            Value::Str("a\"b\\c\n\t\r".into()).to_json(),
            "\"a\\\"b\\\\c\\n\\t\\r\""
        );
        // A control char below 0x20 becomes a \u escape.
        assert_eq!(Value::Str("\u{0001}".into()).to_json(), "\"\\u0001\"");
        // Integral floats print without a decimal point; fractional keep it.
        assert_eq!(Value::Float(42.0).to_json(), "42");
        assert_eq!(Value::Float(2.5).to_json(), "2.5");
        assert_eq!(
            obj(&[("n", Value::Null), ("xs", arr(&[Value::Bool(true)]))]).to_json(),
            "{\"n\":null,\"xs\":[true]}"
        );
        assert_eq!(arr(&[]).to_json(), "[]");
    }

    #[test]
    fn type_names() {
        assert_eq!(Value::Null.type_name(), "null");
        assert_eq!(Value::Bool(true).type_name(), "boolean");
        assert_eq!(Value::Int(1).type_name(), "number");
        assert_eq!(Value::Float(1.0).type_name(), "number");
        assert_eq!(Value::Str("x".into()).type_name(), "string");
        assert_eq!(arr(&[]).type_name(), "array");
        assert_eq!(obj(&[]).type_name(), "object");
    }
}