parsm 0.8.3

Multi-format data processor that understands structured text better than sed or awk. Supports JSON, CSV, YAML, TOML, logfmt, and plain text with powerful filtering and templating.
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use std::fs::File;
use std::io::Write;
use std::process::{Command, Stdio};
use tempfile::NamedTempFile;

/// Helper function to create a Command with proper environment setup
fn parsm_command() -> Command {
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_parsm"));
    cmd.env("RUST_LOG", "parsm=error");
    cmd
}

#[test]
fn test_text_field_selection() {
    // Test basic field selection from text (word selection by index)
    let input = "hello world test\nquick brown fox\njumps over lazy";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("word_0") // First word
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 3);
    assert_eq!(lines[0], "hello");
    assert_eq!(lines[1], "quick");
    assert_eq!(lines[2], "jumps");
}

#[test]
fn test_text_array_access() {
    // Test accessing text as an array with numeric indices
    let input = "alpha beta gamma delta\none two three four";

    let mut child = parsm_command()
        .arg("word_2") // Third element (zero-indexed)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn parsm");

    let stdin = child.stdin.take().expect("get stdin");
    let mut stdin = stdin;
    write!(stdin, "{input}").expect("write to stdin");
    drop(stdin);

    let output = child.wait_with_output().expect("wait for parsm");
    assert!(output.status.success(), "parsm failed: {output:?}");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 2);
    assert_eq!(lines[0], "gamma");
    assert_eq!(lines[1], "three");
}

#[test]
fn test_text_template_rendering() {
    // Test template rendering with text data
    let input = "John 30 Engineer\nJane 25 Designer\nBob 35 Manager";

    let mut child = parsm_command()
        .arg("{$word_0 $word_1 $word_2}")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn parsm");

    let stdin = child.stdin.take().expect("get stdin");
    let mut stdin = stdin;
    write!(stdin, "{input}").expect("write to stdin");
    drop(stdin);

    let output = child.wait_with_output().expect("wait for parsm");
    assert!(output.status.success(), "parsm failed: {output:?}");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 3);
    assert_eq!(lines[0], "John 30 Engineer");
    assert_eq!(lines[1], "Jane 25 Designer");
    assert_eq!(lines[2], "Bob 35 Manager");
}

#[test]
fn test_text_dollar_template_syntax() {
    // Test $variable template syntax with text
    let input = "error connection timeout\ninfo server started\nwarn high memory";

    let mut child = parsm_command()
        .arg("{$word_0 $word_1 $word_2}")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn parsm");

    let stdin = child.stdin.take().expect("get stdin");
    let mut stdin = stdin;
    write!(stdin, "{input}").expect("write to stdin");
    drop(stdin);

    let output = child.wait_with_output().expect("wait for parsm");
    assert!(output.status.success(), "parsm failed: {output:?}");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 3);
    assert_eq!(lines[0], "error connection timeout");
    assert_eq!(lines[1], "info server started");
    assert_eq!(lines[2], "warn high memory");
}

#[test]
fn test_text_filter_operations() {
    // Test filtering text entries
    let input =
        "error connection failed\ninfo server started\nerror database timeout\nwarn memory high";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    // Test filtering by first word
    let output = parsm_command()
        .arg("word_0 == \"error\"")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 2);
    assert!(lines[0].contains("error connection failed"));
    assert!(lines[1].contains("error database timeout"));
}

#[test]
fn test_text_filter_with_template() {
    // Test combined filter and template with text
    let input = "error connection failed retry\ninfo server started successfully\nerror database timeout critical\nwarn memory high alert";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    // Filter error entries and format them
    let output = parsm_command()
        .arg("word_0 == \"error\" {ERROR: $word_1 $word_2}")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 2);
    assert_eq!(lines[0], "ERROR: connection failed");
    assert_eq!(lines[1], "ERROR: database timeout");
}

#[test]
fn test_text_format_detection() {
    // Test that text is correctly detected and parsed
    let input = "this is plain text without special formatting";

    let mut child = parsm_command()
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn parsm");

    let stdin = child.stdin.take().expect("get stdin");
    let mut stdin = stdin;
    write!(stdin, "{input}").expect("write to stdin");
    drop(stdin);

    let output = child.wait_with_output().expect("wait for parsm");
    assert!(output.status.success(), "parsm failed: {output:?}");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 1);

    // Now we expect the original input as output instead of JSON
    assert_eq!(lines[0], input.trim());
}

#[test]
fn test_text_empty_lines() {
    // Test handling of empty lines and whitespace
    let input = "hello world\n\n   \nfoo bar\n";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("word_0")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    // Empty lines should be skipped
    assert_eq!(lines.len(), 2);
    assert_eq!(lines[0], "hello");
    assert_eq!(lines[1], "foo");
}

#[test]
fn test_text_single_word() {
    // Test handling of lines with a single word
    let input = "hello\nworld\ntest";

    let mut child = parsm_command()
        .arg("word_0")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn parsm");

    let stdin = child.stdin.take().expect("get stdin");
    let mut stdin = stdin;
    write!(stdin, "{input}").expect("write to stdin");
    drop(stdin);

    let output = child.wait_with_output().expect("wait for parsm");
    assert!(output.status.success(), "parsm failed: {output:?}");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 3);
    assert_eq!(lines[0], "hello");
    assert_eq!(lines[1], "world");
    assert_eq!(lines[2], "test");
}

#[test]
fn test_text_multiple_spaces() {
    // Test handling of multiple spaces between words
    let input = "word1    word2     word3\nalpha  beta   gamma";

    let mut child = parsm_command()
        .arg("{$word_0-$word_1-$word_2}")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn parsm");

    let stdin = child.stdin.take().expect("get stdin");
    let mut stdin = stdin;
    write!(stdin, "{input}").expect("write to stdin");
    drop(stdin);

    let output = child.wait_with_output().expect("wait for parsm");
    assert!(output.status.success(), "parsm failed: {output:?}");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 2);
    assert_eq!(lines[0], "word1-word2-word3");
    assert_eq!(lines[1], "alpha-beta-gamma");
}

#[test]
fn test_text_nonexistent_field() {
    // Test accessing non-existent fields
    let input = "one two\nthree four";

    let mut child = parsm_command()
        .arg("word_5") // Word that doesn't exist
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn parsm");

    let stdin = child.stdin.take().expect("get stdin");
    let mut stdin = stdin;
    write!(stdin, "{input}").expect("write to stdin");
    drop(stdin);

    let output = child.wait_with_output().expect("wait for parsm");
    assert!(output.status.success(), "parsm failed: {output:?}");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should produce warnings but not crash
    assert!(stdout.trim().is_empty() || !output.stderr.is_empty());
}

#[test]
fn test_text_original_input_template() {
    // Test accessing the original input line
    let input = "first line of text\nsecond line here";

    let mut child = parsm_command()
        .arg("{${0}}")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn parsm");

    let stdin = child.stdin.take().expect("get stdin");
    let mut stdin = stdin;
    write!(stdin, "{input}").expect("write to stdin");
    drop(stdin);

    let output = child.wait_with_output().expect("wait for parsm");
    assert!(output.status.success(), "parsm failed: {output:?}");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 2);
    assert_eq!(lines[0], "first line of text");
    assert_eq!(lines[1], "second line here");
}

#[test]
fn test_text_complex_filtering() {
    // Test complex filtering with logical operators
    let input = "error network timeout critical\ninfo server started normal\nerror disk full critical\nwarn memory high normal\ninfo backup completed normal";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    // Filter for errors that are critical
    let output = parsm_command()
        .arg("word_0 == \"error\" && word_3 == \"critical\"")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 2);
    assert!(lines[0].contains("error network timeout critical"));
    assert!(lines[1].contains("error disk full critical"));
}

#[test]
fn test_text_error_handling() {
    // Test handling of malformed input that doesn't prevent processing
    let input = "good line one\n\nbad line with weird characters: @#$%\ngood line two";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let output = parsm_command()
        .arg("word_0")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    // Should succeed and process all lines (text format is very permissive)
    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    // Should have output from all non-empty lines
    assert_eq!(lines.len(), 3);
    assert_eq!(lines[0], "good");
    assert_eq!(lines[1], "bad");
    assert_eq!(lines[2], "good");
}

#[test]
fn test_text_performance_large_dataset() {
    // Test performance with a larger text dataset
    let mut input = String::new();
    for i in 1..=100 {
        input.push_str(&format!("entry {i} processing data item number {i}\n"));
    }

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    let start_time = std::time::Instant::now();
    let output = parsm_command()
        .arg("word_1") // Second word
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");
    let duration = start_time.elapsed();

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 100);
    assert_eq!(lines[0], "1");
    assert_eq!(lines[99], "100");

    // Should process reasonably quickly
    assert!(
        duration.as_millis() < 1000,
        "Processing took too long: {duration:?}"
    );
}

#[test]
fn test_text_string_operations() {
    // Test string operations like contains
    let input = "user alice logged in\nuser bob failed login\nuser charlie logged out\nadmin alice system check";

    let mut file = NamedTempFile::new().expect("create temp file");
    write!(file, "{input}").expect("write temp file");

    // Filter for lines containing "alice"
    let output = parsm_command()
        .arg("word_1 == \"alice\"")
        .stdin(File::open(file.path()).unwrap())
        .output()
        .expect("run parsm");

    assert!(output.status.success(), "parsm failed: {output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 2);
    assert!(lines[0].contains("user alice logged in"));
    assert!(lines[1].contains("admin alice system check"));
}

#[test]
fn test_text_mixed_content() {
    // Test text format with varied content (numbers, punctuation, etc.)
    let input = "Item-123 Processing at 14.30.45\nWarning Memory usage 85%\nStatus=OK Count=42";

    let mut child = parsm_command()
        .arg("{$word_0 -> $word_1}")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn parsm");

    let stdin = child.stdin.take().expect("get stdin");
    let mut stdin = stdin;
    write!(stdin, "{input}").expect("write to stdin");
    drop(stdin);

    let output = child.wait_with_output().expect("wait for parsm");
    assert!(output.status.success(), "parsm failed: {output:?}");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = stdout.trim().split('\n').collect();

    assert_eq!(lines.len(), 3);
    assert_eq!(lines[0], "Item-123 -> Processing");
    assert_eq!(lines[1], "Warning -> Memory");
    assert_eq!(lines[2], "Status=OK -> Count=42");
}

/// Test text forced format detection with --text flag
#[test]
fn test_text_forced_format() {
    let test_cases = vec![
        // Space-separated text
        ("Alice 30 Engineer", r#""word_0""#, "Alice"),
        ("Alice 30 Engineer", r#""word_1""#, "30"),
        ("Alice 30 Engineer", r#""word_2""#, "Engineer"),
        // Text that might look like other formats but forced as text
        ("hello world", r#""word_0""#, "hello"),
        ("hello world", r#""word_1""#, "world"),
        // Text with colons that might be mistaken for other formats
        ("name: Alice age: 30", r#""word_0""#, "name:"),
        ("name: Alice age: 30", r#""word_1""#, "Alice"),
        ("name: Alice age: 30", r#""word_2""#, "age:"),
        ("name: Alice age: 30", r#""word_3""#, "30"),
        // Test template with forced text
        (
            "Alice 30 Engineer",
            r#"{Name: ${word_0}, Age: ${word_1}}"#,
            "Name: Alice, Age: 30",
        ),
        (
            "Hello world test",
            r#"{${word_0} ${word_2}!}"#,
            "Hello test!",
        ),
        (
            "name: Alice age: 30",
            r#"{${word_1} is ${word_3} years old}"#,
            "Alice is 30 years old",
        ),
    ];

    for (input, expression, expected) in test_cases {
        let mut child = parsm_command()
            .arg("--text")
            .arg(expression)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("Failed to start parsm");

        {
            let stdin = child.stdin.as_mut().expect("Failed to open stdin");
            stdin
                .write_all(input.as_bytes())
                .expect("Failed to write to stdin");
        }

        let output = child.wait_with_output().expect("Failed to read stdout");
        assert!(
            output.status.success(),
            "Text forced format failed for input '{}' with expression '{}': {:?}",
            input,
            expression,
            String::from_utf8_lossy(&output.stderr)
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        let result = stdout.trim();
        assert_eq!(
            result, expected,
            "Failed for text forced input '{input}' with expression '{expression}'",
        );
    }
}

/// Test text forced format filtering with --text flag
#[test]
fn test_text_forced_format_filtering() {
    let test_cases = vec![
        ("Alice 30 Engineer", r#"word_0 == "Alice""#, true),
        ("Bob 25 Student", r#"word_0 == "Alice""#, false),
        ("Alice 30 Engineer", r#"word_2 *= "Eng""#, true),
        ("Bob 25 Student", r#"word_2 *= "Eng""#, false),
        ("count 100 items", r#"word_1 == "100""#, true),
        ("count 50 items", r#"word_1 == "100""#, false),
    ];

    for (input, filter, should_match) in test_cases {
        let mut child = parsm_command()
            .arg("--text")
            .arg(filter)
            .arg(r#"{match}"#)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("Failed to start parsm");

        {
            let stdin = child.stdin.as_mut().expect("Failed to open stdin");
            stdin
                .write_all(input.as_bytes())
                .expect("Failed to write to stdin");
        }

        let output = child.wait_with_output().expect("Failed to read stdout");
        assert!(
            output.status.success(),
            "Text forced format filtering failed for input '{}' with filter '{}': {:?}",
            input,
            filter,
            String::from_utf8_lossy(&output.stderr)
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        let result = stdout.trim();

        if should_match {
            assert_eq!(
                result, "match",
                "Expected match for text forced filter '{filter}' with input '{input}'",
            );
        } else {
            assert_eq!(
                result, "",
                "Expected empty output for text forced filter '{filter}' with input '{input}'",
            );
        }
    }
}