nuon 0.112.2

Support for the NUON format.
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
use core::fmt::Write;
use nu_engine::get_columns;
use nu_protocol::{Range, ShellError, Span, Value, engine::EngineState};
use nu_utils::{ObviousFloat, as_raw_string, escape_quote_string, needs_quoting};

/// Configuration for converting Nushell [`Value`] to NUON data.
///
/// Use [`ToNuonConfig::default()`] to get started, then chain builder methods.
///
/// # Example
/// ```ignore
/// let config = ToNuonConfig::default()
///     .style(ToStyle::Spaces(2))
///     .raw_strings(true);
/// to_nuon(&engine_state, &value, config)?;
/// ```
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct ToNuonConfig {
    /// Formatting style (indentation)
    pub style: ToStyle,
    /// Optional span for error reporting
    pub span: Option<Span>,
    /// Serialize non-serializable types (like closures) as strings
    pub serialize_types: bool,
    /// Prefer raw string syntax (`r#'...'#`) when strings contain quotes or backslashes
    pub raw_strings: bool,
    /// Serialize list-of-records values as list-of-records instead of table syntax
    pub list_of_records: bool,
}

impl ToNuonConfig {
    /// Set the formatting style
    pub fn style(mut self, style: ToStyle) -> Self {
        self.style = style;
        self
    }

    /// Set the span for error reporting
    pub fn span(mut self, span: Option<Span>) -> Self {
        self.span = span;
        self
    }

    /// Enable serialization of non-serializable types as strings
    pub fn serialize_types(mut self, serialize_types: bool) -> Self {
        self.serialize_types = serialize_types;
        self
    }

    /// Prefer raw string syntax when strings contain quotes or backslashes
    pub fn raw_strings(mut self, raw_strings: bool) -> Self {
        self.raw_strings = raw_strings;
        self
    }

    /// Serialize list-of-records values as list-of-records instead of table syntax
    pub fn list_of_records(mut self, list_of_records: bool) -> Self {
        self.list_of_records = list_of_records;
        self
    }
}

/// control the way Nushell [`Value`] is converted to NUON data
#[derive(Clone, Debug, Default)]
pub enum ToStyle {
    /// no extra indentation
    ///
    /// `{ a: 1, b: 2 }` will be converted to `{a: 1, b: 2}`
    #[default]
    Default,
    /// no white space at all
    ///
    /// `{ a: 1, b: 2 }` will be converted to `{a:1,b:2}`
    Raw,
    #[allow(clippy::tabs_in_doc_comments)]
    /// tabulation-based indentation
    ///
    /// using 2 as the variant value, `{ a: 1, b: 2 }` will be converted to
    /// ```text
    /// {
    /// 		a: 1,
    /// 		b: 2
    /// }
    /// ```
    Tabs(usize),
    /// space-based indentation
    ///
    /// using 3 as the variant value, `{ a: 1, b: 2 }` will be converted to
    /// ```text
    /// {
    ///    a: 1,
    ///    b: 2
    /// }
    /// ```
    Spaces(usize),
}

/// convert an actual Nushell [`Value`] to a raw string representation of the NUON data
///
// WARNING: please leave the following two trailing spaces, they matter for the documentation
// formatting
/// > **Note**
/// > a [`Span`] can be passed to [`to_nuon`] if there is context available to the caller, e.g. when
/// > using this function in a command implementation such as [`to nuon`](https://www.nushell.sh/commands/docs/to_nuon.html).
///
/// also see [`super::from_nuon`] for the inverse operation
///
/// # Example
/// ```ignore
/// use nuon::{to_nuon, ToNuonConfig, ToStyle};
///
/// let config = ToNuonConfig::default()
///     .style(ToStyle::Spaces(2))
///     .raw_strings(true);
/// let result = to_nuon(&engine_state, &value, config)?;
/// ```
pub fn to_nuon(
    engine_state: &EngineState,
    input: &Value,
    config: ToNuonConfig,
) -> Result<String, ShellError> {
    let span = config.span.unwrap_or(Span::unknown());

    let indentation = match config.style {
        ToStyle::Default => None,
        ToStyle::Raw => Some("".to_string()),
        ToStyle::Tabs(t) => Some("\t".repeat(t)),
        ToStyle::Spaces(s) => Some(" ".repeat(s)),
    };

    let res = value_to_string(
        engine_state,
        input,
        span,
        0,
        indentation.as_deref(),
        StringifyOptions {
            serialize_types: config.serialize_types,
            raw_strings: config.raw_strings,
            list_of_records: config.list_of_records,
        },
    )?;

    Ok(res)
}

#[derive(Clone, Copy)]
struct StringifyOptions {
    serialize_types: bool,
    raw_strings: bool,
    list_of_records: bool,
}

fn value_to_string(
    engine_state: &EngineState,
    v: &Value,
    span: Span,
    depth: usize,
    indent: Option<&str>,
    options: StringifyOptions,
) -> Result<String, ShellError> {
    let (nl, sep, kv_sep) = get_true_separators(indent);
    let idt = get_true_indentation(depth, indent);
    let idt_po = get_true_indentation(depth + 1, indent);
    let idt_pt = get_true_indentation(depth + 2, indent);

    match v {
        Value::Binary { val, .. } => {
            let mut s = String::with_capacity(2 * val.len());
            for byte in val {
                if write!(s, "{byte:02X}").is_err() {
                    return Err(ShellError::UnsupportedInput {
                        msg: "could not convert binary to string".into(),
                        input: "value originates from here".into(),
                        msg_span: span,
                        input_span: v.span(),
                    });
                }
            }
            Ok(format!("0x[{s}]"))
        }
        Value::Closure { val, .. } => {
            if options.serialize_types {
                Ok(quote_string(
                    &val.coerce_into_string(engine_state, span)?,
                    options.raw_strings,
                ))
            } else {
                Err(ShellError::UnsupportedInput {
                    msg: "closures are currently not deserializable (use --serialize to serialize as a string)".into(),
                    input: "value originates from here".into(),
                    msg_span: span,
                    input_span: v.span(),
                })
            }
        }
        Value::Bool { val, .. } => {
            if *val {
                Ok("true".to_string())
            } else {
                Ok("false".to_string())
            }
        }
        Value::CellPath { val, .. } => Ok(val.to_string()),
        Value::Custom { .. } => Err(ShellError::UnsupportedInput {
            msg: "custom values are currently not nuon-compatible".to_string(),
            input: "value originates from here".into(),
            msg_span: span,
            input_span: v.span(),
        }),
        Value::Date { val, .. } => Ok(val.to_rfc3339()),
        // FIXME: make durations use the shortest lossless representation.
        Value::Duration { val, .. } => Ok(format!("{}ns", *val)),
        // Propagate existing errors
        Value::Error { error, .. } => Err(*error.clone()),
        // FIXME: make filesizes use the shortest lossless representation.
        Value::Filesize { val, .. } => Ok(format!("{}b", val.get())),
        Value::Float { val, .. } => Ok(ObviousFloat(*val).to_string()),
        Value::Int { val, .. } => Ok(val.to_string()),
        Value::List { vals, .. } => {
            let headers = get_columns(vals);
            let is_table =
                !headers.is_empty() && vals.iter().all(|x| x.columns().eq(headers.iter()));

            if is_table && !options.list_of_records {
                // Table output
                let headers: Vec<String> = headers
                    .iter()
                    .map(|string| {
                        let string = if needs_quoting(string) {
                            &escape_quote_string(string)
                        } else {
                            string
                        };
                        format!("{idt}{string}")
                    })
                    .collect();

                let record_rows = |fmt_row: &dyn Fn(Vec<String>) -> String| {
                    vals.iter()
                        .filter_map(|val| {
                            let Value::Record { val, .. } = val else {
                                return None;
                            };
                            Some(
                                val.values()
                                    .map(|v| {
                                        value_to_string_without_quotes(
                                            engine_state,
                                            v,
                                            span,
                                            depth + 2,
                                            indent,
                                            options,
                                        )
                                    })
                                    .collect::<Result<Vec<_>, _>>()
                                    .map(fmt_row),
                            )
                        })
                        .collect::<Result<Vec<_>, _>>()
                };

                if indent.is_some_and(|i| !i.is_empty()) {
                    let header_row = format!("[{}];", headers.join(", "));

                    let value_rows = record_rows(&|row| format!("[{}]", row.join(", ")))?
                        .join(&format!(",{nl}{idt_po}"));

                    Ok(format!(
                        "[{nl}{idt_po}{}{sep}{nl}{idt_po}{}{nl}{idt}]",
                        header_row, value_rows
                    ))
                } else {
                    let headers_output = headers.join(&format!(",{sep}{nl}{idt_pt}"));

                    let table_output =
                        record_rows(&|row| row.join(&format!(",{sep}{nl}{idt_pt}")))?
                            .join(&format!("{nl}{idt_po}],{sep}{nl}{idt_po}[{nl}{idt_pt}"));

                    Ok(format!(
                        "[{nl}{idt_po}[{nl}{idt_pt}{}{nl}{idt_po}];{sep}{nl}{idt_po}[{nl}{idt_pt}{}{nl}{idt_po}]{nl}{idt}]",
                        headers_output, table_output
                    ))
                }
            } else if is_table && options.list_of_records {
                let mut collection = vec![];
                let row_indent = if indent == Some("") { Some("") } else { None };

                for val in vals {
                    collection.push(format!(
                        "{idt_po}{}",
                        value_to_string_without_quotes(
                            engine_state,
                            val,
                            span,
                            depth + 1,
                            row_indent,
                            options,
                        )?
                    ));
                }

                Ok(format!(
                    "[{nl}{}{nl}{idt}]",
                    collection.join(&format!(",{sep}{nl}"))
                ))
            } else {
                let mut collection = vec![];
                for val in vals {
                    collection.push(format!(
                        "{idt_po}{}",
                        value_to_string_without_quotes(
                            engine_state,
                            val,
                            span,
                            depth + 1,
                            indent,
                            options,
                        )?
                    ));
                }
                Ok(format!(
                    "[{nl}{}{nl}{idt}]",
                    collection.join(&format!(",{sep}{nl}"))
                ))
            }
        }
        Value::Nothing { .. } => Ok("null".to_string()),
        Value::Range { val, .. } => match **val {
            Range::IntRange(range) => Ok(range.to_string()),
            Range::FloatRange(range) => Ok(range.to_string()),
        },
        Value::Record { val, .. } => {
            let mut collection = vec![];
            for (col, val) in &**val {
                let col = if needs_quoting(col) {
                    &escape_quote_string(col)
                } else {
                    col
                };
                collection.push(format!(
                    "{idt_po}{col}:{kv_sep}{}",
                    value_to_string_without_quotes(
                        engine_state,
                        val,
                        span,
                        depth + 1,
                        indent,
                        options,
                    )?
                ));
            }
            Ok(format!(
                "{{{nl}{}{nl}{idt}}}",
                collection.join(&format!(",{sep}{nl}"))
            ))
        }
        // All strings outside data structures are quoted because they are in 'command position'
        // (could be mistaken for commands by the Nu parser)
        Value::String { val, .. } => Ok(quote_string(val, options.raw_strings)),
        Value::Glob { val, .. } => Ok(quote_string(val, options.raw_strings)),
    }
}

fn get_true_indentation(depth: usize, indent: Option<&str>) -> String {
    match indent {
        Some(i) => i.repeat(depth),
        None => "".to_string(),
    }
}

/// Quote a string, using raw string syntax if `raw_strings` is true and the string
/// contains quotes or backslashes.
fn quote_string(s: &str, raw_strings: bool) -> String {
    if raw_strings && let Some(raw) = as_raw_string(s) {
        return raw;
    }
    escape_quote_string(s)
}

/// Converts the provided indent into three types of separator:
/// - New line separators
/// - Inline separator
/// - Key-value separators inside Records
fn get_true_separators(indent: Option<&str>) -> (String, String, String) {
    match indent {
        Some("") => ("".to_string(), "".to_string(), "".to_string()),
        Some(_) => ("\n".to_string(), "".to_string(), " ".to_string()),
        None => ("".to_string(), " ".to_string(), " ".to_string()),
    }
}

fn value_to_string_without_quotes(
    engine_state: &EngineState,
    v: &Value,
    span: Span,
    depth: usize,
    indent: Option<&str>,
    options: StringifyOptions,
) -> Result<String, ShellError> {
    match v {
        Value::String { val, .. } => Ok({
            if needs_quoting(val) {
                quote_string(val, options.raw_strings)
            } else {
                val.clone()
            }
        }),
        _ => value_to_string(engine_state, v, span, depth, indent, options),
    }
}