1use crate::semver::value::SemverValue;
2use chrono::{DateTime, Datelike, FixedOffset, Timelike};
3use nu_engine::command_prelude::*;
4use nu_protocol::format_duration_as_timeperiod;
5
6#[derive(Clone)]
7pub struct IntoRecord;
8
9impl Command for IntoRecord {
10 fn name(&self) -> &str {
11 "into record"
12 }
13
14 fn signature(&self) -> Signature {
15 Signature::build("into record")
16 .input_output_types(vec![
17 (Type::Date, Type::record()),
18 (Type::Duration, Type::record()),
19 (Type::List(Box::new(Type::Any)), Type::record()),
20 (Type::record(), Type::record()),
21 ])
22 .category(Category::Conversions)
23 }
24
25 fn description(&self) -> &str {
26 "Convert value to a record."
27 }
28
29 fn search_terms(&self) -> Vec<&str> {
30 vec!["convert"]
31 }
32
33 fn run(
34 &self,
35 _engine_state: &EngineState,
36 _stack: &mut Stack,
37 call: &Call,
38 input: PipelineData,
39 ) -> Result<PipelineData, ShellError> {
40 into_record(call, input)
41 }
42
43 fn examples(&self) -> Vec<Example<'_>> {
44 vec![
45 Example {
46 description: "Convert from one row table to record.",
47 example: "[[value]; [false]] | into record",
48 result: Some(Value::test_record(record! {
49 "value" => Value::test_bool(false),
50 })),
51 },
52 Example {
53 description: "Convert from list of records to record.",
54 example: "[{foo: bar} {baz: quux}] | into record",
55 result: Some(Value::test_record(record! {
56 "foo" => Value::test_string("bar"),
57 "baz" => Value::test_string("quux"),
58 })),
59 },
60 Example {
61 description: "Convert from list of pairs into record.",
62 example: "[[foo bar] [baz quux]] | into record",
63 result: Some(Value::test_record(record! {
64 "foo" => Value::test_string("bar"),
65 "baz" => Value::test_string("quux"),
66 })),
67 },
68 Example {
69 description: "convert duration to record (weeks max).",
70 example: "(-500day - 4hr - 5sec) | into record",
71 result: Some(Value::test_record(record! {
72 "week" => Value::test_int(71),
73 "day" => Value::test_int(3),
74 "hour" => Value::test_int(4),
75 "second" => Value::test_int(5),
76 "sign" => Value::test_string("-"),
77 })),
78 },
79 Example {
80 description: "convert record to record.",
81 example: "{a: 1, b: 2} | into record",
82 result: Some(Value::test_record(record! {
83 "a" => Value::test_int(1),
84 "b" => Value::test_int(2),
85 })),
86 },
87 Example {
88 description: "convert date to record.",
89 example: "2020-04-12T22:10:57+02:00 | into record",
90 result: Some(Value::test_record(record! {
91 "year" => Value::test_int(2020),
92 "month" => Value::test_int(4),
93 "day" => Value::test_int(12),
94 "hour" => Value::test_int(22),
95 "minute" => Value::test_int(10),
96 "second" => Value::test_int(57),
97 "millisecond" => Value::test_int(0),
98 "microsecond" => Value::test_int(0),
99 "nanosecond" => Value::test_int(0),
100 "timezone" => Value::test_string("+02:00"),
101 })),
102 },
103 Example {
104 description: "convert date components to table columns.",
105 example: "2020-04-12T22:10:57+02:00 | into record | transpose | transpose -r",
106 result: None,
107 },
108 ]
109 }
110}
111
112fn into_record(call: &Call, input: PipelineData) -> Result<PipelineData, ShellError> {
113 let span = input.span().unwrap_or(call.head);
114 match input {
115 PipelineData::Value(Value::Date { val, .. }, _) => {
116 Ok(parse_date_into_record(val, span).into_pipeline_data())
117 }
118 PipelineData::Value(Value::Duration { val, .. }, _) => {
119 Ok(parse_duration_into_record(val, span).into_pipeline_data())
120 }
121 PipelineData::Value(Value::Custom { val, .. }, _) => {
122 if let Some(semver) = val.as_any().downcast_ref::<SemverValue>() {
123 Ok(parse_semver_into_record(semver, span).into_pipeline_data())
124 } else {
125 Err(ShellError::TypeMismatch {
126 err_message: format!("Can't convert {} to record", val.type_name()),
127 span,
128 })
129 }
130 }
131 PipelineData::Value(Value::List { .. }, _) | PipelineData::ListStream(..) => {
132 let mut input = input;
133 let mut record = Record::new();
134 let metadata = input.take_metadata();
135
136 enum ExpectedType {
137 Record,
138 Pair,
139 }
140 let mut expected_type = None;
141
142 for item in input.into_iter() {
143 let span = item.span();
144 match item {
145 Value::Record { val, .. }
146 if matches!(expected_type, None | Some(ExpectedType::Record)) =>
147 {
148 for (key, val) in val.into_owned() {
150 record.insert(key, val);
151 }
152 expected_type = Some(ExpectedType::Record);
153 }
154 Value::List { mut vals, .. }
155 if matches!(expected_type, None | Some(ExpectedType::Pair)) =>
156 {
157 if vals.len() == 2 {
158 let (val, key) = vals.pop().zip(vals.pop()).expect("length is < 2");
159 record.insert(key.coerce_into_string()?, val);
160 } else {
161 return Err(ShellError::IncorrectValue {
162 msg: format!(
163 "expected inner list with two elements, but found {} element(s)",
164 vals.len()
165 ),
166 val_span: span,
167 call_span: call.head,
168 });
169 }
170 expected_type = Some(ExpectedType::Pair);
171 }
172 Value::Nothing { .. } => {}
173 Value::Error { error, .. } => return Err(*error),
174 _ => {
175 return Err(ShellError::TypeMismatch {
176 err_message: format!(
177 "expected {}, found {} (while building record from list)",
178 match expected_type {
179 Some(ExpectedType::Record) => "record",
180 Some(ExpectedType::Pair) => "list with two elements",
181 None => "record or list with two elements",
182 },
183 item.get_type(),
184 ),
185 span,
186 });
187 }
188 }
189 }
190 Ok(Value::record(record, span).into_pipeline_data_with_metadata(metadata))
191 }
192 PipelineData::Value(Value::Record { .. }, _) => Ok(input),
193 PipelineData::Value(Value::Error { error, .. }, _) => Err(*error),
194 other => Err(ShellError::TypeMismatch {
195 err_message: format!("Can't convert {} to record", other.get_type()),
196 span,
197 }),
198 }
199}
200
201fn parse_date_into_record(date: DateTime<FixedOffset>, span: Span) -> Value {
202 Value::record(
203 record! {
204 "year" => Value::int(date.year() as i64, span),
205 "month" => Value::int(date.month() as i64, span),
206 "day" => Value::int(date.day() as i64, span),
207 "hour" => Value::int(date.hour() as i64, span),
208 "minute" => Value::int(date.minute() as i64, span),
209 "second" => Value::int(date.second() as i64, span),
210 "millisecond" => Value::int(date.timestamp_subsec_millis() as i64, span),
211 "microsecond" => Value::int((date.nanosecond() / 1_000 % 1_000) as i64, span),
212 "nanosecond" => Value::int((date.nanosecond() % 1_000) as i64, span),
213 "timezone" => Value::string(date.offset().to_string(), span),
214 },
215 span,
216 )
217}
218
219fn parse_duration_into_record(duration: i64, span: Span) -> Value {
220 let (sign, periods) = format_duration_as_timeperiod(duration);
221
222 let mut record = Record::new();
223 for p in periods {
224 let num_with_unit = p.to_text().to_string();
225 let split = num_with_unit.split(' ').collect::<Vec<&str>>();
226 record.push(
227 match split[1] {
228 "ns" => "nanosecond",
229 "µs" => "microsecond",
230 "ms" => "millisecond",
231 "sec" => "second",
232 "min" => "minute",
233 "hr" => "hour",
234 "day" => "day",
235 "wk" => "week",
236 _ => "unknown",
237 },
238 Value::int(split[0].parse().unwrap_or(0), span),
239 );
240 }
241
242 record.push(
243 "sign",
244 Value::string(if sign == -1 { "-" } else { "+" }, span),
245 );
246
247 Value::record(record, span)
248}
249
250#[cfg(test)]
251mod tests {
252 use super::*;
253 use crate::semver::value::SemverValue;
254
255 fn create_semver_value(version: &str) -> Value {
256 let semver = SemverValue::new(semver::Version::parse(version).unwrap());
257 Value::custom(Box::new(semver), Span::test_data())
258 }
259
260 #[test]
261 fn test_parse_semver_into_record_basic() {
262 let semver_val = SemverValue::new(semver::Version::parse("1.2.3").unwrap());
263 let result = parse_semver_into_record(&semver_val, Span::test_data());
264
265 match result {
266 Value::Record { val, .. } => {
267 assert_eq!(val.get("major").unwrap().as_int().unwrap(), 1);
268 assert_eq!(val.get("minor").unwrap().as_int().unwrap(), 2);
269 assert_eq!(val.get("patch").unwrap().as_int().unwrap(), 3);
270 assert_eq!(val.get("pre").unwrap().as_str().unwrap(), "");
271 assert_eq!(val.get("build").unwrap().as_str().unwrap(), "");
272
273 let pre_identifiers = val.get("pre_identifiers").unwrap().as_list().unwrap();
274 assert_eq!(pre_identifiers.len(), 0);
275
276 let build_identifiers = val.get("build_identifiers").unwrap().as_list().unwrap();
277 assert_eq!(build_identifiers.len(), 0);
278 }
279 _ => panic!("Expected Record value"),
280 }
281 }
282
283 #[test]
284 fn test_parse_semver_into_record_with_prerelease() {
285 let semver_val = SemverValue::new(semver::Version::parse("1.2.3-alpha.1").unwrap());
286 let result = parse_semver_into_record(&semver_val, Span::test_data());
287
288 match result {
289 Value::Record { val, .. } => {
290 assert_eq!(val.get("pre").unwrap().as_str().unwrap(), "alpha.1");
291
292 let pre_identifiers = val.get("pre_identifiers").unwrap().as_list().unwrap();
293 assert_eq!(pre_identifiers.len(), 2);
294 assert_eq!(pre_identifiers[0].as_str().unwrap(), "alpha");
295 assert_eq!(pre_identifiers[1].as_int().unwrap(), 1);
296 }
297 _ => panic!("Expected Record value"),
298 }
299 }
300
301 #[test]
302 fn test_parse_semver_into_record_with_build() {
303 let semver_val = SemverValue::new(semver::Version::parse("1.2.3+build.2").unwrap());
304 let result = parse_semver_into_record(&semver_val, Span::test_data());
305
306 match result {
307 Value::Record { val, .. } => {
308 assert_eq!(val.get("build").unwrap().as_str().unwrap(), "build.2");
309
310 let build_identifiers = val.get("build_identifiers").unwrap().as_list().unwrap();
311 assert_eq!(build_identifiers.len(), 2);
312 assert_eq!(build_identifiers[0].as_str().unwrap(), "build");
313 assert_eq!(build_identifiers[1].as_int().unwrap(), 2);
314 }
315 _ => panic!("Expected Record value"),
316 }
317 }
318
319 #[test]
320 fn test_parse_semver_into_record_with_both() {
321 let semver_val = SemverValue::new(semver::Version::parse("1.2.3-alpha.1+build.2").unwrap());
322 let result = parse_semver_into_record(&semver_val, Span::test_data());
323
324 match result {
325 Value::Record { val, .. } => {
326 assert_eq!(val.get("major").unwrap().as_int().unwrap(), 1);
327 assert_eq!(val.get("minor").unwrap().as_int().unwrap(), 2);
328 assert_eq!(val.get("patch").unwrap().as_int().unwrap(), 3);
329 assert_eq!(val.get("pre").unwrap().as_str().unwrap(), "alpha.1");
330 assert_eq!(val.get("build").unwrap().as_str().unwrap(), "build.2");
331
332 let pre_identifiers = val.get("pre_identifiers").unwrap().as_list().unwrap();
333 assert_eq!(pre_identifiers.len(), 2);
334
335 let build_identifiers = val.get("build_identifiers").unwrap().as_list().unwrap();
336 assert_eq!(build_identifiers.len(), 2);
337 }
338 _ => panic!("Expected Record value"),
339 }
340 }
341
342 #[test]
343 fn test_into_record_with_semver() {
344 let semver_val = create_semver_value("1.2.3");
345 let semver_ref = match &semver_val {
346 Value::Custom { val, .. } => val.as_any().downcast_ref::<SemverValue>().unwrap(),
347 _ => panic!("Expected Custom value"),
348 };
349 let result = parse_semver_into_record(semver_ref, Span::test_data());
350
351 match result {
352 Value::Record { val, .. } => {
353 assert_eq!(val.get("major").unwrap().as_int().unwrap(), 1);
354 assert_eq!(val.get("minor").unwrap().as_int().unwrap(), 2);
355 assert_eq!(val.get("patch").unwrap().as_int().unwrap(), 3);
356 }
357 _ => panic!("Expected Record value"),
358 }
359 }
360}
361
362fn parse_semver_into_record(semver: &SemverValue, span: Span) -> Value {
363 let version = &semver.version;
364
365 let pre_identifiers: Vec<Value> = if version.pre.is_empty() {
366 Vec::new()
367 } else {
368 version
369 .pre
370 .split('.')
371 .map(|id| {
372 if let Ok(num) = id.parse::<i64>() {
373 Value::int(num, span)
374 } else {
375 Value::string(id.to_string(), span)
376 }
377 })
378 .collect()
379 };
380
381 let build_identifiers: Vec<Value> = if version.build.is_empty() {
382 Vec::new()
383 } else {
384 version
385 .build
386 .split('.')
387 .map(|id| {
388 if let Ok(num) = id.parse::<i64>() {
389 Value::int(num, span)
390 } else {
391 Value::string(id.to_string(), span)
392 }
393 })
394 .collect()
395 };
396
397 Value::record(
398 record! {
399 "major" => Value::int(version.major as i64, span),
400 "minor" => Value::int(version.minor as i64, span),
401 "patch" => Value::int(version.patch as i64, span),
402 "pre" => Value::string(version.pre.to_string(), span),
403 "build" => Value::string(version.build.to_string(), span),
404 "pre_identifiers" => Value::list(pre_identifiers, span),
405 "build_identifiers" => Value::list(build_identifiers, span),
406 },
407 span,
408 )
409}
410
411#[cfg(test)]
412mod test {
413 use super::*;
414
415 #[test]
416 fn test_examples() -> nu_test_support::Result {
417 nu_test_support::test().examples(IntoRecord)
418 }
419}