edikt-core 0.5.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
//! The builtin function registry (the value calculus' jq-named functions).
//!
//! One `eval_call` dispatches over the whole registry - `length`/`keys`/`has`,
//! the string family (`split`/`join`, affix predicates, case mapping), the
//! regex family (`test`/`match`/`capture`/`sub`/`gsub`), `tonumber`/`tostring`,
//! and `select`. Grows deliberately, never speculatively: a new builtin lands
//! here as one `match` arm plus (if non-trivial) a helper in this file.
//!
//! `del` is handled at the value level right here too (it is a function in the
//! language): it defines what value `del(path)` produces.

use crate::ast::{Expr, Step};
use crate::eval::{EvalError, eval};
use crate::strings;
use crate::value::Value;
pub(crate) fn eval_call(name: &str, args: &[Expr], input: &Value) -> Result<Vec<Value>, EvalError> {
    let arity = |n: usize| -> Result<(), EvalError> {
        if args.len() == n {
            Ok(())
        } else {
            Err(EvalError::new(format!(
                "{name} takes {n} argument(s), got {}",
                args.len()
            )))
        }
    };
    // For builtins with a trailing optional argument (regex flags).
    let arity_between = |min: usize, max: usize| -> Result<(), EvalError> {
        if (min..=max).contains(&args.len()) {
            Ok(())
        } else {
            Err(EvalError::new(format!(
                "{name} takes {min}-{max} arguments, got {}",
                args.len()
            )))
        }
    };
    // The optional flags argument, defaulting to none.
    let flags_arg = |at: usize| -> Result<String, EvalError> {
        match args.get(at) {
            Some(a) => str_arg(a, input, "flags"),
            None => Ok(String::new()),
        }
    };

    match name {
        "select" => {
            arity(1)?;
            let mut out = Vec::new();
            for cond in eval(&args[0], input)? {
                if cond.is_truthy() {
                    out.push(input.clone());
                }
            }
            Ok(out)
        }
        "length" => {
            arity(0)?;
            Ok(vec![length(input)?])
        }
        "keys" => {
            arity(0)?;
            Ok(vec![keys(input)?])
        }
        "type" => {
            arity(0)?;
            Ok(vec![Value::Str(input.type_name().to_string())])
        }
        "tostring" => {
            arity(0)?;
            Ok(vec![Value::Str(input.to_raw_string())])
        }
        "tonumber" => {
            arity(0)?;
            Ok(vec![tonumber(input)?])
        }
        "ascii_upcase" => {
            arity(0)?;
            Ok(vec![map_str(input, |s| s.to_uppercase())?])
        }
        "ascii_downcase" => {
            arity(0)?;
            Ok(vec![map_str(input, |s| s.to_lowercase())?])
        }
        "has" => {
            arity(1)?;
            let mut out = Vec::new();
            for key in eval(&args[0], input)? {
                out.push(Value::Bool(has(input, &key)?));
            }
            Ok(out)
        }
        "ltrimstr" => {
            arity(1)?;
            trim_str(input, &args[0], true)
        }
        "rtrimstr" => {
            arity(1)?;
            trim_str(input, &args[0], false)
        }
        "startswith" | "endswith" => {
            arity(1)?;
            let s = str_input(input, name)?;
            let affix = str_arg(&args[0], input, "the affix")?;
            let hit = if name == "startswith" {
                s.starts_with(&affix)
            } else {
                s.ends_with(&affix)
            };
            Ok(vec![Value::Bool(hit)])
        }
        "test" => {
            arity_between(1, 2)?;
            let re = str_arg(&args[0], input, "the regex")?;
            Ok(vec![strings::test(
                str_input(input, name)?,
                &re,
                &flags_arg(1)?,
            )?])
        }
        "match" => {
            arity_between(1, 2)?;
            let re = str_arg(&args[0], input, "the regex")?;
            strings::find(str_input(input, name)?, &re, &flags_arg(1)?)
        }
        "capture" => {
            arity_between(1, 2)?;
            let re = str_arg(&args[0], input, "the regex")?;
            strings::capture(str_input(input, name)?, &re, &flags_arg(1)?)
        }
        "sub" | "gsub" => {
            arity_between(2, 3)?;
            let re = str_arg(&args[0], input, "the regex")?;
            let repl = str_arg(&args[1], input, "the replacement")?;
            let mut flags = flags_arg(2)?;
            if name == "gsub" {
                flags.push('g');
            }
            Ok(vec![strings::sub(
                str_input(input, name)?,
                &re,
                &repl,
                &flags,
            )?])
        }
        "split" => {
            arity_between(1, 2)?;
            let sep = str_arg(&args[0], input, "the separator")?;
            // jq's shape: 1-arg splits on a literal, 2-arg on a regex.
            let regex_flags = if args.len() == 2 {
                Some(flags_arg(1)?)
            } else {
                None
            };
            Ok(vec![strings::split(
                str_input(input, name)?,
                &sep,
                regex_flags.as_deref(),
            )?])
        }
        "join" => {
            arity(1)?;
            let Value::Array(items) = input else {
                return Err(EvalError::new(format!(
                    "join requires an array input, got {}",
                    input.type_name()
                )));
            };
            let sep = str_arg(&args[0], input, "the separator")?;
            Ok(vec![strings::join(items, &sep)?])
        }
        "del" => {
            arity(1)?;
            let steps = args[0]
                .as_path()
                .ok_or_else(|| EvalError::new("del(...) takes a path"))?;
            Ok(vec![delete_path(input, steps)?])
        }
        _ => Err(EvalError::new(format!("unknown function `{name}`"))),
    }
}

/// Return a copy of `v` with the value at `steps` removed. A missing key or
/// out-of-range index is a no-op (jq semantics).
fn delete_path(v: &Value, steps: &[Step]) -> Result<Value, EvalError> {
    let Some((head, rest)) = steps.split_first() else {
        return Err(EvalError::new("del(.) is not allowed"));
    };
    if rest.is_empty() {
        return remove_step(v, head);
    }
    match head {
        Step::Field(k) => {
            let mut obj = match v {
                Value::Object(m) => m.clone(),
                Value::Null => return Ok(Value::Null),
                other => {
                    return Err(EvalError::new(format!(
                        "cannot descend into {}",
                        other.type_name()
                    )));
                }
            };
            if let Some(pair) = obj.iter_mut().find(|(kk, _)| kk == k) {
                pair.1 = delete_path(&pair.1, rest)?;
            }
            Ok(Value::Object(obj))
        }
        Step::Index(i) => {
            let mut arr = match v {
                Value::Array(a) => a.clone(),
                Value::Null => return Ok(Value::Null),
                other => {
                    return Err(EvalError::new(format!(
                        "cannot index {} with a number",
                        other.type_name()
                    )));
                }
            };
            let idx = if *i < 0 { arr.len() as i64 + i } else { *i };
            if idx >= 0 && (idx as usize) < arr.len() {
                let idx = idx as usize;
                arr[idx] = delete_path(&arr[idx], rest)?;
            }
            Ok(Value::Array(arr))
        }
        Step::Iterate => match v {
            Value::Array(a) => {
                let mut out = Vec::with_capacity(a.len());
                for e in a {
                    out.push(delete_path(e, rest)?);
                }
                Ok(Value::Array(out))
            }
            Value::Object(m) => {
                let mut out = Vec::with_capacity(m.len());
                for (k, e) in m {
                    out.push((k.clone(), delete_path(e, rest)?));
                }
                Ok(Value::Object(out))
            }
            other => Err(EvalError::new(format!(
                "cannot iterate over {}",
                other.type_name()
            ))),
        },
        Step::Comment(_) => Err(EvalError::new(comment_mutation_unsupported())),
    }
}

/// Remove `step` from the container `v` (the leaf of a `del` path).
fn remove_step(v: &Value, step: &Step) -> Result<Value, EvalError> {
    match step {
        Step::Field(k) => match v {
            Value::Object(m) => {
                let kept = m.iter().filter(|(kk, _)| kk != k).cloned().collect();
                Ok(Value::Object(kept))
            }
            Value::Null => Ok(Value::Null),
            other => Err(EvalError::new(format!(
                "cannot delete a field of {}",
                other.type_name()
            ))),
        },
        Step::Index(i) => match v {
            Value::Array(a) => {
                let mut arr = a.clone();
                let idx = if *i < 0 { arr.len() as i64 + i } else { *i };
                if idx >= 0 && (idx as usize) < arr.len() {
                    arr.remove(idx as usize);
                }
                Ok(Value::Array(arr))
            }
            Value::Null => Ok(Value::Null),
            other => Err(EvalError::new(format!(
                "cannot delete an index of {}",
                other.type_name()
            ))),
        },
        Step::Iterate => match v {
            Value::Array(_) => Ok(Value::Array(Vec::new())),
            Value::Object(_) => Ok(Value::Object(Vec::new())),
            other => Err(EvalError::new(format!(
                "cannot iterate over {}",
                other.type_name()
            ))),
        },
        Step::Comment(_) => Err(EvalError::new(comment_mutation_unsupported())),
    }
}

/// The message for a comment edit, which lands in v0.2 Phase 2.
pub(crate) fn comment_mutation_unsupported() -> &'static str {
    "editing comments (`#`) is not supported yet (planned for v0.2); reading works, e.g. `edikt '.foo.#' file`"
}

fn length(v: &Value) -> Result<Value, EvalError> {
    let n = match v {
        Value::Null => 0,
        Value::Str(s) => s.chars().count() as i64,
        Value::Array(a) => a.len() as i64,
        Value::Object(m) => m.len() as i64,
        other => {
            return Err(EvalError::new(format!(
                "{} has no length",
                other.type_name()
            )));
        }
    };
    Ok(Value::Int(n))
}

fn keys(v: &Value) -> Result<Value, EvalError> {
    match v {
        Value::Object(m) => {
            let mut ks: Vec<String> = m.iter().map(|(k, _)| k.clone()).collect();
            ks.sort(); // jq's `keys` is sorted; use `keys_unsorted` later for order
            Ok(Value::Array(ks.into_iter().map(Value::Str).collect()))
        }
        Value::Array(a) => Ok(Value::Array((0..a.len() as i64).map(Value::Int).collect())),
        other => Err(EvalError::new(format!("{} has no keys", other.type_name()))),
    }
}

fn tonumber(v: &Value) -> Result<Value, EvalError> {
    match v {
        Value::Int(_) | Value::Float(_) => Ok(v.clone()),
        Value::Str(s) => {
            let t = s.trim();
            if let Ok(i) = t.parse::<i64>() {
                Ok(Value::Int(i))
            } else if let Ok(f) = t.parse::<f64>() {
                Ok(Value::Float(f))
            } else {
                Err(EvalError::new(format!("cannot parse \"{s}\" as a number")))
            }
        }
        other => Err(EvalError::new(format!(
            "cannot parse {} as a number",
            other.type_name()
        ))),
    }
}

/// The input as a string, for string-only builtins.
fn str_input<'a>(input: &'a Value, name: &str) -> Result<&'a str, EvalError> {
    match input {
        Value::Str(s) => Ok(s),
        other => Err(EvalError::new(format!(
            "{name} requires a string input, got {}",
            other.type_name()
        ))),
    }
}

/// Evaluate an argument expression to a single string (its first value).
fn str_arg(arg: &Expr, input: &Value, what: &str) -> Result<String, EvalError> {
    match eval(arg, input)?.into_iter().next() {
        Some(Value::Str(s)) => Ok(s),
        Some(other) => Err(EvalError::new(format!(
            "{what} must be a string, got {}",
            other.type_name()
        ))),
        None => Err(EvalError::new(format!("{what} produced no value"))),
    }
}

fn map_str(v: &Value, f: impl Fn(&str) -> String) -> Result<Value, EvalError> {
    match v {
        Value::Str(s) => Ok(Value::Str(f(s))),
        other => Err(EvalError::new(format!(
            "expected a string, got {}",
            other.type_name()
        ))),
    }
}

fn has(v: &Value, key: &Value) -> Result<bool, EvalError> {
    match (v, key) {
        (Value::Object(m), Value::Str(k)) => Ok(m.iter().any(|(kk, _)| kk == k)),
        (Value::Array(a), Value::Int(i)) => Ok(*i >= 0 && (*i as usize) < a.len()),
        _ => Err(EvalError::new(format!(
            "cannot check membership of {} in {}",
            key.type_name(),
            v.type_name()
        ))),
    }
}

fn trim_str(input: &Value, arg: &Expr, left: bool) -> Result<Vec<Value>, EvalError> {
    let s = match input {
        Value::Str(s) => s,
        other => {
            return Err(EvalError::new(format!(
                "expected a string, got {}",
                other.type_name()
            )));
        }
    };
    let mut out = Vec::new();
    for prefix in eval(arg, input)? {
        let p = match &prefix {
            Value::Str(p) => p,
            other => {
                return Err(EvalError::new(format!(
                    "expected a string argument, got {}",
                    other.type_name()
                )));
            }
        };
        let trimmed = if left {
            s.strip_prefix(p).unwrap_or(s)
        } else {
            s.strip_suffix(p).unwrap_or(s)
        };
        out.push(Value::Str(trimmed.to_string()));
    }
    Ok(out)
}