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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
use indicatif::{ProgressBar, ProgressStyle};
use blake3::Hasher as Blake3Hasher;
use chrono::NaiveDate;

use std::{
    str,
    ops::Deref,
    error::Error,
    process::Command,
    fs::{self, File},
    path::{self, Path},
    collections::{HashSet, HashMap},
    io::{Write, Read, BufRead, BufReader},
};

mod constants;
mod macros;
mod options;
mod operations;
mod separator;
mod slice;
mod strings;

pub use self::{
    constants::*,
    macros::*,
    options::*,
    operations::*,
    separator::*,
    slice::*,
    strings::*,
};

pub type MyError = Box<dyn std::error::Error + Send + Sync>;
pub type MyResult<T> = Result<T, MyError>;

const HEX: [char; 16] = [
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
	'a', 'b', 'c', 'd', 'e', 'f',
];

/// Clear (wipe) the terminal screen
pub fn clear_terminal_screen() {
    let result = if cfg!(target_os = "windows") {
        Command::new("cmd").args(["/c", "cls"]).spawn()
    } else {
        // "clear" or "tput reset"
        Command::new("tput").arg("reset").spawn()
    };

    // Alternative solution:
    if result.is_err() {
        print!("{esc}c", esc = 27 as char);
    }
}

// https://stackoverflow.com/questions/69297477/getting-the-length-of-an-int
// https://users.rust-lang.org/t/whats-the-quickest-way-to-get-the-length-of-an-integer
// https://internals.rust-lang.org/t/pre-rfc-lo-and-hi-methods-for-splitting-integers-into-their-halves
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f29cbb40ffce2498c005d390b22dd51c
// https://github.com/blueglyph/ilog
/// Returns the number of digits in an integer.
///
/// The input can be an integer from u8 to u128.
pub fn num_digits<I>(integer: I) -> usize
where
    I: ilog::IntLog
{
    integer.checked_log10().unwrap_or(0) + 1
}

// https://stackoverflow.com/questions/56620265/how-to-access-the-bufreader-twice/
/// File is an object providing access to an open file on the filesystem.
/// Use the seek or rewind functions to reset the position of the files to start.
pub fn open_file<P>(path: P) -> Result<File, Box<dyn Error>>
where
    P: AsRef<Path> + std::marker::Copy + std::fmt::Debug,
{
    let file: File = match fs::OpenOptions::new()
        .read(true)
        .write(false)
        .create(false)
        .open(path)
        {
            Ok(file) => file,
            Err(error) => {
                eprintln!("Failed to open file {path:?}");
                panic!("{error}");
            }
        };

    Ok(file)
}

/// Adds a counter for the number of lines in a file.
pub trait FileExtension {
    fn count_lines(&mut self) -> Result<u64, Box<dyn Error>>;    // use BufReader
    // fn count_lines(&mut self) -> Result<u64, Box<dyn Error>>; // use memmap2
}

impl FileExtension for File {
    /**
    Count the number of lines in the file.

    Example:
    ```
    use claudiofsr_lib::{FileExtension, open_file};
    use std::{fs::File, io::Write, path::Path, error::Error};

    fn main() -> Result<(), Box<dyn Error>> {

        let filename = "/tmp/sample.txt";
        let mut file = File::create(filename)?;
        file.write_all(b"A test\nActual content\nMore content\nAnother test")?;

        let path = Path::new(filename);
        let mut file: File = open_file(path)?;
        let number_of_lines: u64 = file.count_lines()?;

        assert_eq!(number_of_lines, 4);
        Ok(())
    }
    ````
    */
    fn count_lines(&mut self) -> Result<u64, Box<dyn Error>> {

        let count: u64 = BufReader::new(self)
            //.lines()     // Return an error if the read bytes are not valid UTF-8
            .split(b'\n')  // Ignores invalid UTF-8 but
            .try_count()?; // Catches other errors

        Ok(count)
    }

    /*
    /// Count the number of lines in the file
    ///
    /// use memmap2::Mmap;
    fn count_lines(&mut self) -> Result<u64, Box<dyn Error>> {

        // https://docs.rs/memmap2/latest/memmap2/struct.Mmap.html
        let count: u64 = unsafe { Mmap::map(&*self)? }
            .par_split(|&byte| byte == b'\n') // ignore invalid UTF-8
            .count()
            .try_into()?;

        Ok(count)
    }
    */
}

/**
Count function consumes the Lines:

`let number_of_lines = BufReader::new(file).lines().count();`

`count()` essentially loops over all the elements of the iterator, incrementing a counter until the iterator returns None.

In this case, once the file can no longer be read, the iterator never returns None, and the function runs forever.

You can make the iterator return None when the inner value is an error by using functions such as try_fold().

```
    use claudiofsr_lib::IteratorExtension;
    use std::io::{BufRead, BufReader};

    let text: &str = "this\nis\na\ntest\n";
    let counter: Result<u64, _> = BufReader::new(text.as_bytes())
        //.lines()    // Return an error if the read bytes are not valid UTF-8
        .split(b'\n') // Ignores invalid UTF-8 but
        .try_count(); // Catches other errors

    //assert!(result.is_ok_and(|c| c == 4));
    assert!(matches!(counter, Ok(4)));
```

<https://www.reddit.com/r/rust/comments/wyk1l0/can_you_compose_stdiolines_with_stditercount/>
*/
pub trait IteratorExtension<E> {
    fn try_count(&mut self) -> Result<u64, E>;
}

impl<T, U, E> IteratorExtension<E> for T
where
    T: Iterator<Item = Result<U, E>>
{
    /**
    Try to count the iter number
    ```
        use claudiofsr_lib::IteratorExtension;
        use std::io::{BufRead, BufReader};

        let invalid_unicode = b"\xc3\x28\x30\x0a\x31\x0a\x32\x0a";
        let counter = BufReader::new(&invalid_unicode[..])
                //.lines(); // Return an error if the read bytes are not valid UTF-8
                .split(b'\n')
                .try_count();

        assert!(matches!(counter, Ok(3)));
    ```
    <https://www.reddit.com/r/rust/comments/wyk1l0/can_you_compose_stdiolines_with_stditercount/>
    */
    fn try_count(&mut self) -> Result<u64, E> {
        self.try_fold(0, |accumulator: u64, element: Result<U, E>|
            element.map(|_| accumulator + 1)
        )
    }
}

/// Byte slice `&[u8]` extension.
pub trait BytesExtension {
    fn trim(&self) -> &Self;
    fn to_hex_string(&self) -> String;
}

impl BytesExtension for [u8] {
    /**
    Trim ascii whitespace from the start and end of `&[u8]`.

    Returns `&[u8]` with leading and trailing whitespace removed.

    Example:
    ```
        use claudiofsr_lib::BytesExtension;

        let text: &str = " foo bar\r\n";
        let bytes: Vec<u8> = text.bytes().collect();

        println!("bytes: {bytes:?}");

        let trimmed = bytes.trim();

        println!("trimmed: {trimmed:?}");

        assert_eq!(bytes, [32, 102, 111, 111, 32, 98, 97, 114, 13, 10]);
        assert_eq!(trimmed, [102, 111, 111, 32, 98, 97, 114]);
    ```

    <https://stackoverflow.com/questions/31101915/how-to-implement-trim-for-vecu8>
    */
    fn trim(&self) -> &[u8] {
        let from = match self.iter().position(|x| !x.is_ascii_whitespace()) {
            Some(i) => i,
            None => return &self[0..0],
        };
        let to = self.iter().rposition(|x| !x.is_ascii_whitespace()).unwrap();
        &self[from..=to]
    }

    /**
    `&[u8]` to  hex string

    Example:
    ```
        use claudiofsr_lib::BytesExtension;

        let text: &str = " foo bar\n";
        let bytes: Vec<u8> = text.bytes().collect();

        println!("bytes: {bytes:?}");

        let string = bytes.to_hex_string();

        println!("string: {string:?}");

        assert_eq!(bytes, [32, 102, 111, 111, 32, 98, 97, 114, 10]);
        assert_eq!(string, "20666f6f206261720a");
    ```
    */
    fn to_hex_string(&self) -> String {
        self.iter()
            .flat_map(|byte| {
                let a: char = HEX[(*byte as usize)/16];
                let b: char = HEX[(*byte as usize)%16];
                vec![a, b]
            })
            .collect()
    }
}

/**
Convert Vec\<&str\> to Vec\<String\>

<https://www.reddit.com/r/learnrust/comments/h82em8/best_way_to_create_a_vecstring_from_str>

Example:
```
    use claudiofsr_lib::to_vec_string;
    let original: Vec<&str> = vec!["this", "that", "the other"];
    let result: Vec<String> = to_vec_string(&original);
    assert_eq!(result, vec![
        String::from("this"),
        String::from("that"),
        String::from("the other"),
    ]);
```
*/
#[allow(dead_code)]
pub fn to_vec_string<T>(v: &[T]) -> Vec<String>
    where
        T: ToString
{
    v.iter().map(|x| x.to_string()).collect()
}

/**
Convert Vec\<String\> to Vec\<&str\>

<https://stackoverflow.com/questions/41179659/convert-vecstring-into-a-slice-of-str-in-rust>

Example:
```
    use claudiofsr_lib::{svec, to_vec_slice};
    let original: Vec<String> = svec!["this", "that", "the other"];
    let result: Vec<&str> = to_vec_slice(&original);
    assert_eq!(result, vec![
        "this",
        "that",
        "the other",
    ]);
```
*/
#[allow(dead_code)]
pub fn to_vec_slice<T>(v: &[T]) -> Vec<&str>
    where
        T: AsRef<str>
{
    v.iter().map(|x| x.as_ref()).collect()
}

/// Gets Date from a string containing 8 digits.
///
/// Date format: DDMMYYYY.
///
/// Check if NaiveDate is valid.
///
/// Returns None on the out-of-range date, invalid month and/or day.
///
/// <https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html#method.from_ymd_opt>
///
/// <https://docs.rs/chrono/latest/chrono/struct.DateTime.html#method.parse_from_str>
///
/// Example:
/// ```
///     use claudiofsr_lib::get_naive_date;
///     use chrono::{NaiveDate, Datelike};
///
///     let date_str1: &str = "06-12-2022T00:00:00-03:00";
///     let date1: Option<NaiveDate> = get_naive_date(date_str1);
///     let tuple: Option<(i32, u32, u32)> = date1.map(|dt|(dt.year(), dt.month(), dt.day()));
///
///     let date_str2: &str = "29021973";
///     let date2: Option<NaiveDate> = get_naive_date(date_str2);
///
///     assert_eq!(date1, NaiveDate::from_ymd_opt(2022, 12, 6));
///     assert_eq!(tuple, Some((2022, 12, 6)));
///     assert_eq!(date2, None);
/// ```
pub fn get_naive_date<T>(date: T) -> Option<NaiveDate>
    where
        T: Deref<Target=str> + std::fmt::Display,
{
    // "06-12-2022T00:00:00-03:00" -> "061220220000000300"
    let digits: String = date.remove_non_digits();

    let ddmmyyyy: &str = if digits.chars_count() >= 8 {
        &digits[..8]
    } else {
        return None;
    };

    match NaiveDate::parse_from_str(ddmmyyyy, "%-d%-m%Y") {
        Ok(dt) => Some(dt),
        Err(why) => {
            eprintln!("fn get_naive_date()");
            eprintln!("Data inválida ou inexistente!");
            eprintln!("Erro: {why}");
            eprintln!("\t'{date}'");
            None
        }
    }
}

/// Gets Date from a string containing 8 digits.
pub fn get_naive_date_v2<T>(date: T) -> Option<NaiveDate>
    where
        T: Deref<Target=str> + std::fmt::Display,
{
    let digits: String = date.remove_non_digits();

    // date: DDMMYYYY
    let ddmmyyyy: u32 = if digits.chars_count() >= 8 {
        digits[..8].parse::<u32>()
        .expect("fn get_naive_date()\nEsperado um número inteiro com 8 dígitos!")
    } else {
        return None;
    };

    let day    = ddmmyyyy / 1_000_000;
    let mmyyyy = ddmmyyyy % 1_000_000;

    let month = mmyyyy / 10_000;
    let year  = mmyyyy % 10_000;

    let dt: Option<NaiveDate> = NaiveDate::from_ymd_opt(year as i32, month, day);

    if dt.is_none() {
        eprintln!("Erro! Data inválida ou inexistente:");
        eprintln!("\t'{date}': day: {day} ; month: {month} ; year: {year}");
    }

    dt
}

// https://stackoverflow.com/questions/43516351/how-to-convert-a-string-of-digits-into-a-vector-of-digits
// https://stackoverflow.com/questions/27535289/what-is-the-correct-way-to-return-an-iterator-or-any-other-trait
/// Convert a string of digits to a vector of digits Vec\<u32\>
///
/// Example:
/// ```
///     use claudiofsr_lib::string_to_digits;
///
///     let digits_slice: &str = "06122022";
///     let vector1: Vec<u32> = string_to_digits(digits_slice);
///
///     let digits_string: String = "06122022".to_string();
///     let vector2: Vec<u32> = string_to_digits(digits_string);
///
///     assert_eq!(vector1, vector2);
///     assert_eq!(vector1, vec![0, 6, 1, 2, 2, 0, 2, 2]);
/// ```
pub fn string_to_digits<T>(string: T) -> Vec<u32>
where
    T: Deref<Target=str>,
{
    let opt_vec: Option<Vec<u32>> = string
        .chars()
        .map(|ch: char| ch.to_digit(10))
        .collect();

    match opt_vec {
        Some(vec_of_digits) => vec_of_digits,
        None                => vec![],
    }
}

// https://stackoverflow.com/questions/26536871/how-can-i-convert-a-string-of-numbers-to-an-array-or-vector-of-integers-in-rust
/// Convert a string of digits to a vector of digits Vec\<u32\>
///
/// Example:
/// ```
///     use claudiofsr_lib::string_to_vec_of_integers;
///
///     let digits_slice: &str = "06 12 2022";
///     let vector1: Vec<u32> = string_to_vec_of_integers(digits_slice).unwrap();
///
///     let digits_string: String = "06 12 2022".to_string();
///     let vector2: Vec<u32> = string_to_vec_of_integers(digits_string).unwrap();
///
///     assert_eq!(vector1, vector2);
///     assert_eq!(vector1, vec![6, 12, 2022]);
/// ```
pub fn string_to_vec_of_integers<T>(string: T) -> Result<Vec<u32>, Box<dyn Error>>
where
    T: Deref<Target=str>,
{
    let vec_u32: Result<Vec<u32>, _> = string // Error: ParseIntError
        .split_whitespace()
        .map(|s: &str| s.parse::<u32>())
        .collect();

    Ok(vec_u32?)
}

/// Two Rounding method for floating-point operations:
///
/// 1. Round to nearest value, ties to even:
///
///     if the number falls midway, it is rounded to the nearest value with an even least significant digit.
///
/// 2. Round to nearest value, ties away from zero (or ties to away):
///
///     if the number falls midway, it is rounded to the nearest value above (for positive numbers) or below (for negative numbers).
///
/// Python takes the first approach and Rust takes the second.
///
/// Neither is contradicting the IEEE-754 standard, which defines and allows for both.
///
/// ```
///     use claudiofsr_lib::round_f64;
///     let decimals: u32 = 2;
///
///     let number01: f64 = 1.454999;
///     let result01: f64 = round_f64(number01, decimals);
///     let expected01: f64 = 1.45;
///     assert!(matches!(expected01, result01));
///
///     let number02: f64 = 1.455000;
///     let result02: f64 = round_f64(number02, decimals);
///     let expected02: f64 = 1.46;
///     assert!(matches!(expected02, result02));
/// ```
/// <https://floating-point-gui.de/languages/rust>
///
/// <https://doc.rust-lang.org/std/primitive.f64.html#method.powf>
///
/// <https://pola-rs.github.io/polars/src/polars_core/series/ops/round.rs.html#8>
pub fn round_f64(value: f64, decimals: u32) -> f64 {
    if decimals == 0 {
        value.round()
    } else {
        let multiplier = 10.0_f64.powf(decimals as f64);
        (value * multiplier).round() / multiplier
    }
}

/// Command line progress with indicatif ProgressBar
pub fn get_progressbar(msg: &'static str, total: usize) -> MyResult<ProgressBar> {
    let style = get_style(0, 0, 38)?;

    let pb = ProgressBar::new(total.try_into()?);
    pb.set_message(msg);
    pb.set_style(style);

    Ok(pb)
}

/// Genarate ProgressStyle by template and progress characters.
pub fn get_style(
    template_index: usize,
    chars_index: usize,
    length: usize,
) -> MyResult<ProgressStyle> {

    let template_01 = format!("{{msg:{}}} {{spinner:.green}} [{{wide_bar:.cyan/blue}}] {{percent}}/100% ({{eta}}) [{{elapsed_precise}}]", length);
    let template_02 = format!("{{msg:{}}} {{spinner:.green}} [{{wide_bar:.cyan/blue}}] {{percent}}/100% ({{eta}})", length);
    let template_03 = format!("{{msg:{}}} {{spinner:.green}} [{{wide_bar:.cyan/blue}}] {{pos}}/{{len}} ({{eta}})", length);
    let template_04 = format!("[{{elapsed_precise}}] {{bar:40.cyan/blue}} {{pos}}/{{len}} {{msg:{}}}", length);

    let templates = [
        template_01,
        template_02,
        template_03,
        template_04,
    ];

    let progress_characters = ["#>-",
        "## ",
        "■□ ",
        "█░-",
        "🦀👾👻",
    ];

    let style: ProgressStyle = ProgressStyle::default_bar()
        .template(&templates[template_index])?
        .progress_chars(progress_characters[chars_index]);

    Ok(style)
}

/// Print to file and to stdout
pub fn my_print<P>(write_buffer: &[u8], path: P) -> Result<(), Box<dyn Error>>
where P: AsRef<path::Path>
{
    // Print to file
    let mut file = fs::File::create(path)?;
    file.write_all(write_buffer)?;

    // Converts a slice of bytes to a string slice
    let print_msg = match str::from_utf8(write_buffer) {
        Ok(valid_uft8) => valid_uft8,
        Err(error) => {
            eprintln!("fn my_print()");
            eprintln!("Invalid UTF-8 sequence!");
            panic!("{error}");
        }
    };

    // Print to stdout
    // writeln!(std::io::stdout(), "{print_msg}")?;
    println!("{print_msg}");

    Ok(())
}

/// Calculates the Blake3 hash from Path.
///
/// <https://docs.rs/blake3/latest/blake3>
///
/// <https://rust-lang-nursery.github.io/rust-cookbook/cryptography/hashing.html>
pub fn blake3_hash<P>(path: P) -> Result<String, Box<dyn Error>>
where
    P: AsRef<Path> + std::marker::Copy + std::fmt::Debug,
{
    let file: File = open_file(path)?;
    let mut reader: BufReader<File> = BufReader::new(file);
    let mut buffer = [0; 1024];

    let mut hasher = Blake3Hasher::new();

    loop {
        let count = reader.read(&mut buffer)?;
        if count == 0 {
            break;
        }
        hasher.update(&buffer[..count]);
    }

    let hash: String = hasher.finalize().to_string();

    Ok(hash)
}

/// Split a slice into smaller slices of size N.
///
/// Then print the result.
pub fn print_split<T>(values: &[T], size: usize)
where
    T: std::fmt::Debug
{
    for value in values.chunks(size) {
        let text = value
            .iter()
            .map(|v| format!("{v:?}"))
            .collect::<Vec<String>>()
            .join(" ");
        println!("{text}");
    }
}

/// Get unique values from vector items.
pub trait Unique<T> {
    /**
    Get unique values from vector items.

    This method operates in place, visiting each element exactly once in the
    original order, and preserves the order of the retained elements.

    Example:
    ```
        use claudiofsr_lib::Unique;

        let mut items1: Vec<u16> = Vec::new();
        let mut items2: Vec<u32> = vec![1, 3, 1, 2, 2, 4, 3];
        let mut items3: Vec<&str> = vec!["foo", "foo", "bar", "foo"];

        items1.unique();
        items2.unique();
        items3.unique();

        assert_eq!(items1, []);
        assert_eq!(items2, [1, 3, 2, 4]);
        assert_eq!(items3, ["foo", "bar"]);
    ```
    `items.retain(|item| seen.insert(item.clone()))`

    This works because retain only keeps items for which the predicate returns true,
    and insert only returns true if the item was not previously present in the set.

    Since the vector is traversed in order, we end up keeping just the first occurrence of each item.

    <https://stackoverflow.com/questions/64819025/is-there-a-simple-way-remove-duplicate-elements-from-an-array>
    */
    fn unique(&mut self);
}

impl<T> Unique<T> for Vec<T>
where
    T: Clone + Eq + std::hash::Hash
{
    fn unique(self: &mut Vec<T>) {
        let mut seen: HashSet<T> = HashSet::new();
        self.retain(|item| seen.insert(item.clone()));
    }
}

/// Partition into unique and duplicate slice elements.
pub trait Partition<T> {
    /**
    Partition into unique and duplicate slice elements.

    Order is preserved.

    Example:
    ```
        use claudiofsr_lib::Partition;

        let mut items_a: Vec<u16> = vec![1, 3, 2, 1, 5, 2, 9, 2];
        let (unique, duplicate): (Vec<u16>, Vec<u16>) = items_a.partition_dup();
        assert_eq!(unique, [3, 5, 9]);
        assert_eq!(duplicate, [1, 2, 1, 2, 2]);

        let mut items_b = vec![1, 3, 2, 5, 9];
        let (unique, duplicate) = items_b.partition_dup();
        assert_eq!(unique, [1, 3, 2, 5, 9]);
        assert_eq!(duplicate, []);
    ```
    */
    fn partition_dup(&self) -> (Vec<T>, Vec<T>);
}

impl<T> Partition<T> for Vec<T>
where
    T: Clone + Eq + std::hash::Hash,
{
    fn partition_dup(&self) -> (Vec<T>, Vec<T>) {
        let mut seen: HashSet<T> = HashSet::new();
        let mut filter: HashMap<T, bool> = HashMap::new();
        let mut unique = Vec::new();
        let mut duplicate = Vec::new();

        self.iter().for_each(|item| {
            filter.insert(item.clone(), seen.insert(item.clone()));
        });

        self.iter().for_each(|item| {
            let is_unique: bool = filter[item];
            if is_unique {
                unique.push(item.clone());
            } else {
                duplicate.push(item.clone());
            }
        });

        (unique, duplicate)
    }
}

/// Try Convert Extension
pub trait TryConvertExtension<T> {
    /**
    Try converting type T to type U

    "Simple and safe type conversions that may fail
    in a controlled way under some circumstances.""

    Example:
    ```
        use claudiofsr_lib::TryConvertExtension;

        let type_u8: u8 = 5;
        let type_i16: i16 = 5;
        let type_u32: u32 = 5;
        let type_f64: f64 = 5.0;

        let value_f64: f64 = type_u8.try_convert();
        assert_eq!(type_f64, value_f64);

        let value_u32: u32 = type_i16.try_convert();
        assert_eq!(type_u32, value_u32);

        let value_usize: usize = 7_i32.try_convert();
        assert_eq!(7_usize, value_usize);

        let value_f64: f64 = 9_u16.try_convert();
        assert_eq!(9.0_f64, value_f64);

        // With TurboFish

        let value_u32 = type_i16.try_convert::<u32>();
        assert_eq!(type_u32, value_u32);

        let value_f64 = 2_i32.try_convert::<f64>();
        assert_eq!(2.0_f64, value_f64);
    ```
    */
    fn try_convert<U>(self) -> U
    where
        U: TryFrom<T>,
        <U as TryFrom<T>>::Error: std::fmt::Display;
}

impl<T> TryConvertExtension<T> for T {
    fn try_convert<U>(self) -> U
    where
        U: TryFrom<T>,
        <U as TryFrom<T>>::Error: std::fmt::Display
    {
        match U::try_from(self) {
            Ok(type_u) => type_u,
            Err(why) => {
                let t = std::any::type_name::<T>();
                let u = std::any::type_name::<U>();
                panic!("Error converting from {t} to {u}: {why}")
            }
        }
    }
}

#[cfg(test)]
mod functions {
    use super::*;
    use chrono::NaiveDate;
    use std::collections::HashMap;

    // cargo test -- --help
    // cargo test -- --nocapture
    // cargo test -- --show-output

    #[test]
    fn clear_terminal() {
        // cargo test -- --show-output clear_terminal
        clear_terminal_screen()
    }

    #[test]
    fn unique_values() {
        // cargo test -- --show-output unique_values

        let mut vector = vec![1, 4, 3, 4, 5, 4, 3, 4, 2, 3];
        println!("vector: {:?}", vector);

        vector.unique();
        println!("vector: {:?}", vector);

        assert_eq!(vector, [1, 4, 3, 5, 2]);
    }

    #[test]
    fn partition_values() {
        // cargo test -- --show-output partition_values

        let vector: Vec<String> = ["aa", "ab", "aa", "aa", "cc", "aa", "ab", "d1"]
            .iter()
            .map(|s| s.to_string())
            .collect();
        println!("vector: {:?}", vector);

        let (u, d) = vector.partition_dup();
        println!("unique: {u:?}");
        println!("duplicate: {d:?}");

        assert_eq!(u, ["cc", "d1"]);
        assert_eq!(d, ["aa", "ab", "aa", "aa", "aa", "ab"]);
    }

    #[test]
    fn basic_trimming() {
        // https://stackoverflow.com/questions/31101915/how-to-implement-trim-for-vecu8
        // cargo test -- basic_trimming
        assert_eq!(b" A ".trim(), b"A");
        assert_eq!(b" AB ".trim(), b"AB");
        assert_eq!(b"A ".trim(), b"A");
        assert_eq!(b"AB ".trim(), b"AB");
        assert_eq!(b" A".trim(), b"A");
        assert_eq!(b" AB".trim(), b"AB");
        assert_eq!(b" A B ".trim(), b"A B");
        assert_eq!(b"A B ".trim(), b"A B");
        assert_eq!(b" A B".trim(), b"A B");
        assert_eq!(b" ".trim(), b"");
        assert_eq!(b"  ".trim(), b"");
        assert_eq!(b"\nA\n".trim(), b"A");
        assert_eq!(b"\nA  B\r\n".trim(), b"A  B");
        assert_eq!(b"\r\nA  B\r\n".trim(), b"A  B");
    }

    #[test]
    fn data_dia_mes_ano() -> Result<(), Box<dyn Error>> {
        // cargo test -- --show-output data_dia_mes_ano

        for (date, result) in [
            ("18052022", NaiveDate::from_ymd_opt(2022,  5, 18)),
            ("15121500", NaiveDate::from_ymd_opt(1500, 12, 15)),
            ("29021972", NaiveDate::from_ymd_opt(1972,  2, 29)),
            ("18152022", None),
            ("29021973", None),
            ("2921972", None),
            ("2023", None),
            ("", None),
        ] {
            println!("date: '{date}' ; result: {result:?}");
            let naive_date: Option<NaiveDate> = get_naive_date(date);
            assert_eq!(naive_date, result);
        }

        Ok(())
    }

    #[test]
    fn test_num_digits() -> Result<(), Box<dyn Error>> {
        // cargo test -- --show-output num_digits

        let input: u8 = 0;
        let lenght_u8_zero = num_digits(input);
        println!("num_digits({input}) => {lenght_u8_zero}");

        let input: u8 = 255;
        let lenght_u8 = num_digits(input);
        println!("num_digits({input}) => {lenght_u8}");

        let input: u16 = 65535;
        let lenght_u16 = num_digits(input);
        println!("num_digits({input}) => {lenght_u16}");

        let input: u32 = 4294967295;
        let lenght_u32 = num_digits(input);
        println!("num_digits({input}) => {lenght_u32}");

        let input: f64 = 123456.789;
        let lenght_f64 = num_digits(input as usize);
        println!("num_digits({input}) => {lenght_f64}");

        let input: u64 = 18446744073709551615;
        let lenght_u64 = num_digits(input);
        println!("num_digits({input}) => {lenght_u64}");

        let input: u128 = 340282366920938463463374607431768211455;
        let lenght_u128 = num_digits(input);
        println!("num_digits({input}) => {lenght_u128}");

        assert_eq!(
            (
                lenght_u8_zero, lenght_u8, lenght_u16, lenght_u32,
                lenght_f64, lenght_u64, lenght_u128
            ),
            (1, 3, 5, 10, 6, 20, 39)
        );

        Ok(())
    }

    #[test]
    fn test_group_anagrams() -> Result<(), Box<dyn Error>> {
        // cargo test -- --show-output test_group_anagrams
        // https://leetcode.com/problems/group-anagrams/description/
        // https://leetcode.com/problems/group-anagrams/solutions/2155441/rust-hashmap-solution-simple/

        pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
            let mut h = HashMap::new();

            for s in strs {
                let mut key: Vec<char> = s.chars().collect();
                key.sort();
                h.entry(key).or_insert(Vec::new()).push(s);
            }

            h.into_values().collect()
        }

        let text = svec!("abc", "bac", "def", "ab", "tuvxz", "abc", "a", "bca", "fed");
        let result = group_anagrams(text);

        println!("result: {result:?}");

        Ok(())
    }
}