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
use core::fmt::Write;
use nu_engine::get_columns;
use nu_parser::escape_quote_string;
use nu_protocol::ast::{Call, RangeInclusion};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
};
#[derive(Clone)]
pub struct ToNuon;
impl Command for ToNuon {
fn name(&self) -> &str {
"to nuon"
}
fn signature(&self) -> Signature {
Signature::build("to nuon").category(Category::Experimental)
}
fn usage(&self) -> &str {
"Converts table data into Nuon (Nushell Object Notation) text."
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
Ok(Value::String {
val: to_nuon(call, input)?,
span: call.head,
}
.into_pipeline_data())
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Outputs a nuon string representing the contents of this list",
example: "[1 2 3] | to nuon",
result: Some(Value::test_string("[1, 2, 3]")),
}]
}
}
fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
match v {
Value::Binary { val, .. } => {
let mut s = String::with_capacity(2 * val.len());
for byte in val {
if write!(s, "{:02X}", byte).is_err() {
return Err(ShellError::UnsupportedInput(
"binary could not translate to string".into(),
span,
));
}
}
Ok(format!("0x[{}]", s))
}
Value::Block { .. } => Err(ShellError::UnsupportedInput(
"block not supported".into(),
span,
)),
Value::Bool { val, .. } => {
if *val {
Ok("true".to_string())
} else {
Ok("false".to_string())
}
}
Value::CellPath { .. } => Err(ShellError::UnsupportedInput(
"cellpath not supported".to_string(),
span,
)),
Value::CustomValue { .. } => Err(ShellError::UnsupportedInput(
"custom not supported".to_string(),
span,
)),
Value::Date { val, .. } => Ok(val.to_rfc3339()),
Value::Duration { val, .. } => Ok(format!("{}ns", *val)),
Value::Error { .. } => Err(ShellError::UnsupportedInput(
"error not supported".to_string(),
span,
)),
Value::Filesize { val, .. } => Ok(format!("{}b", *val)),
Value::Float { val, .. } => {
if &val.round() == val
&& val != &f64::NAN
&& val != &f64::INFINITY
&& val != &f64::NEG_INFINITY
{
Ok(format!("{}.0", *val))
} else {
Ok(format!("{}", *val))
}
}
Value::Int { val, .. } => Ok(format!("{}", *val)),
Value::List { vals, .. } => {
let headers = get_columns(vals);
if !headers.is_empty() && vals.iter().all(|x| x.columns() == headers) {
let headers_output = headers.join(", ");
let mut table_output = vec![];
for val in vals {
let mut row = vec![];
if let Value::Record { vals, .. } = val {
for val in vals {
row.push(value_to_string(val, span)?);
}
}
table_output.push(row.join(", "));
}
Ok(format!(
"[[{}]; [{}]]",
headers_output,
table_output.join("], [")
))
} else {
let mut collection = vec![];
for val in vals {
collection.push(value_to_string(val, span)?);
}
Ok(format!("[{}]", collection.join(", ")))
}
}
Value::Nothing { .. } => Ok("$nothing".to_string()),
Value::Range { val, .. } => Ok(format!(
"{}..{}{}",
value_to_string(&val.from, span)?,
if val.inclusion == RangeInclusion::RightExclusive {
"<"
} else {
""
},
value_to_string(&val.to, span)?
)),
Value::Record { cols, vals, .. } => {
let mut collection = vec![];
for (col, val) in cols.iter().zip(vals) {
collection.push(format!("\"{}\": {}", col, value_to_string(val, span)?));
}
Ok(format!("{{{}}}", collection.join(", ")))
}
Value::String { val, .. } => Ok(escape_quote_string(val)),
}
}
fn to_nuon(call: &Call, input: PipelineData) -> Result<String, ShellError> {
let v = input.into_value(call.head);
value_to_string(&v, call.head)
}
#[cfg(test)]
mod test {
#[test]
fn test_examples() {
use super::ToNuon;
use crate::test_examples;
test_examples(ToNuon {})
}
}