forked from nushell/nushell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toml.rs
247 lines (222 loc) · 7.81 KB
/
toml.rs
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
use chrono::SecondsFormat;
use nu_engine::command_prelude::*;
use nu_protocol::ast::PathMember;
#[derive(Clone)]
pub struct ToToml;
impl Command for ToToml {
fn name(&self) -> &str {
"to toml"
}
fn signature(&self) -> Signature {
Signature::build("to toml")
.input_output_types(vec![(Type::Record(vec![]), Type::String)])
.category(Category::Formats)
}
fn usage(&self) -> &str {
"Convert record into .toml text."
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Outputs an TOML string representing the contents of this record",
example: r#"{foo: 1 bar: 'qwe'} | to toml"#,
result: Some(Value::test_string("bar = \"qwe\"\nfoo = 1\n")),
}]
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
to_toml(engine_state, input, head)
}
}
// Helper method to recursively convert nu_protocol::Value -> toml::Value
// This shouldn't be called at the top-level
fn helper(engine_state: &EngineState, v: &Value) -> Result<toml::Value, ShellError> {
let span = v.span();
Ok(match &v {
Value::Bool { val, .. } => toml::Value::Boolean(*val),
Value::Int { val, .. } => toml::Value::Integer(*val),
Value::Filesize { val, .. } => toml::Value::Integer(*val),
Value::Duration { val, .. } => toml::Value::String(val.to_string()),
Value::Date { val, .. } => {
toml::Value::String(val.to_rfc3339_opts(SecondsFormat::AutoSi, false))
}
Value::Range { .. } => toml::Value::String("<Range>".to_string()),
Value::Float { val, .. } => toml::Value::Float(*val),
Value::String { val, .. } | Value::Glob { val, .. } => toml::Value::String(val.clone()),
Value::Record { val, .. } => {
let mut m = toml::map::Map::new();
for (k, v) in val {
m.insert(k.clone(), helper(engine_state, v)?);
}
toml::Value::Table(m)
}
Value::LazyRecord { val, .. } => {
let collected = val.collect()?;
helper(engine_state, &collected)?
}
Value::List { vals, .. } => toml::Value::Array(toml_list(engine_state, vals)?),
Value::Block { .. } => {
let code = engine_state.get_span_contents(span);
let code = String::from_utf8_lossy(code).to_string();
toml::Value::String(code)
}
Value::Closure { .. } => {
let code = engine_state.get_span_contents(span);
let code = String::from_utf8_lossy(code).to_string();
toml::Value::String(code)
}
Value::Nothing { .. } => toml::Value::String("<Nothing>".to_string()),
Value::Error { error, .. } => return Err(*error.clone()),
Value::Binary { val, .. } => toml::Value::Array(
val.iter()
.map(|x| toml::Value::Integer(*x as i64))
.collect(),
),
Value::CellPath { val, .. } => toml::Value::Array(
val.members
.iter()
.map(|x| match &x {
PathMember::String { val, .. } => Ok(toml::Value::String(val.clone())),
PathMember::Int { val, .. } => Ok(toml::Value::Integer(*val as i64)),
})
.collect::<Result<Vec<toml::Value>, ShellError>>()?,
),
Value::Custom { .. } => toml::Value::String("<Custom Value>".to_string()),
})
}
fn toml_list(engine_state: &EngineState, input: &[Value]) -> Result<Vec<toml::Value>, ShellError> {
let mut out = vec![];
for value in input {
out.push(helper(engine_state, value)?);
}
Ok(out)
}
fn toml_into_pipeline_data(
toml_value: &toml::Value,
value_type: Type,
span: Span,
) -> Result<PipelineData, ShellError> {
match toml::to_string(&toml_value) {
Ok(serde_toml_string) => Ok(Value::string(serde_toml_string, span).into_pipeline_data()),
_ => Ok(Value::error(
ShellError::CantConvert {
to_type: "TOML".into(),
from_type: value_type.to_string(),
span,
help: None,
},
span,
)
.into_pipeline_data()),
}
}
fn value_to_toml_value(
engine_state: &EngineState,
v: &Value,
head: Span,
) -> Result<toml::Value, ShellError> {
match v {
Value::Record { .. } => helper(engine_state, v),
// Propagate existing errors
Value::Error { error, .. } => Err(*error.clone()),
_ => Err(ShellError::UnsupportedInput {
msg: format!("{:?} is not valid top-level TOML", v.get_type()),
input: "value originates from here".into(),
msg_span: head,
input_span: v.span(),
}),
}
}
fn to_toml(
engine_state: &EngineState,
input: PipelineData,
span: Span,
) -> Result<PipelineData, ShellError> {
let value = input.into_value(span);
let toml_value = value_to_toml_value(engine_state, &value, span)?;
match toml_value {
toml::Value::Array(ref vec) => match vec[..] {
[toml::Value::Table(_)] => toml_into_pipeline_data(
vec.iter().next().expect("this should never trigger"),
value.get_type(),
span,
),
_ => toml_into_pipeline_data(&toml_value, value.get_type(), span),
},
_ => toml_into_pipeline_data(&toml_value, value.get_type(), span),
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::TimeZone;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(ToToml {})
}
#[test]
fn to_toml_creates_correct_date() {
let engine_state = EngineState::new();
let test_date = Value::date(
chrono::FixedOffset::east_opt(60 * 120)
.unwrap()
.with_ymd_and_hms(1980, 10, 12, 10, 12, 44)
.unwrap(),
Span::test_data(),
);
let reference_date = toml::Value::String(String::from("1980-10-12T10:12:44+02:00"));
let result = helper(&engine_state, &test_date);
assert!(result.is_ok_and(|res| res == reference_date));
}
#[test]
fn test_value_to_toml_value() {
//
// Positive Tests
//
let engine_state = EngineState::new();
let mut m = indexmap::IndexMap::new();
m.insert("rust".to_owned(), Value::test_string("editor"));
m.insert("is".to_owned(), Value::nothing(Span::test_data()));
m.insert(
"features".to_owned(),
Value::list(
vec![Value::test_string("hello"), Value::test_string("array")],
Span::test_data(),
),
);
let tv = value_to_toml_value(
&engine_state,
&Value::record(m.into_iter().collect(), Span::test_data()),
Span::test_data(),
)
.expect("Expected Ok from valid TOML dictionary");
assert_eq!(
tv.get("features"),
Some(&toml::Value::Array(vec![
toml::Value::String("hello".to_owned()),
toml::Value::String("array".to_owned())
]))
);
//
// Negative Tests
//
value_to_toml_value(
&engine_state,
&Value::test_string("not_valid"),
Span::test_data(),
)
.expect_err("Expected non-valid toml (String) to cause error!");
value_to_toml_value(
&engine_state,
&Value::list(vec![Value::test_string("1")], Span::test_data()),
Span::test_data(),
)
.expect_err("Expected non-valid toml (Table) to cause error!");
}
}