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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
pub mod errors;
pub mod stats;

use crate::io_helpers::buf_reader::get_bufreader;
use crate::json::paths::ValuePaths;
use crate::json::{Value, ValueType};
use crate::{io_helpers, Cli, Settings};

use self::errors::collection::{
    Errors, ErrorsPar, IndexedNDJSONError, IntoEnumeratedErrFiltered, IntoErrFiltered,
    NDJSONProcessingErrors,
};
use self::errors::NDJSONError;
pub use self::stats::{FileStats, Stats};

use dashmap::DashMap;
use indicatif::{ProgressBar, ProgressStyle};
use rayon::iter::ParallelBridge;
use rayon::prelude::ParallelIterator;

use std::error::Error;
use std::fs::File;
use std::io::{self, prelude::*};
use std::iter::Zip;
use std::ops::RangeFrom;
use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::Receiver;

// Reusable types for function signatures
type IJSONCandidate = (usize, String);
type IdJSON = (String, Value);
type IdJSONIter<'a> = Box<dyn Iterator<Item = IdJSON> + 'a>;
type NDJSONErrors = Errors<IndexedNDJSONError>;
type NDJSONErrorsPar = ErrorsPar<IndexedNDJSONError>;

trait Indexed: Iterator {
    fn indexed(self) -> Zip<RangeFrom<usize>, Self>
    where
        Self: Sized,
    {
        (1usize..).zip(self)
    }
}

impl<T> Indexed for T where T: Iterator {}

// TODO: add or switch to method on `Receiver<String>`?
/// Indexes data from the mpsc channel, converts it to serde JSON `Value`s and filters out data that does not
/// parse as JSON to the `errors` container. Single threaded.
///
/// See also: [`parse_ndjson_receiver_par`]
pub fn parse_ndjson_receiver<'a>(
    _args: &Cli,
    receiver: Receiver<String>,
    errors: &NDJSONErrors,
) -> Result<IdJSONIter<'a>, Box<dyn Error>> {
    let json_iter = receiver
        .into_iter()
        .indexed()
        .map(|(i, json_candidate)| {
            (
                i.to_string(),
                serde_json::from_str::<Value>(&json_candidate),
            )
        })
        .to_err_filtered(errors.new_ref());

    Ok(Box::new(json_iter))
}

// TODO: add or switch to method on `Receiver<String>`?
/// Indexes data from the mpsc channel, converts it to serde JSON `Value`s and filters out data that does not
/// parse as JSON to the `errors` container. Multithreaded version of [`parse_ndjson_receiver`].
///
/// See also: [`parse_ndjson_receiver`], [`parse_ndjson_bufreader_par`] & [`parse_ndjson_iter_par`]
pub fn parse_ndjson_receiver_par<'a>(
    args: &Cli,
    receiver: Receiver<String>,
    errors: &'a NDJSONErrorsPar,
) -> impl ParallelIterator<Item = IdJSON> + 'a {
    let receiver = receiver.into_iter().indexed();
    parse_ndjson_iter_par(args, receiver, errors)
}

// TODO: rename or switch function args?
// TODO: add or switch to method on `&PathBuf`?
/// Indexes data from the file_path with a bufreader, converts it to serde JSON `Value`s
/// and filters out data that does not
/// parse as JSON to the `errors` container. Multithreaded version of [`parse_ndjson_bufreader`].
///
/// See also: [`parse_ndjson_bufreader`], [`parse_ndjson_receiver_par`] & [`parse_ndjson_iter_par`]
pub fn parse_ndjson_bufreader_par<'a>(
    args: &Cli,
    file_path: &PathBuf,
    errors: &'a NDJSONErrorsPar,
) -> Result<impl ParallelIterator<Item = IdJSON> + 'a, NDJSONError> {
    let reader = get_bufreader(args, file_path)?;

    let iter = reader.lines().enumerate();
    let iter = iter.filter_map(|(i, line)| {
        let i = i + 1; // count lines from 1
        let io_errors = errors.new_ref();
        match line {
            Err(e) => {
                io_errors.push(IndexedNDJSONError {
                    location: i.to_string(),
                    error: NDJSONError::IOError(e),
                });
                None
            }
            Ok(json) => Some((i, json)),
        }
    });

    Ok(parse_ndjson_iter_par(args, iter, errors))
}

// https://github.com/rayon-rs/rayon/issues/628
// https://users.rust-lang.org/t/how-to-wrap-a-non-object-safe-trait-in-an-object-safe-one/33904
/// Processes indexed data from the Iterator, converts it to serde JSON `Value`s
/// and filters out data that does not parse as JSON to the `errors` container.
///
/// See also: [`parse_ndjson_receiver_par`] & [`parse_ndjson_bufreader_par`]
pub fn parse_ndjson_iter_par<'a>(
    args: &Cli,
    iter: impl Iterator<Item = IJSONCandidate> + Send + 'a,
    errors: &'a NDJSONErrorsPar,
) -> impl ParallelIterator<Item = IdJSON> + 'a {
    let iter = iter.take(args.lines.unwrap_or(usize::MAX));

    let json_iter = iter.par_bridge().map(|(i, json_candidate)| {
        (
            i.to_string(),
            serde_json::from_str::<Value>(&json_candidate),
        )
    });

    json_iter.filter_map(|(id, json)| {
        let json_parse_errors = errors.new_ref();
        match json {
            Err(e) => {
                json_parse_errors.push(IndexedNDJSONError {
                    location: id,
                    error: NDJSONError::JSONParsingError(e),
                });
                None
            }
            Ok(json) => Some((id, json)),
        }
    })
}

/// Indexes data from the bufreader, converts it to serde JSON `Value`s
/// and filters out data that does not
/// parse as JSON to the `errors` container. Single threaded version of [`parse_ndjson_bufreader_par`].
///
/// See also: [`parse_ndjson_bufreader_par`], [`parse_ndjson_file`], [`parse_ndjson_file_path`] & [`parse_ndjson_receiver`]
pub fn parse_ndjson_bufreader<'a>(
    _args: &Cli,
    reader: impl BufRead + 'a,
    errors: &NDJSONErrors,
) -> IdJSONIter<'a> {
    let json_iter = reader.lines();

    let json_iter = json_iter.to_enumerated_err_filtered(errors.new_ref());

    let json_iter = json_iter.map(|(i, json_candidate)| {
        (
            i.to_string(),
            serde_json::from_str::<Value>(&json_candidate),
        )
    });
    let json_iter = json_iter.to_err_filtered(errors.new_ref());

    Box::new(json_iter)
}

/// Indexes data from the file, converts it to serde JSON `Value`s
/// and filters out data that does not
/// parse as JSON to the `errors` container. Single threaded.
///
/// See also: [`parse_ndjson_bufreader`], [`parse_ndjson_file_path`] & [`parse_ndjson_receiver`]
pub fn parse_ndjson_file<'a>(args: &Cli, file: File, errors: &NDJSONErrors) -> IdJSONIter<'a> {
    let reader = io::BufReader::new(file);
    parse_ndjson_bufreader(args, reader, errors)
}

/// Indexes data from the file_path, converts it to serde JSON `Value`s
/// and filters out data that does not
/// parse as JSON to the `errors` container. Single threaded.
///
/// See also: [`parse_ndjson_bufreader`], [`parse_ndjson_file`] & [`parse_ndjson_receiver`]
pub fn parse_ndjson_file_path<'a>(
    args: &Cli,
    file_path: &PathBuf,
    errors: &NDJSONErrors,
) -> Result<IdJSONIter<'a>, NDJSONError> {
    let reader = get_bufreader(args, file_path)?;
    Ok(parse_ndjson_bufreader(args, reader, errors))
}

/// Handles the jsonpath query expansion of the Iterators values. Single threaded
///
/// See also [`expand_jsonpath_query_par`]
pub fn expand_jsonpath_query<'a>(
    settings: &'a Settings,
    json_iter: impl Iterator<Item = IdJSON> + 'a,
    errors: &NDJSONErrors,
) -> IdJSONIter<'a> {
    let missing = errors.new_ref();
    let json_iter_out: IdJSONIter<'a>;
    if let Some(ref selector) = settings.jsonpath_selector {
        let path = settings.args.jsonpath.to_owned();
        let path = path.expect("must exist for jsonpath_selector to exist");
        let expanded = json_iter.flat_map(move |(ref id, ref json)| {
            let selected = selector.query(json);
            if selected.is_empty() {
                missing.push(IndexedNDJSONError::new(
                    id.to_owned(),
                    NDJSONError::EmptyQuery,
                ))
            }
            selected
                .into_iter()
                .enumerate()
                .map(|(i, json)| (format!("{id}:{path}[{i}]"), json.to_owned()))
                .collect::<Vec<_>>()
        });
        json_iter_out = Box::new(expanded);
    } else {
        json_iter_out = Box::new(json_iter);
    }
    json_iter_out
}

/// Handles the jsonpath query expansion of the Iterators values. Multi-threaded.
///
/// See also [`expand_jsonpath_query`]
pub fn expand_jsonpath_query_par<'a>(
    settings: &'a Settings,
    json_iter: impl ParallelIterator<Item = IdJSON> + 'a,
    errors: &NDJSONErrorsPar,
) -> impl ParallelIterator<Item = IdJSON> + 'a {
    let missing = errors.new_ref();

    json_iter.flat_map(move |(id, json)| {
        if let Some(ref selector) = settings.jsonpath_selector {
            let path = settings.args.jsonpath.to_owned();
            let path = path.expect("must exist for jsonpath_selector to exist");

            let selected = selector.query(&json);
            if selected.is_empty() {
                missing.push(IndexedNDJSONError::new(
                    id.to_owned(),
                    NDJSONError::EmptyQuery,
                ))
            }
            selected
                .into_iter()
                .enumerate()
                .map(|(i, json)| (format!("{id}:{path}[{i}]"), json.to_owned()))
                .collect::<Vec<_>>()
        } else {
            vec![(id, json)]
        }
    })
}

/// Apply pre-processing based on settings from CLI args. Single threaded.
///
/// See also [`apply_settings_par`]
pub fn apply_settings<'a>(
    settings: &'a Settings,
    json_iter: impl Iterator<Item = IdJSON> + 'a,
    errors: &NDJSONErrors,
) -> IdJSONIter<'a> {
    let args = &settings.args;

    let json_iter = limit(args, json_iter);
    expand_jsonpath_query(settings, json_iter, errors)
}

/// Apply pre-processing based on settings from CLI args. Multi-threaded.
///
/// See also [`apply_settings`]
pub fn apply_settings_par<'a>(
    settings: &'a Settings,
    json_iter: impl ParallelIterator<Item = IdJSON> + 'a,
    errors: &NDJSONErrorsPar,
) -> impl ParallelIterator<Item = IdJSON> + 'a {
    expand_jsonpath_query_par(settings, json_iter, errors)
}

/// Main function processing the JSON data, collecting key infomation about the content.
/// Single threaded.
///
/// See also [`process_json_iterable_par`]
pub fn process_json_iterable(
    settings: &Settings,
    json_iter: impl Iterator<Item = IdJSON>,
    errors: &NDJSONErrors,
) -> Stats {
    let mut fs = Stats::new();
    let args = &settings.args;

    let json_iter = apply_settings(settings, json_iter, errors);

    let spinner = ProgressBar::new_spinner().with_style(
        ProgressStyle::with_template("{spinner} {elapsed_precise} Lines: {pos:>10}\t{per_sec}\n")
            .unwrap(),
    );

    for (_id, json) in json_iter {
        spinner.inc(1);
        fs.line_count += 1;

        for value_path in json.value_paths(args.explode_arrays, args.inspect_arrays) {
            let path = value_path.jsonpath();
            let counter = fs.keys_count.entry(path.to_owned()).or_insert(0);
            *counter += 1;

            let type_ = value_path.value.value_type();
            let path_type = format!("{}::{}", path, type_);
            let counter = fs.keys_types_count.entry(path_type).or_insert(0);
            *counter += 1;
        }
    }
    spinner.finish();

    for indexed_error in errors.container.borrow().as_slice() {
        let IndexedNDJSONError { location, error } = indexed_error;
        let location = location.to_owned();
        match error {
            // TODO: use or syntax here?
            NDJSONError::JSONParsingError(_) => fs.bad_lines.push(location),
            NDJSONError::EmptyQuery => fs.empty_lines.push(location),
            NDJSONError::IOError(_) => fs.bad_lines.push(location),
        }
    }
    fs
}

/// Main function processing the JSON data, collecting key infomation about the content.
/// Mulit-threaded version of [`process_json_iterable`].
///
/// See also [`process_json_iterable_par`]
pub fn process_json_iterable_par<'a>(
    settings: &Settings,
    json_iter: impl ParallelIterator<Item = IdJSON> + 'a,
    errors: &'a NDJSONErrorsPar,
) -> Stats {
    let mut fs = Stats::new();
    let args = &settings.args;

    let keys_count: DashMap<String, usize> = DashMap::new();
    let keys_types_count: DashMap<String, usize> = DashMap::new();
    let line_count = AtomicUsize::new(0);

    let json_iter = apply_settings_par(settings, json_iter, errors);

    let spinner = ProgressBar::new_spinner().with_style(
        ProgressStyle::with_template("{spinner} {elapsed_precise} Lines: {pos:>10}\t{per_sec}\n")
            .unwrap(),
    );

    json_iter.for_each(|(_id, json)| {
        line_count.fetch_add(1, Ordering::Release);

        for value_path in json.value_paths(args.explode_arrays, args.inspect_arrays) {
            let path = value_path.jsonpath();
            let mut counter = keys_count.entry(path.to_owned()).or_insert(0);
            *counter.value_mut() += 1;

            let type_ = value_path.value.value_type();
            let path_type = format!("{}::{}", path, type_);
            let mut counter = keys_types_count.entry(path_type).or_insert(0);
            *counter.value_mut() += 1;
        }
        spinner.inc(1);
    });

    spinner.finish();

    for indexed_error in errors.container.lock().unwrap().as_slice() {
        let IndexedNDJSONError { location, error } = indexed_error;
        let location = location.to_owned();
        match error {
            NDJSONError::JSONParsingError(_) => fs.bad_lines.push(location),
            NDJSONError::EmptyQuery => fs.empty_lines.push(location),
            NDJSONError::IOError(_) => fs.bad_lines.push(location),
        }
    }

    fs.keys_count = keys_count
        .into_read_only()
        .iter()
        .map(|(k, v)| (k.to_owned(), v.to_owned()))
        .collect();
    fs.line_count = line_count.load(Ordering::Acquire);
    fs.keys_types_count = keys_types_count
        .into_read_only()
        .iter()
        .map(|(k, v)| (k.to_owned(), v.to_owned()))
        .collect();
    fs
}

/// Apply line limiting from the arg to the Iterator
///
/// See also [`parse_iter`]
pub fn limit<'a, I, T>(args: &Cli, iter: I) -> Box<dyn Iterator<Item = T> + 'a>
where
    I: Iterator<Item = T> + 'a,
{
    if let Some(n) = args.lines {
        Box::new(iter.take(n))
    } else {
        Box::new(iter)
    }
}

// TODO: Rename?
/// Early version of [`apply_settings`], kept as an example of alternative version of
/// [`limit`] that could be used without the need to `Box` the return value
///
/// See also [`limit`]
#[deprecated(note = "Superseded by `apply_settings`")]
pub fn parse_iter<E, I>(args: &Cli, iter: I) -> impl Iterator<Item = Result<String, E>>
where
    I: Iterator<Item = Result<String, E>>,
{
    if let Some(n) = args.lines {
        iter.take(n)
    } else {
        iter.take(usize::MAX)
    }
}

pub struct StatsResult {
    pub stats: Stats,
    pub errors: Box<dyn NDJSONProcessingErrors>,
}

pub trait JSONStats {
    fn json_stats(self, settings: &Settings) -> Result<StatsResult, NDJSONError>;
}

// TODO: Add tests
impl JSONStats for io::Stdin {
    fn json_stats(self, settings: &Settings) -> Result<StatsResult, NDJSONError> {
        let stats;
        let errors: Box<dyn NDJSONProcessingErrors>;
        if settings.args.parallel {
            let stdin = io_helpers::stdin::spawn_stdin_channel(self, 1_000_000);
            let _errors = ErrorsPar::default();
            let json_iter = parse_ndjson_receiver_par(&settings.args, stdin, &_errors);
            stats = process_json_iterable_par(settings, json_iter, &_errors);
            errors = Box::new(_errors);
        } else {
            let stdin = self.lock();
            let _errors = Errors::default();
            let json_iter = parse_ndjson_bufreader(&settings.args, stdin, &_errors);
            stats = process_json_iterable(settings, json_iter, &_errors);
            errors = Box::new(_errors);
        }
        Ok(StatsResult { stats, errors })
    }
}

impl JSONStats for &PathBuf {
    fn json_stats(self, settings: &Settings) -> Result<StatsResult, NDJSONError> {
        let stats;
        let errors: Box<dyn NDJSONProcessingErrors>;
        if settings.args.parallel {
            let _errors = ErrorsPar::default();
            let json_iter = parse_ndjson_bufreader_par(&settings.args, self, &_errors)?;
            stats = process_json_iterable_par(settings, json_iter, &_errors);
            errors = Box::new(_errors);
        } else {
            let _errors = Errors::default();
            let json_iter = parse_ndjson_file_path(&settings.args, self, &_errors)?;
            stats = process_json_iterable(settings, json_iter, &_errors);
            errors = Box::new(_errors);
        }
        Ok(StatsResult { stats, errors })
    }
}

#[cfg(test)]
mod tests {
    use crate::json::IndexMap;
    use serde_json::json;

    use super::*;
    use std::fs::File;
    use std::io::{Seek, SeekFrom, Write};

    // TODO: How to test stdin?

    #[test]
    fn line_read() {
        let mut tmpfile = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmpfile, r#"{{"key1": 123}}"#).unwrap();
        writeln!(tmpfile, r#"{{"key2": 123}}"#).unwrap();
        tmpfile.seek(SeekFrom::Start(0)).unwrap();
        let path = tmpfile.path().to_path_buf();

        let args = Cli::default();
        let buf_reader: Box<dyn BufRead> = get_bufreader(&args, &path).unwrap();
        let mut indexed = buf_reader.lines().indexed();

        let (i, s) = indexed.next().unwrap();
        assert_eq!(1, i);
        assert_eq!(r#"{"key1": 123}"#.to_string(), s.unwrap());
        let (i, s) = indexed.next().unwrap();
        assert_eq!(2, i);
        assert_eq!(r#"{"key2": 123}"#.to_string(), s.unwrap());

        assert!(indexed.next().is_none());
    }

    #[test]
    fn simple_json_stats() {
        let mut tmpfile = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmpfile, r#"{{"key1": 123}}"#).unwrap();
        writeln!(tmpfile, r#"{{"key2": 123}}"#).unwrap();
        writeln!(tmpfile, r#"{{"key1": 123}}"#).unwrap();
        tmpfile.seek(SeekFrom::Start(0)).unwrap();
        let path = tmpfile.path().to_path_buf();

        let expected = StatsResult {
            stats: Stats {
                keys_count: IndexMap::from([("$.key1".to_string(), 2), ("$.key2".to_string(), 1)]),
                line_count: 3,
                bad_lines: vec![],
                keys_types_count: IndexMap::from([
                    ("$.key1::Number".to_string(), 2),
                    ("$.key2::Number".to_string(), 1),
                ]),
                empty_lines: vec![],
            },
            errors: Box::new(Errors::<NDJSONError>::default()),
        };

        let args = Cli::default();
        let settings = Settings::init(args).unwrap();

        let actual = path.json_stats(&settings).unwrap();
        assert_eq!(expected.stats, actual.stats);
    }

    #[test]
    fn simple_json_stats_par() {
        let mut tmpfile = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmpfile, r#"{{"key1": 123}}"#).unwrap();
        writeln!(tmpfile, r#"{{"key2": 123}}"#).unwrap();
        writeln!(tmpfile, r#"{{"key1": 123}}"#).unwrap();
        tmpfile.seek(SeekFrom::Start(0)).unwrap();
        let path = tmpfile.path().to_path_buf();

        let expected = StatsResult {
            stats: Stats {
                keys_count: IndexMap::from([("$.key1".to_string(), 2), ("$.key2".to_string(), 1)]),
                line_count: 3,
                bad_lines: vec![],
                keys_types_count: IndexMap::from([
                    ("$.key1::Number".to_string(), 2),
                    ("$.key2::Number".to_string(), 1),
                ]),
                empty_lines: vec![],
            },
            errors: Box::new(Errors::<NDJSONErrorsPar>::default()),
        };

        let mut args = Cli::default();
        args.parallel = true;
        let settings = Settings::init(args).unwrap();

        let actual = path.json_stats(&settings).unwrap();
        assert_eq!(expected.stats, actual.stats);
    }

    #[test]
    fn simple_ndjson() {
        let mut tmpfile: File = tempfile::tempfile().unwrap();
        writeln!(tmpfile, r#"{{"key1": 123}}"#).unwrap();
        writeln!(tmpfile, r#"{{"key2": 123}}"#).unwrap();
        writeln!(tmpfile, r#"{{"key1": 123}}"#).unwrap();
        tmpfile.seek(SeekFrom::Start(0)).unwrap();
        let reader = io::BufReader::new(tmpfile);

        let expected: Vec<IdJSON> = vec![
            (1.to_string(), json!({"key1": 123})),
            (2.to_string(), json!({"key2": 123})),
            (3.to_string(), json!({"key1": 123})),
        ];

        let args = Cli::default();
        let errors = Errors::default();

        let json_iter = parse_ndjson_bufreader(&args, reader, &errors);
        assert_eq!(expected, json_iter.collect::<Vec<IdJSON>>());
        assert!(errors.container.borrow().is_empty())
    }

    #[test]
    fn bad_ndjson_file() {
        let mut tmpfile: File = tempfile::tempfile().unwrap();
        writeln!(tmpfile, r#"{{"key1": 123}}"#).unwrap();
        writeln!(tmpfile, r#"not valid json"#).unwrap();
        writeln!(tmpfile, r#"{{"key1": 123}}"#).unwrap();
        tmpfile.seek(SeekFrom::Start(0)).unwrap();
        let reader = io::BufReader::new(tmpfile);

        let expected: Vec<IdJSON> = vec![
            (1.to_string(), json!({"key1": 123})),
            (3.to_string(), json!({"key1": 123})),
        ];

        let args = Cli::default();
        let errors = Errors::default();

        let json_iter = parse_ndjson_bufreader(&args, reader, &errors);
        assert_eq!(expected, json_iter.collect::<Vec<IdJSON>>());
        assert!(errors.container.borrow().len() == 1)
    }

    #[test]
    fn simple_expand_jsonpath_query() {
        let json_iter_in: Vec<IdJSON> = vec![
            (1.to_string(), json!({"key1": [1, 2, 3]})),
            (2.to_string(), json!({"key2": 123})),
            (3.to_string(), json!({"key1": [4, 5]})),
        ];
        let json_iter_in = json_iter_in.iter().cloned();

        let mut args = Cli::default();
        args.jsonpath = Some("$.key1[*]".to_string());
        let settings = Settings::init(args).unwrap();
        let errors = Errors::default();

        let expected: Vec<IdJSON> = vec![
            ("1:$.key1[*][0]".to_string(), json!(1)),
            ("1:$.key1[*][1]".to_string(), json!(2)),
            ("1:$.key1[*][2]".to_string(), json!(3)),
            ("3:$.key1[*][0]".to_string(), json!(4)),
            ("3:$.key1[*][1]".to_string(), json!(5)),
        ];

        let json_iter = expand_jsonpath_query(&settings, json_iter_in, &errors);
        assert_eq!(expected, json_iter.collect::<Vec<IdJSON>>());
        assert!(errors.container.borrow().len() == 1)
    }

    #[test]
    fn simple_process_json_iterable() {
        let json_iter_in: Vec<IdJSON> = vec![
            (1.to_string(), json!({"key1": 123})),
            (2.to_string(), json!({"key2": 123})),
            (3.to_string(), json!({"key1": 123})),
        ];
        let json_iter_in = json_iter_in.iter().cloned();

        let args = Cli::default();
        let settings = Settings::init(args).unwrap();
        let errors = Errors::default();

        let expected = Stats {
            keys_count: IndexMap::from([("$.key1".to_string(), 2), ("$.key2".to_string(), 1)]),
            line_count: 3,
            keys_types_count: IndexMap::from([
                ("$.key1::Number".to_string(), 2),
                ("$.key2::Number".to_string(), 1),
            ]),
            ..Default::default()
        };

        let stats = process_json_iterable(&settings, json_iter_in, &errors);
        assert_eq!(expected, stats);
        assert!(errors.container.borrow().is_empty())
    }

    #[test]
    fn bad_process_json_iterable_path_query() {
        let json_iter_in: Vec<IdJSON> = vec![
            (1.to_string(), json!({"key1": 123})),
            (2.to_string(), json!({"key2": 123})),
            (3.to_string(), json!({"key1": 123})),
        ];
        let json_iter_in = json_iter_in.iter().cloned();

        let mut args = Cli::default();
        args.jsonpath = Some("$.key1".to_string());
        let settings = Settings::init(args).unwrap();
        let errors = Errors::default();

        let expected = Stats {
            keys_count: IndexMap::from([("$".to_string(), 2)]),
            line_count: 2,
            keys_types_count: IndexMap::from([("$::Number".to_string(), 2)]),
            empty_lines: vec![2.to_string()],
            ..Default::default()
        };

        let stats = process_json_iterable(&settings, json_iter_in, &errors);
        assert_eq!(expected, stats);
        assert!(errors.container.borrow().len() == 1)
    }

    #[test]
    fn simple_process_json_iterable_par() {
        let iter: Vec<(String, Value)> = vec![
            (1.to_string(), json!({"key1": 123})),
            (2.to_string(), json!({"key2": 123})),
            (3.to_string(), json!({"key1": 123})),
        ];
        let iter = iter.into_iter().par_bridge();

        let expected = Stats {
            keys_count: IndexMap::from([("$.key1".to_string(), 2), ("$.key2".to_string(), 1)]),
            line_count: 3,
            keys_types_count: IndexMap::from([
                ("$.key1::Number".to_string(), 2),
                ("$.key2::Number".to_string(), 1),
            ]),
            ..Default::default()
        };

        let args = Cli::default();
        let settings = Settings::init(args).unwrap();
        let errors = ErrorsPar::default();
        let stats = process_json_iterable_par(&settings, iter, &errors);
        assert_eq!(expected, stats);
    }

    #[test]
    fn simple_process_json_iterable_par_jsonpath() {
        let iter: Vec<(String, Value)> = vec![
            (1.to_string(), json!({"key1": 123})),
            (2.to_string(), json!({"a": {"key2": 123}})),
            (3.to_string(), json!({"key1": 123})),
        ];
        let iter = iter.into_iter().par_bridge();

        let expected = Stats {
            keys_count: IndexMap::from([("$.key2".to_string(), 1)]),
            line_count: 1,
            keys_types_count: IndexMap::from([("$.key2::Number".to_string(), 1)]),
            empty_lines: vec![1.to_string(), 3.to_string()],
            ..Default::default()
        };

        let mut args = Cli::default();
        args.jsonpath = Some("$.a".to_string());
        let settings = Settings::init(args).unwrap();
        let errors = ErrorsPar::default();
        let stats = process_json_iterable_par(&settings, iter, &errors);
        assert_eq!(expected, stats);
    }

    #[test]
    fn add_filestats() {
        let lhs = stats::FileStats {
            file_path: "file/1.json".to_string(),
            stats: Stats {
                keys_count: IndexMap::from([("$.key1".to_string(), 3), ("$.key2".to_string(), 2)]),
                line_count: 5,
                keys_types_count: IndexMap::from([
                    ("$.key1::Number".to_string(), 3),
                    ("$.key2::Number".to_string(), 2),
                ]),
                bad_lines: vec!["4".to_string()],
                empty_lines: vec!["5".to_string()],
            },
        };
        let rhs = stats::FileStats {
            file_path: "file/2.json".to_string(),
            stats: Stats {
                keys_count: IndexMap::from([("$.key3".to_string(), 3), ("$.key2".to_string(), 2)]),
                line_count: 7,
                keys_types_count: IndexMap::from([
                    ("$.key3::Number".to_string(), 3),
                    ("$.key2::Number".to_string(), 2),
                ]),
                bad_lines: vec!["1".to_string()],
                empty_lines: vec!["2".to_string()],
            },
        };
        let expected = Stats {
            keys_count: IndexMap::from([
                ("$.key1".to_string(), 3),
                ("$.key2".to_string(), 4),
                ("$.key3".to_string(), 3),
            ]),
            line_count: 12,
            keys_types_count: IndexMap::from([
                ("$.key1::Number".to_string(), 3),
                ("$.key2::Number".to_string(), 4),
                ("$.key3::Number".to_string(), 3),
            ]),
            bad_lines: vec!["file/1.json:4".to_string(), "file/2.json:1".to_string()],
            empty_lines: vec!["file/1.json:5".to_string(), "file/2.json:2".to_string()],
        };

        let vec_of_file_stats = vec![lhs.clone(), rhs.clone()];
        let actual_ref = lhs.clone() + &rhs;
        let actual = lhs + rhs;

        assert_eq!(actual, expected);
        assert_eq!(actual_ref, expected);
        assert_eq!(vec_of_file_stats.iter().sum::<Stats>(), expected);
    }
}