nu-command 0.114.0

Nushell's built-in commands
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
use crate::formats::{KDL_CANONICAL_METADATA_KEY, KDL_CANONICAL_METADATA_VALUE};
use kdl::{KdlDocument, KdlError, KdlNode, KdlValue};
use nu_engine::command_prelude::*;
use nu_protocol::{
    DEFAULT_ERROR_CONTEXT, shell_error::generic::GenericError, truncated_source_window,
};
use num_traits::ToPrimitive;

#[derive(Clone)]
pub struct FromKdl;

impl Command for FromKdl {
    fn name(&self) -> &str {
        "from kdl"
    }

    fn description(&self) -> &str {
        "Convert KDL text into structured data."
    }

    fn signature(&self) -> nu_protocol::Signature {
        Signature::build("from kdl")
            .input_output_types(vec![(Type::String, Type::Any)])
            .category(Category::Formats)
    }

    fn examples(&self) -> Vec<Example<'_>> {
        let span = Span::unknown();

        vec![
            Example {
                example: r#""node attr=1 attr2=#true {bloc}" | from kdl"#,
                description: "Converts KDL formatted string to canonical node rows.",
                result: Some(Value::test_list(vec![Value::test_record(record! {
                    "name" => Value::string("node", span),
                    "args" => Value::test_list(vec![]),
                    "props" => Value::test_record(record! {
                        "attr" => 1.into_value(span),
                        "attr2" => true.into_value(span),
                    }),
                    "children" => Value::test_list(vec![Value::test_record(record! {
                        "name" => Value::string("bloc", span),
                        "args" => Value::test_list(vec![]),
                        "props" => Value::test_record(record! {}),
                        "children" => Value::test_list(vec![]),
                    })]),
                })])),
            },
            Example {
                description: "Converts KDL formatted string to canonical node rows.",
                example: r#"'package { name nu; version 0.1; description "new type of shell" }' | from kdl"#,
                result: Some(Value::test_list(vec![Value::test_record(record! {
                    "name" => Value::string("package", span),
                    "args" => Value::test_list(vec![]),
                    "props" => Value::test_record(record! {}),
                    "children" => Value::test_list(vec![
                        Value::test_record(record! {
                            "name" => Value::string("name", span),
                            "args" => Value::test_list(vec![Value::string("nu", span)]),
                            "props" => Value::test_record(record! {}),
                            "children" => Value::test_list(vec![]),
                        }),
                        Value::test_record(record! {
                            "name" => Value::string("version", span),
                            "args" => Value::test_list(vec![Value::float(0.1, span)]),
                            "props" => Value::test_record(record! {}),
                            "children" => Value::test_list(vec![]),
                        }),
                        Value::test_record(record! {
                            "name" => Value::string("description", span),
                            "args" => Value::test_list(vec![Value::string("new type of shell", span)]),
                            "props" => Value::test_record(record! {}),
                            "children" => Value::test_list(vec![]),
                        }),
                    ]),
                })])),
            },
            Example {
                description: "Duplicate sibling node names are preserved in-order.",
                example: r#""node one; node two" | from kdl"#,
                result: Some(Value::test_list(vec![
                    Value::test_record(record! {
                        "name" => Value::string("node", span),
                        "args" => Value::test_list(vec![Value::string("one", span)]),
                        "props" => Value::test_record(record! {}),
                        "children" => Value::test_list(vec![]),
                    }),
                    Value::test_record(record! {
                        "name" => Value::string("node", span),
                        "args" => Value::test_list(vec![Value::string("two", span)]),
                        "props" => Value::test_record(record! {}),
                        "children" => Value::test_list(vec![]),
                    }),
                ])),
            },
        ]
    }

    fn run(
        &self,
        _engine: &EngineState,
        _stack: &mut Stack,
        call: &Call,
        mut input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let span = input.span().unwrap_or(call.head);
        let mut metadata = input
            .take_metadata()
            .unwrap_or_default()
            .with_content_type(None);

        let kdl_string_object = input.collect_string_strict(span)?;

        let kdl_data = parse_kdl_document_with_diagnostics(&kdl_string_object.0, span)?;
        let rows = convert_kdl_document_to_node_rows(kdl_data.nodes(), span)?;

        // Mark this output as canonical KDL node rows so `to kdl` can round-trip
        // without guessing by shape and accidentally reinterpreting normal records.
        metadata.custom.insert(
            KDL_CANONICAL_METADATA_KEY,
            Value::string(KDL_CANONICAL_METADATA_VALUE, span),
        );

        Ok(Value::list(rows, span).into_pipeline_data_with_metadata(Some(metadata)))
    }
}

fn parse_kdl_document_with_diagnostics(input: &str, span: Span) -> Result<KdlDocument, ShellError> {
    KdlDocument::parse(input).map_err(|err| kdl_error_to_shell_error(input, span, &err))
}

fn kdl_error_to_shell_error(input: &str, span: Span, err: &KdlError) -> ShellError {
    if let Some(diagnostic) = err.diagnostics.first() {
        let diagnostic_message = kdl_diagnostics_message(&err.diagnostics);
        let byte_offset = diagnostic.span.offset();
        let (src, label_span) = truncated_source_window(
            input,
            Span::new(byte_offset, byte_offset),
            DEFAULT_ERROR_CONTEXT,
        );

        return ShellError::Generic(
            GenericError::new(
                "Error while parsing KDL text",
                "error parsing KDL text",
                span,
            )
            .with_inner([ShellError::OutsideSpannedLabeledError {
                src,
                error: "Error while parsing KDL text".into(),
                msg: diagnostic_message,
                span: label_span,
            }]),
        );
    }

    ShellError::CantConvert {
        to_type: format!("structured kdl data ({err})"),
        from_type: "string".into(),
        span,
        help: None,
    }
}

fn kdl_diagnostics_message(diagnostics: &[kdl::KdlDiagnostic]) -> String {
    if diagnostics.len() == 1 {
        return kdl_diagnostic_message(&diagnostics[0]);
    }

    diagnostics
        .iter()
        .enumerate()
        .map(|(index, diagnostic)| {
            format!(
                "diagnostic {}:\n{}",
                index + 1,
                kdl_diagnostic_message(diagnostic)
            )
        })
        .collect::<Vec<_>>()
        .join("\n\n")
}

fn kdl_diagnostic_message(diagnostic: &kdl::KdlDiagnostic) -> String {
    let mut parts = Vec::new();

    if let Some(message) = &diagnostic.message {
        parts.push(message.clone());
    }

    if let Some(label) = &diagnostic.label
        && parts.last() != Some(label)
    {
        parts.push(label.clone());
    }

    if let Some(help) = &diagnostic.help {
        parts.push(format!("help: {help}"));
    }

    if parts.is_empty() {
        "error parsing KDL text".to_owned()
    } else {
        parts.join("\n")
    }
}

fn convert_kdl_document_to_node_rows(
    nodes: &[KdlNode],
    span: Span,
) -> Result<Vec<Value>, ShellError> {
    let mut rows = Vec::with_capacity(nodes.len());

    for node in nodes {
        rows.push(convert_kdl_node_to_node_row(node, span)?);
    }

    Ok(rows)
}

fn convert_kdl_node_to_node_row(kdl_node: &KdlNode, span: Span) -> Result<Value, ShellError> {
    let mut args = Vec::new();
    let mut props = Record::new();

    for entry in kdl_node.entries() {
        if let Some(name) = entry.name() {
            props.insert(
                name.value().to_string(),
                convert_kdl_value_to_nu_value(entry.value(), span)?,
            );
            continue;
        }

        args.push(convert_kdl_value_to_nu_value(entry.value(), span)?);
    }

    let children = if let Some(children_doc) = kdl_node.children() {
        convert_kdl_document_to_node_rows(children_doc.nodes(), span)?
    } else {
        Vec::new()
    };

    let row = record! {
        "name" => Value::string(kdl_node.name().value(), span),
        "args" => Value::list(args, span),
        "props" => props.into_value(span),
        "children" => Value::list(children, span),
    };

    Ok(row.into_value(span))
}

fn convert_kdl_value_to_nu_value(value: &KdlValue, span: Span) -> Result<Value, ShellError> {
    match value {
        KdlValue::String(val) => Ok(Value::string(val, span)),
        KdlValue::Integer(val) => Ok(Value::int(
            val.to_i64().ok_or(ShellError::UnsupportedInput {
                msg: "integer value is too large to fit in i64".to_owned(),
                input: "value originates from here".to_owned(),
                msg_span: span,
                input_span: span,
            })?,
            span,
        )),
        KdlValue::Float(val) => Ok(Value::float(*val, span)),
        KdlValue::Bool(val) => Ok(Value::bool(*val, span)),
        KdlValue::Null => Ok(Value::nothing(span)),
    }
}

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

    fn node_name(row: &Value) -> &str {
        row.as_record()
            .ok()
            .and_then(|record| record.get("name"))
            .and_then(|value| value.as_str().ok())
            .expect("row should contain string name")
    }

    #[test]
    fn test_examples() -> nu_test_support::Result {
        nu_test_support::test().examples(FromKdl)
    }

    #[test]
    fn duplicate_sibling_names_are_preserved_in_order() {
        let span = Span::test_data();
        let kdl_document = KdlDocument::parse("node one\nnode two\nnode three")
            .expect("failed to parse duplicate sibling document");

        let output_rows = convert_kdl_document_to_node_rows(kdl_document.nodes(), span)
            .expect("conversion failed");

        assert_eq!(output_rows.len(), 3);
        assert_eq!(node_name(&output_rows[0]), "node");
        assert_eq!(node_name(&output_rows[1]), "node");
        assert_eq!(node_name(&output_rows[2]), "node");

        let second_args = output_rows[1]
            .as_record()
            .ok()
            .and_then(|record| record.get("args"))
            .and_then(|value| value.as_list().ok())
            .expect("missing args list");

        assert_eq!(
            second_args.first().cloned(),
            Some(Value::string("two", span))
        );
    }

    #[test]
    fn duplicate_properties_use_rightmost_value() {
        let span = Span::test_data();
        let kdl_document = KdlDocument::parse("node attr=1 attr=2")
            .expect("failed to parse duplicate property document");

        let output_rows = convert_kdl_document_to_node_rows(kdl_document.nodes(), span)
            .expect("conversion failed");

        let props = output_rows[0]
            .as_record()
            .ok()
            .and_then(|record| record.get("props"))
            .and_then(|value| value.as_record().ok())
            .expect("missing props record");

        assert_eq!(props.len(), 1);
        assert_eq!(props.get("attr"), Some(&Value::int(2, span)));
    }

    #[test]
    fn parse_errors_use_structured_kdl_diagnostics() {
        let error = parse_kdl_document_with_diagnostics("node 1.", Span::test_data())
            .expect_err("invalid KDL should fail");

        let ShellError::Generic(generic) = error else {
            panic!("expected generic shell error");
        };

        let Some(ShellError::OutsideSpannedLabeledError { msg, .. }) = generic.inner.first() else {
            panic!("expected structured inner parse diagnostic");
        };

        assert!(!msg.trim().is_empty());
        assert_ne!(msg.trim(), "error parsing KDL text");
    }

    #[test]
    fn multiple_kdl_diagnostics_are_aggregated() {
        let err = KdlDocument::parse("node 1.").expect_err("input should fail to parse");
        let mut diagnostics = err.diagnostics.clone();

        diagnostics.push(
            diagnostics
                .first()
                .expect("expected at least one diagnostic")
                .clone(),
        );

        let message = kdl_diagnostics_message(&diagnostics);

        assert!(message.contains("diagnostic 1:"));
        assert!(message.contains("diagnostic 2:"));
    }

    #[test]
    fn canonical_row_shape_splits_args_props_and_children() {
        let span = Span::test_data();
        let kdl_document = KdlDocument::parse("item 1 2 enabled=#true { child 9 }")
            .expect("failed to parse mixed kdl node");

        let output_rows = convert_kdl_document_to_node_rows(kdl_document.nodes(), span)
            .expect("conversion failed");

        let row = output_rows
            .first()
            .and_then(|value| value.as_record().ok())
            .expect("missing top-level row");

        assert_eq!(row.get("name").cloned(), Some(Value::string("item", span)));
        assert_eq!(
            row.get("args")
                .and_then(|value| value.as_list().ok())
                .map(|args| args.len()),
            Some(2)
        );
        assert_eq!(
            row.get("props")
                .and_then(|value| value.as_record().ok())
                .and_then(|props| props.get("enabled"))
                .cloned(),
            Some(Value::bool(true, span))
        );
        assert_eq!(
            row.get("children")
                .and_then(|value| value.as_list().ok())
                .map(|children| children.len()),
            Some(1)
        );
    }

    #[test]
    fn test_official_kdl_website_example() {
        const KDL_WEBSITE_EXAMPLE: &str = r#"
        package {
          name my-pkg
          version "1.2.3"

          dependencies {
            // Nodes can have standalone values as well as
            // key/value pairs.
            lodash "^3.2.1" optional=#true alias=underscore
          }

          scripts {
            // "Raw" and dedented multi-line strings are supported.
            message """
            hello
            world
            """
          }

          // `\` breaks up a single node across multiple lines.
          the-matrix 1 2 3 \
          4 5 6 \
          7 8 9

          // "Slashdash" comments operate at the node level,
          // with just `/-`.
          /-this-is-commented {
            this entire node {
              is gone
            }
          }
        }"#;

        let kdl_document =
            KdlDocument::parse(KDL_WEBSITE_EXAMPLE).expect("failed to parse kdl string");

        let span = Span::test_data();

        let output_rows = convert_kdl_document_to_node_rows(kdl_document.nodes(), span)
            .expect("kdl conversion failed");

        let package = output_rows
            .first()
            .and_then(|row| row.as_record().ok())
            .expect("missing package node");

        let package_children = package
            .get("children")
            .and_then(|value| value.as_list().ok())
            .expect("missing package children");

        let matrix_row = package_children
            .iter()
            .find(|node| {
                node.as_record()
                    .ok()
                    .and_then(|record| record.get("name"))
                    .and_then(|name| name.as_str().ok())
                    == Some("the-matrix")
            })
            .expect("missing matrix row");

        assert_eq!(
            matrix_row
                .as_record()
                .ok()
                .and_then(|record| record.get("args"))
                .and_then(|args| args.as_list().ok())
                .and_then(|args| args.first())
                .cloned()
                .expect("missing matrix first arg"),
            Value::int(1, span)
        );

        let scripts_row = package_children
            .iter()
            .find(|node| {
                node.as_record()
                    .ok()
                    .and_then(|record| record.get("name"))
                    .and_then(|name| name.as_str().ok())
                    == Some("scripts")
            })
            .expect("missing scripts row");

        let message_row = scripts_row
            .as_record()
            .ok()
            .and_then(|record| record.get("children"))
            .and_then(|children| children.as_list().ok())
            .and_then(|children| {
                children.iter().find(|node| {
                    node.as_record()
                        .ok()
                        .and_then(|record| record.get("name"))
                        .and_then(|name| name.as_str().ok())
                        == Some("message")
                })
            })
            .expect("missing message row");

        assert_eq!(
            message_row
                .as_record()
                .ok()
                .and_then(|record| record.get("args"))
                .and_then(|args| args.as_list().ok())
                .and_then(|args| args.first())
                .cloned()
                .expect("missing message text"),
            Value::string("hello\nworld", span)
        );
    }

    #[test]
    fn kdl_error_source_is_bounded() {
        let mut input = String::with_capacity(50_000);
        for _ in 0..2000 {
            input.push_str("node1 key=1; ");
        }
        input.push_str("node2 \"unclosed"); // Syntax error: unclosed string

        let result = parse_kdl_document_with_diagnostics(&input, Span::test_data());
        assert!(result.is_err(), "should fail to parse");

        let err = result.unwrap_err();
        match &err {
            ShellError::Generic(GenericError { inner, .. }) => {
                let inner_err = inner.first().expect("should have inner error");
                match inner_err {
                    ShellError::OutsideSpannedLabeledError { src, .. } => {
                        assert!(
                            src.len() < 20_000,
                            "error source should be bounded, got {} bytes",
                            src.len()
                        );
                    }
                    other => panic!("expected OutsideSpannedLabeledError, got {other:?}"),
                }
            }
            other => panic!("expected Generic error, got {other:?}"),
        }
    }

    #[test]
    fn kdl_parse_success_not_affected() {
        let result = parse_kdl_document_with_diagnostics(
            r#"node1 key=1; node2 key="val""#,
            Span::test_data(),
        );
        assert!(result.is_ok(), "valid KDL should still parse");
    }
}