rust-stdf 0.3.1

A library for parsing Standard Test Data Format (STDF) files of version V4 and V4-2007.
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
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
//
// atdf_types.rs
// Author: noonchen - chennoon233@foxmail.com
// Created Date: October 26th 2022
// -----
// Last Modified: Mon Nov 14 2022
// Modified By: noonchen
// -----
// Copyright (c) 2022 noonchen
//

use self::atdf_record_field::*;
use crate::{stdf_error::StdfError, stdf_record_type::*, *};
use chrono::NaiveDateTime;
use std::collections::hash_map::HashMap;

macro_rules! ser_optional {
    ($struct_name:ident.$field:ident, $method:ident) => {
        if let Some(ref $field) = $struct_name.$field {
            $field.$method()
        } else {
            String::new()
        }
    };
    (&$struct_name:ident.$field:ident, $method:ident()) => {
        if let Some(ref $field) = $struct_name.$field {
            $method(&$field)
        } else {
            String::new()
        }
    };
}

pub(crate) mod atdf_record_field {
    // ATDF fields may not map to STDF fields
    // (ATDF field name, is required? or must presented)
    pub(crate) const FAR_FIELD: [(&str, bool); 4] = [
        ("FileType", true),
        ("STDF_VER", true),
        ("ATDFVer", true),
        ("ScaleFlag", false),
    ];

    pub(crate) const ATR_FIELD: [(&str, bool); 2] = [("MOD_TIM", false), ("CMD_LINE", false)];

    pub(crate) const MIR_FIELD: [(&str, bool); 38] = [
        ("LOT_ID", true),
        ("PART_TYP", true),
        ("JOB_NAM", true),
        ("NODE_NAM", true),
        ("TSTR_TYP", true),
        ("SETUP_T", true),
        ("START_T", true),
        ("OPER_NAM", true),
        ("MODE_COD", true),
        ("STAT_NUM", true),
        ("SBLOT_ID", false),
        ("TEST_COD", false),
        ("RTST_COD", false),
        ("JOB_REV", false),
        ("EXEC_TYP", false),
        ("EXEC_VER", false),
        ("PROT_COD", false),
        ("CMOD_COD", false),
        ("BURN_TIM", false),
        ("TST_TEMP", false),
        ("USER_TXT", false),
        ("AUX_FILE", false),
        ("PKG_TYP", false),
        ("FAMLY_ID", false),
        ("DATE_COD", false),
        ("FACIL_ID", false),
        ("FLOOR_ID", false),
        ("PROC_ID", false),
        ("OPER_FRQ", false),
        ("SPEC_NAM", false),
        ("SPEC_VER", false),
        ("FLOW_ID", false),
        ("SETUP_ID", false),
        ("DSGN_REV", false),
        ("ENG_ID", false),
        ("ROM_COD", false),
        ("SERL_NUM", false),
        ("SUPR_NAM", false),
    ];

    pub(crate) const MRR_FIELD: [(&str, bool); 4] = [
        ("FINISH_T", true),
        ("DISP_COD", false),
        ("USR_DESC", false),
        ("EXC_DESC", false),
    ];

    pub(crate) const PCR_FIELD: [(&str, bool); 7] = [
        ("HEAD_NUM", true),
        ("SITE_NUM", true),
        ("PART_CNT", false),
        ("RTST_CNT", false),
        ("ABRT_CNT", false),
        ("GOOD_CNT", false),
        ("FUNC_CNT", false),
    ];

    pub(crate) const HBR_FIELD: [(&str, bool); 6] = [
        ("HEAD_NUM", true),
        ("SITE_NUM", true),
        ("HBIN_NUM", true),
        ("HBIN_CNT", true),
        ("HBIN_PF", false),
        ("HBIN_NAM", false),
    ];

    pub(crate) const SBR_FIELD: [(&str, bool); 6] = [
        ("HEAD_NUM", true),
        ("SITE_NUM", true),
        ("SBIN_NUM", true),
        ("SBIN_CNT", true),
        ("SBIN_PF", false),
        ("SBIN_NAM", false),
    ];

    pub(crate) const PMR_FIELD: [(&str, bool); 7] = [
        ("PMR_INDX", true),
        ("CHAN_TYP", false),
        ("CHAN_NAM", false),
        ("PHY_NAM", false),
        ("LOG_NAM", false),
        ("HEAD_NUM", false),
        ("SITE_NUM", false),
    ];

    pub(crate) const PGR_FIELD: [(&str, bool); 3] =
        [("GRP_INDX", true), ("GRP_NAM", false), ("PMR_INDX", false)];

    pub(crate) const PLR_FIELD: [(&str, bool); 5] = [
        ("GRP_INDX", true),
        ("GRP_MODE", false),
        ("GRP_RADX", false),
        ("PGM_CHAL,PGM_CHAR", false),
        ("RTN_CHAL,RTN_CHAR", false),
    ];

    //required by spec, but it could be missing thou
    pub(crate) const RDR_FIELD: [(&str, bool); 1] = [("RTST_BIN", false)];

    pub(crate) const SDR_FIELD: [(&str, bool); 19] = [
        ("HEAD_NUM", true),
        ("SITE_GRP", true),
        ("SITE_NUM", true),
        ("HAND_TYP", false),
        ("HAND_ID", false),
        ("CARD_TYP", false),
        ("CARD_ID", false),
        ("LOAD_TYP", false),
        ("LOAD_ID", false),
        ("DIB_TYP", false),
        ("DIB_ID", false),
        ("CABL_TYP", false),
        ("CABL_ID", false),
        ("CONT_TYP", false),
        ("CONT_ID", false),
        ("LASR_TYP", false),
        ("LASR_ID", false),
        ("EXTR_TYP", false),
        ("EXTR_ID", false),
    ];

    pub(crate) const WIR_FIELD: [(&str, bool); 4] = [
        ("HEAD_NUM", true),
        ("START_T", true),
        ("SITE_GRP", false),
        ("WAFER_ID", false),
    ];

    pub(crate) const WRR_FIELD: [(&str, bool); 14] = [
        ("HEAD_NUM", true),
        ("FINISH_T", true),
        ("PART_CNT", true),
        ("WAFER_ID", false),
        ("SITE_GRP", false),
        ("RTST_CNT", false),
        ("ABRT_CNT", false),
        ("GOOD_CNT", false),
        ("FUNC_CNT", false),
        ("FABWF_ID", false),
        ("FRAME_ID", false),
        ("MASK_ID", false),
        ("USR_DESC", false),
        ("EXC_DESC", false),
    ];

    pub(crate) const WCR_FIELD: [(&str, bool); 9] = [
        ("WF_FLAT", false),
        ("POS_X", false),
        ("POS_Y", false),
        ("WAFR_SIZ", false),
        ("DIE_HT", false),
        ("DIE_WID", false),
        ("WF_UNITS", false),
        ("CENTER_X", false),
        ("CENTER_Y", false),
    ];

    pub(crate) const PIR_FIELD: [(&str, bool); 2] = [("HEAD_NUM", true), ("SITE_NUM", true)];

    pub(crate) const PRR_FIELD: [(&str, bool); 14] = [
        ("HEAD_NUM", true),
        ("SITE_NUM", true),
        ("PART_ID", true),
        ("NUM_TEST", true),
        ("Pass/Fail", true),
        ("HARD_BIN", true),
        ("SOFT_BIN", false),
        ("X_COORD", false),
        ("Y_COORD", false),
        ("RetestCode", false),
        ("AbortCode", false),
        ("TEST_T", false),
        ("PART_TXT", false),
        ("PART_FIX", false),
    ];

    pub(crate) const TSR_FIELD: [(&str, bool); 15] = [
        ("HEAD_NUM", true),
        ("SITE_NUM", true),
        ("TEST_NUM", true),
        ("TEST_NAM", false),
        ("TEST_TYP", false),
        ("EXEC_CNT", false),
        ("FAIL_CNT", false),
        ("ALRM_CNT", false),
        ("SEQ_NAME", false),
        ("TEST_LBL", false),
        ("TEST_TIM", false),
        ("TEST_MIN", false),
        ("TEST_MAX", false),
        ("TST_SUMS", false),
        ("TST_SQRS", false),
    ];

    pub(crate) const PTR_FIELD: [(&str, bool); 20] = [
        ("TEST_NUM", true),
        ("HEAD_NUM", true),
        ("SITE_NUM", true),
        ("RESULT", false),
        ("Pass/Fail", false),
        ("AlarmFlags", false),
        ("TEST_TXT", false),
        ("ALARM_ID", false),
        ("LimitCompare", false),
        ("UNITS", false),
        ("LO_LIMIT", false),
        ("HI_LIMIT", false),
        ("C_RESFMT", false),
        ("C_LLMFMT", false),
        ("C_HLMFMT", false),
        ("LO_SPEC", false),
        ("HI_SPEC", false),
        ("RES_SCAL", false),
        ("LLM_SCAL", false),
        ("HLM_SCAL", false),
    ];

    pub(crate) const MPR_FIELD: [(&str, bool); 25] = [
        ("TEST_NUM", true),
        ("HEAD_NUM", true),
        ("SITE_NUM", true),
        ("RTN_STAT", false),
        ("RTN_RSLT", false),
        ("Pass/Fail", false),
        ("AlarmFlags", false),
        ("TEST_TXT", false),
        ("ALARM_ID", false),
        ("LimitCompare", false),
        ("UNITS", false),
        ("LO_LIMIT", false),
        ("HI_LIMIT", false),
        ("START_IN", false),
        ("INCR_IN", false),
        ("UNITS_IN", false),
        ("RTN_INDX", false),
        ("C_RESFMT", false),
        ("C_LLMFMT", false),
        ("C_HLMFMT", false),
        ("LO_SPEC", false),
        ("HI_SPEC", false),
        ("RES_SCAL", false),
        ("LLM_SCAL", false),
        ("HLM_SCAL", false),
    ];

    pub(crate) const FTR_FIELD: [(&str, bool); 26] = [
        ("TEST_NUM", true),
        ("HEAD_NUM", true),
        ("SITE_NUM", true),
        ("Pass/Fail", true),
        ("AlarmFlags", false),
        ("VECT_NAM", false),
        ("TIME_SET", false),
        ("CYCL_CNT", false),
        ("REL_VADR", false),
        ("REPT_CNT", false),
        ("NUM_FAIL", false),
        ("XFAIL_AD", false),
        ("YFAIL_AD", false),
        ("VECT_OFF", false),
        ("RTN_INDX", false),
        ("RTN_STAT", false),
        ("PGM_INDX", false),
        ("PGM_STAT", false),
        ("FAIL_PIN", false),
        ("OP_CODE", false),
        ("TEST_TXT", false),
        ("ALARM_ID", false),
        ("PROG_TXT", false),
        ("RSLT_TXT", false),
        ("PATG_NUM", false),
        ("SPIN_MAP", false),
    ];

    pub(crate) const BPS_FIELD: [(&str, bool); 1] = [("SEQ_NAME", false)];
    pub(crate) const EPS_FIELD: [(&str, bool); 0] = [];
    // GDR is a special case, there is only GEN_DATA, however it's data is delimited by | symbol
    pub(crate) const GDR_FIELD: [(&str, bool); 0] = [];

    pub(crate) const DTR_FIELD: [(&str, bool); 1] = [("TEST_DAT", false)];

    pub(crate) const INVALID_FIELD: [(&str, bool); 0] = [];
}

#[derive(Debug)]
pub struct AtdfRecord {
    rec_name: String,
    type_code: u64,
    scale_flag: bool, // currently not used... maybe in the future
    data_map: HashMap<String, String>,
}

impl From<&AtdfRecord> for StdfRecord {
    #[inline(always)]
    fn from(atdf_rec: &AtdfRecord) -> Self {
        //TODO
        if atdf_rec.scale_flag {}
        StdfRecord::new(atdf_rec.type_code)
    }
}

impl AtdfRecord {
    #[inline(always)]
    pub fn from_atdf_string(
        atdf_str: &str,
        delim: char,
        scale_flag: bool,
    ) -> Result<Self, StdfError> {
        // do some ATDF syntax checking here, start parsing ATDF rec
        let (rec_name, rec_data) = atdf_str.split_once(':').unwrap_or(("", atdf_str));
        let type_code = get_code_from_rec_name(rec_name);
        if type_code == REC_INVALID {
            return Err(StdfError {
                code: 2,
                msg: format!(
                    "Unrecognized record name {}, remaining data {}",
                    rec_name, rec_data
                ),
            });
        }
        // map data to each atdf fields, use empty string as default field data
        let field_data: Vec<&str> = rec_data.split(delim).collect();
        let field_name = get_atdf_fields(type_code);
        // check required fields exist
        if field_data.len() < count_reqired(field_name) {
            return Err(StdfError {
                code: 2,
                msg: format!(
                    "{} record has {} required fields, only {} found in {:?}",
                    rec_name,
                    count_reqired(field_name),
                    field_data.len(),
                    field_data
                ),
            });
        }
        let data_map = if type_code == REC_GDR {
            // GDR is a special case, data is split with delimiter
            (0..field_data.len())
                .zip(field_data)
                .map(|(i, d)| (i.to_string(), d.to_string()))
                .collect()
        } else {
            field_name
                .iter()
                .enumerate()
                .map(|(i, &(fname, _))| {
                    (
                        fname.to_string(),
                        field_data.get(i).unwrap_or(&"").to_string(),
                    )
                })
                .collect()
        };
        Ok(AtdfRecord {
            rec_name: rec_name.to_string(),
            type_code,
            scale_flag,
            data_map,
        })
    }

    #[inline(always)]
    pub fn to_atdf_string(&self) -> String {
        let field_name = get_atdf_fields(self.type_code);
        let rec_data = if self.type_code == REC_GDR {
            (0..self.data_map.len())
                .map(|num| num.to_string())
                .map(|num_str| {
                    self.data_map
                        .get(&num_str)
                        .unwrap_or(&String::from(""))
                        .clone()
                })
                .collect::<Vec<String>>()
                .join("|")
        } else {
            field_name
                .iter()
                .map(|&(nam, _b)| self.data_map.get(nam).unwrap_or(&String::from("")).clone())
                .collect::<Vec<String>>()
                .join("|")
        };
        format!("{}:{}", self.rec_name, rec_data)
    }
}

impl From<&StdfRecord> for AtdfRecord {
    /// Records introduced in V4-2007 **CANNOT**
    /// be converted to ATDF.
    ///
    /// If you have ATDF spec for V4-2007, it would
    /// be most helpful for me to dev the full feature.
    #[inline(always)]
    fn from(stdf_rec: &StdfRecord) -> Self {
        let type_code;
        let rec_name;
        let atdf_fields: &[(&str, bool)];
        let data_list;

        match stdf_rec {
            // rec type 15
            StdfRecord::PTR(rec) => {
                type_code = REC_PTR;
                rec_name = "PTR".to_string();
                atdf_fields = &PTR_FIELD;
                data_list = atdf_data_from_ptr(rec);
            }
            StdfRecord::MPR(rec) => {
                type_code = REC_MPR;
                rec_name = "MPR".to_string();
                atdf_fields = &MPR_FIELD;
                data_list = atdf_data_from_mpr(rec);
            }
            StdfRecord::FTR(rec) => {
                type_code = REC_FTR;
                rec_name = "FTR".to_string();
                atdf_fields = &FTR_FIELD;
                data_list = atdf_data_from_ftr(rec);
            }
            // StdfRecord::STR(rec) => {
            //     type_code = REC_STR;
            //     rec_name = "STR".to_string();
            //     atdf_fields = &STR_FIELD;
            //     data_list = atdf_data_from_str_rec(rec);
            // }
            // rec type 5
            StdfRecord::PIR(rec) => {
                type_code = REC_PIR;
                rec_name = "PIR".to_string();
                atdf_fields = &PIR_FIELD;
                data_list = atdf_data_from_pir(rec);
            }

            StdfRecord::PRR(rec) => {
                type_code = REC_PRR;
                rec_name = "PRR".to_string();
                atdf_fields = &PRR_FIELD;
                data_list = atdf_data_from_prr(rec);
            }
            // rec type 2
            StdfRecord::WIR(rec) => {
                type_code = REC_WIR;
                rec_name = "WIR".to_string();
                atdf_fields = &WIR_FIELD;
                data_list = atdf_data_from_wir(rec);
            }
            StdfRecord::WRR(rec) => {
                type_code = REC_WRR;
                rec_name = "WRR".to_string();
                atdf_fields = &WRR_FIELD;
                data_list = atdf_data_from_wrr(rec);
            }
            StdfRecord::WCR(rec) => {
                type_code = REC_WCR;
                rec_name = "WCR".to_string();
                atdf_fields = &WCR_FIELD;
                data_list = atdf_data_from_wcr(rec);
            }
            // rec type 50
            StdfRecord::GDR(rec) => {
                type_code = REC_GDR;
                rec_name = "GDR".to_string();
                atdf_fields = &GDR_FIELD;
                data_list = atdf_data_from_gdr(rec);
            }
            StdfRecord::DTR(rec) => {
                type_code = REC_DTR;
                rec_name = "DTR".to_string();
                atdf_fields = &DTR_FIELD;
                data_list = atdf_data_from_dtr(rec);
            }
            // rec type 10
            StdfRecord::TSR(rec) => {
                type_code = REC_TSR;
                rec_name = "TSR".to_string();
                atdf_fields = &TSR_FIELD;
                data_list = atdf_data_from_tsr(rec);
            }
            // rec type 1
            StdfRecord::MIR(rec) => {
                type_code = REC_MIR;
                rec_name = "MIR".to_string();
                atdf_fields = &MIR_FIELD;
                data_list = atdf_data_from_mir(rec);
            }
            StdfRecord::MRR(rec) => {
                type_code = REC_MRR;
                rec_name = "MRR".to_string();
                atdf_fields = &MRR_FIELD;
                data_list = atdf_data_from_mrr(rec);
            }
            StdfRecord::PCR(rec) => {
                type_code = REC_PCR;
                rec_name = "PCR".to_string();
                atdf_fields = &PCR_FIELD;
                data_list = atdf_data_from_pcr(rec);
            }
            StdfRecord::HBR(rec) => {
                type_code = REC_HBR;
                rec_name = "HBR".to_string();
                atdf_fields = &HBR_FIELD;
                data_list = atdf_data_from_hbr(rec);
            }
            StdfRecord::SBR(rec) => {
                type_code = REC_SBR;
                rec_name = "SBR".to_string();
                atdf_fields = &SBR_FIELD;
                data_list = atdf_data_from_sbr(rec);
            }
            StdfRecord::PMR(rec) => {
                type_code = REC_PMR;
                rec_name = "PMR".to_string();
                atdf_fields = &PMR_FIELD;
                data_list = atdf_data_from_pmr(rec);
            }
            StdfRecord::PGR(rec) => {
                type_code = REC_PGR;
                rec_name = "PGR".to_string();
                atdf_fields = &PGR_FIELD;
                data_list = atdf_data_from_pgr(rec);
            }
            StdfRecord::PLR(rec) => {
                type_code = REC_PLR;
                rec_name = "PLR".to_string();
                atdf_fields = &PLR_FIELD;
                data_list = atdf_data_from_plr(rec);
            }
            StdfRecord::RDR(rec) => {
                type_code = REC_RDR;
                rec_name = "RDR".to_string();
                atdf_fields = &RDR_FIELD;
                data_list = atdf_data_from_rdr(rec);
            }
            StdfRecord::SDR(rec) => {
                type_code = REC_SDR;
                rec_name = "SDR".to_string();
                atdf_fields = &SDR_FIELD;
                data_list = atdf_data_from_sdr(rec);
            }
            // StdfRecord::PSR(rec) => {
            //     type_code = REC_PSR;
            //     rec_name = "PSR".to_string();
            //     atdf_fields = &PSR_FIELD;
            //     data_list = atdf_data_from_psr(rec);
            // }
            // StdfRecord::NMR(rec) => {
            //     type_code = REC_NMR;
            //     rec_name = "NMR".to_string();
            //     atdf_fields = &NMR_FIELD;
            //     data_list = atdf_data_from_nmr(rec);
            // }
            // StdfRecord::CNR(rec) => {
            //     type_code = REC_CNR;
            //     rec_name = "CNR".to_string();
            //     atdf_fields = &CNR_FIELD;
            //     data_list = atdf_data_from_cnr(rec);
            // }
            // StdfRecord::SSR(rec) => {
            //     type_code = REC_SSR;
            //     rec_name = "SSR".to_string();
            //     atdf_fields = &SSR_FIELD;
            //     data_list = atdf_data_from_ssr(rec);
            // }
            // StdfRecord::CDR(rec) => {
            //     type_code = REC_CDR;
            //     rec_name = "CDR".to_string();
            //     atdf_fields = &CDR_FIELD;
            //     data_list = atdf_data_from_cdr(rec);
            // }
            // rec type 0
            StdfRecord::FAR(rec) => {
                type_code = REC_FAR;
                rec_name = "FAR".to_string();
                atdf_fields = &FAR_FIELD;
                data_list = atdf_data_from_far(rec);
            }
            StdfRecord::ATR(rec) => {
                type_code = REC_ATR;
                rec_name = "ATR".to_string();
                atdf_fields = &ATR_FIELD;
                data_list = atdf_data_from_atr(rec);
            }
            // StdfRecord::VUR(rec) => {
            //     type_code = REC_VUR;
            //     rec_name = "VUR".to_string();
            //     atdf_fields = &VUR_FIELD;
            //     data_list = atdf_data_from_vur(rec);
            // }
            // rec type 20
            StdfRecord::BPS(rec) => {
                type_code = REC_BPS;
                rec_name = "BPS".to_string();
                atdf_fields = &BPS_FIELD;
                data_list = atdf_data_from_bps(rec);
            }
            StdfRecord::EPS(rec) => {
                type_code = REC_EPS;
                rec_name = "EPS".to_string();
                atdf_fields = &EPS_FIELD;
                data_list = atdf_data_from_eps(rec);
            }
            // rec type 180: Reserved
            // rec type 181: Reserved
            // not matched
            _ => {
                type_code = REC_INVALID;
                rec_name = "INVALID".to_string();
                atdf_fields = &INVALID_FIELD;
                data_list = vec![];
            }
        };

        AtdfRecord {
            rec_name,
            type_code,
            scale_flag: false, // default Unscale
            data_map: if type_code == REC_GDR {
                create_atdf_gdr_map(data_list)
            } else {
                create_atdf_map_from_fields_and_data(atdf_fields, data_list)
            },
        }
    }
}

// ATDF help functions

#[inline(always)]
pub(crate) fn get_atdf_fields(rec_type: u64) -> &'static [(&'static str, bool)] {
    match rec_type {
        REC_FAR => &FAR_FIELD,
        REC_ATR => &ATR_FIELD,
        // REC_VUR => &VUR_FIELD,
        REC_MIR => &MIR_FIELD,
        REC_MRR => &MRR_FIELD,
        REC_PCR => &PCR_FIELD,
        REC_HBR => &HBR_FIELD,
        REC_SBR => &SBR_FIELD,
        REC_PMR => &PMR_FIELD,
        REC_PGR => &PGR_FIELD,
        REC_PLR => &PLR_FIELD,
        REC_RDR => &RDR_FIELD,
        REC_SDR => &SDR_FIELD,
        // REC_PSR => &PSR,
        // REC_NMR => &NMR,
        // REC_CNR => &CNR,
        // REC_SSR => &SSR,
        // REC_CDR => &CDR,
        REC_WIR => &WIR_FIELD,
        REC_WRR => &WRR_FIELD,
        REC_WCR => &WCR_FIELD,
        REC_PIR => &PIR_FIELD,
        REC_PRR => &PRR_FIELD,
        REC_TSR => &TSR_FIELD,
        REC_PTR => &PTR_FIELD,
        REC_MPR => &MPR_FIELD,
        REC_FTR => &FTR_FIELD,
        // REC_STR => &STR_FIELD,
        REC_BPS => &BPS_FIELD,
        REC_EPS => &EPS_FIELD,
        REC_GDR => &GDR_FIELD,
        REC_DTR => &DTR_FIELD,
        _ => &INVALID_FIELD,
    }
}

#[inline(always)]
pub(crate) fn count_reqired(p_arr: &[(&str, bool)]) -> usize {
    p_arr
        .iter()
        .fold(0, |cnt: usize, (_, b)| cnt + (*b as usize))
}

// STDF -> ATDF convertion help functions
// parameter test value will be scaled by default

#[inline(always)]
pub(crate) fn atdf_data_from_ptr(rec: &PTR) -> Vec<String> {
    let test_bits = flag_to_array(&rec.test_flg);
    let parm_bits = flag_to_array(&rec.parm_flg);
    let mut alarm_flags = "".to_string();
    if test_bits[0] == 1 {
        alarm_flags.push('A')
    }
    if test_bits[2] == 1 {
        alarm_flags.push('U')
    }
    if test_bits[3] == 1 {
        alarm_flags.push('T')
    }
    if test_bits[4] == 1 {
        alarm_flags.push('N')
    }
    if test_bits[5] == 1 {
        alarm_flags.push('X')
    }
    if parm_bits[0] == 1 {
        alarm_flags.push('S')
    }
    if parm_bits[1] == 1 {
        alarm_flags.push('D')
    }
    if parm_bits[2] == 1 {
        alarm_flags.push('O')
    }
    if parm_bits[3] == 1 {
        alarm_flags.push('H')
    }
    if parm_bits[4] == 1 {
        alarm_flags.push('L')
    }

    vec![
        rec.test_num.to_string(), //TEST_NUM
        rec.head_num.to_string(), //HEAD_NUM
        rec.site_num.to_string(), //SITE_NUM
        rec.result.to_string(),   //RESULT
        //Pass/Fail, TEST_FLG bits 6 & 7, PARM_FLG bit 5
        if parm_bits[5] == 1 {
            "A".to_string()
        } else if test_bits[6] | test_bits[7] == 0 {
            "P".to_string()
        } else if test_bits[6] == 1 {
            "".to_string()
        } else {
            "F".to_string()
        },
        alarm_flags,          //AlarmFlags
        rec.test_txt.clone(), //TEST_TXT
        rec.alarm_id.clone(), //ALARM_ID
        //LimitCompare
        if parm_bits[6] | parm_bits[7] == 0 {
            "".to_string()
        } else if parm_bits[6] == 1 {
            ">=".to_string()
        } else {
            "<=".to_string()
        },
        ser_optional!(rec.units, clone),        //UNITS
        ser_optional!(rec.lo_limit, to_string), //LO_LIMIT
        ser_optional!(rec.hi_limit, to_string), //HI_LIMIT
        ser_optional!(rec.c_resfmt, clone),     //C_RESFMT
        ser_optional!(rec.c_llmfmt, clone),     //C_LLMFMT
        ser_optional!(rec.c_hlmfmt, clone),     //C_HLMFMT
        ser_optional!(rec.lo_spec, to_string),  //LO_SPEC
        ser_optional!(rec.hi_spec, to_string),  //HI_SPEC
        ser_optional!(rec.res_scal, to_string), //RES_SCAL
        ser_optional!(rec.llm_scal, to_string), //LLM_SCAL
        ser_optional!(rec.hlm_scal, to_string), //HLM_SCAL
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_mpr(rec: &MPR) -> Vec<String> {
    let test_bits = flag_to_array(&rec.test_flg);
    let parm_bits = flag_to_array(&rec.parm_flg);
    let mut alarm_flags = "".to_string();
    if test_bits[0] == 1 {
        alarm_flags.push('A')
    }
    if test_bits[2] == 1 {
        alarm_flags.push('U')
    }
    if test_bits[3] == 1 {
        alarm_flags.push('T')
    }
    if test_bits[4] == 1 {
        alarm_flags.push('N')
    }
    if test_bits[5] == 1 {
        alarm_flags.push('X')
    }
    if parm_bits[0] == 1 {
        alarm_flags.push('S')
    }
    if parm_bits[1] == 1 {
        alarm_flags.push('D')
    }
    if parm_bits[2] == 1 {
        alarm_flags.push('O')
    }
    if parm_bits[3] == 1 {
        alarm_flags.push('H')
    }
    if parm_bits[4] == 1 {
        alarm_flags.push('L')
    }

    vec![
        rec.test_num.to_string(),        //TEST_NUM
        rec.head_num.to_string(),        //HEAD_NUM
        rec.site_num.to_string(),        //SITE_NUM
        ser_kx_digit_hex(&rec.rtn_stat), //RTN_STAT
        ser_stdf_kx_data(&rec.rtn_rslt), //RTN_RSLT
        //Pass/Fail, TEST_FLG bits 6 & 7, PARM_FLG bit 5
        if parm_bits[5] == 1 {
            "A".to_string()
        } else if test_bits[6] | test_bits[7] == 0 {
            "P".to_string()
        } else if test_bits[6] == 1 {
            "".to_string()
        } else {
            "F".to_string()
        },
        alarm_flags,          //AlarmFlags
        rec.test_txt.clone(), //TEST_TXT
        rec.alarm_id.clone(), //ALARM_ID
        //LimitCompare
        if parm_bits[6] | parm_bits[7] == 0 {
            "".to_string()
        } else if parm_bits[6] == 1 {
            ">=".to_string()
        } else {
            "<=".to_string()
        },
        ser_optional!(rec.units, clone),                  //UNITS
        ser_optional!(rec.lo_limit, to_string),           //LO_LIMIT
        ser_optional!(rec.hi_limit, to_string),           //HI_LIMIT
        ser_optional!(rec.start_in, to_string),           //START_IN
        ser_optional!(rec.incr_in, to_string),            //INCR_IN
        ser_optional!(rec.units_in, clone),               //UNITS_IN
        ser_optional!(&rec.rtn_indx, ser_stdf_kx_data()), //RTN_INDX
        ser_optional!(rec.c_resfmt, clone),               //C_RESFMT
        ser_optional!(rec.c_llmfmt, clone),               //C_LLMFMT
        ser_optional!(rec.c_hlmfmt, clone),               //C_HLMFMT
        ser_optional!(rec.lo_spec, to_string),            //LO_SPEC
        ser_optional!(rec.hi_spec, to_string),            //HI_SPEC
        ser_optional!(rec.res_scal, to_string),           //RES_SCAL
        ser_optional!(rec.llm_scal, to_string),           //LLM_SCAL
        ser_optional!(rec.hlm_scal, to_string),           //HLM_SCAL
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_ftr(rec: &FTR) -> Vec<String> {
    let test_bits = flag_to_array(&rec.test_flg);
    let mut alarm_flags = "".to_string();
    if test_bits[0] == 1 {
        alarm_flags.push('A')
    }
    if test_bits[2] == 1 {
        alarm_flags.push('N')
    }
    if test_bits[3] == 1 {
        alarm_flags.push('T')
    }
    if test_bits[4] == 1 {
        alarm_flags.push('U')
    }
    if test_bits[5] == 1 {
        alarm_flags.push('X')
    }

    vec![
        rec.test_num.to_string(), //TEST_NUM
        rec.head_num.to_string(), //HEAD_NUM
        rec.site_num.to_string(), //SITE_NUM
        //Pass/Fail
        if test_bits[6] | test_bits[7] == 0 {
            "P".to_string()
        } else if test_bits[6] == 1 {
            "".to_string()
        } else {
            "F".to_string()
        },
        alarm_flags,                     //AlarmFlags
        rec.vect_nam.clone(),            //VECT_NAM
        rec.time_set.clone(),            //TIME_SET
        rec.cycl_cnt.to_string(),        //CYCL_CNT
        rec.rel_vadr.to_string(),        //REL_VADR
        rec.rept_cnt.to_string(),        //REPT_CNT
        rec.num_fail.to_string(),        //NUM_FAIL
        rec.xfail_ad.to_string(),        //XFAIL_AD
        rec.yfail_ad.to_string(),        //YFAIL_AD
        rec.vect_off.to_string(),        //VECT_OFF
        ser_stdf_kx_data(&rec.rtn_indx), //RTN_INDX
        ser_kx_digit_hex(&rec.rtn_stat), //RTN_STAT
        ser_stdf_kx_data(&rec.pgm_indx), //PGM_INDX
        ser_kx_digit_hex(&rec.pgm_stat), //PGM_STAT
        ser_stdf_kx_data(&rec.fail_pin), //FAIL_PIN
        rec.op_code.clone(),             //OP_CODE
        rec.test_txt.clone(),            //TEST_TXT
        rec.alarm_id.clone(),            //ALARM_ID
        rec.prog_txt.clone(),            //PROG_TXT
        rec.rslt_txt.clone(),            //RSLT_TXT
        rec.patg_num.to_string(),        //PATG_NUM
        ser_stdf_kx_data(&rec.spin_map), //SPIN_MAP
    ]
}

/// ignored because I do not know ATDF structure in V4-2007
// pub(crate) fn atdf_data_from_str_rec(rec: &STR) -> Vec<String>  {
//     vec![]}

#[inline(always)]
pub(crate) fn atdf_data_from_pir(rec: &PIR) -> Vec<String> {
    vec![
        rec.head_num.to_string(), //HEAD_NUM
        rec.site_num.to_string(), //SITE_NUM
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_prr(rec: &PRR) -> Vec<String> {
    let flg_bits = flag_to_array(&rec.part_flg);
    vec![
        rec.head_num.to_string(), //HEAD_NUM
        rec.site_num.to_string(), //SITE_NUM
        rec.part_id.clone(),      //PART_ID
        rec.num_test.to_string(), //NUM_TEST
        //Pass/Fail, bits 3 & 4
        if flg_bits[3] | flg_bits[4] == 0 {
            "P".to_string()
        } else {
            "F".to_string()
        },
        rec.hard_bin.to_string(), //HARD_BIN
        rec.soft_bin.to_string(), //SOFT_BIN
        rec.x_coord.to_string(),  //X_COORD
        rec.y_coord.to_string(),  //Y_COORD
        //RetestCode, bit 0 or 1
        if flg_bits[0] | flg_bits[1] != 0 {
            if flg_bits[0] != 0 {
                "I".to_string()
            } else {
                "C".to_string()
            }
        } else {
            "".to_string()
        },
        //AbortCode
        if flg_bits[2] == 0 {
            "".to_string()
        } else {
            "Y".to_string()
        },
        rec.test_t.to_string(),   //TEST_T
        rec.part_txt.clone(),     //PART_TXT
        ser_bn_dn(&rec.part_fix), //PART_FIX
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_wir(rec: &WIR) -> Vec<String> {
    vec![
        rec.head_num.to_string(), //HEAD_NUM
        rec.start_t.to_string(),  //START_T
        rec.site_grp.to_string(), //SITE_GRP
        rec.wafer_id.clone(),     //WAFER_ID
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_wrr(rec: &WRR) -> Vec<String> {
    vec![
        rec.head_num.to_string(), //HEAD_NUM
        rec.finish_t.to_string(), //FINISH_T
        rec.part_cnt.to_string(), //PART_CNT
        rec.wafer_id.clone(),     //WAFER_ID
        rec.site_grp.to_string(), //SITE_GRP
        rec.rtst_cnt.to_string(), //RTST_CNT
        rec.abrt_cnt.to_string(), //ABRT_CNT
        rec.good_cnt.to_string(), //GOOD_CNT
        rec.func_cnt.to_string(), //FUNC_CNT
        rec.fabwf_id.clone(),     //FABWF_ID
        rec.frame_id.clone(),     //FRAME_ID
        rec.mask_id.clone(),      //MASK_ID
        rec.usr_desc.clone(),     //USR_DESC
        rec.exc_desc.clone(),     //EXC_DESC
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_wcr(rec: &WCR) -> Vec<String> {
    vec![
        rec.wf_flat.to_string(),  //WF_FLAT
        rec.pos_x.to_string(),    //POS_X
        rec.pos_y.to_string(),    //POS_Y
        rec.wafr_siz.to_string(), //WAFR_SIZ
        rec.die_ht.to_string(),   //DIE_HT
        rec.die_wid.to_string(),  //DIE_WID
        rec.wf_units.to_string(), //WF_UNITS
        rec.center_x.to_string(), //CENTER_X
        rec.center_y.to_string(), //CENTER_Y
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_gdr(rec: &GDR) -> Vec<String> {
    let mut gen_data_list = vec![];
    for v1_data in &rec.gen_data {
        match v1_data {
            V1::U1(u1) => gen_data_list.push(format!("U{}", u1)),
            V1::U2(u2) => gen_data_list.push(format!("M{}", u2)),
            V1::U4(u4) => gen_data_list.push(format!("B{}", u4)),
            V1::I1(i1) => gen_data_list.push(format!("I{}", i1)),
            V1::I2(i2) => gen_data_list.push(format!("S{}", i2)),
            V1::I4(i4) => gen_data_list.push(format!("L{}", i4)),
            V1::R4(r4) => gen_data_list.push(format!("F{}", r4)),
            V1::R8(r8) => gen_data_list.push(format!("D{}", r8)),
            V1::Cn(cn) => gen_data_list.push(format!("T{}", cn)),
            V1::Bn(bn) => gen_data_list.push(format!("X{}", ser_bn_dn(bn))),
            V1::Dn(dn) => gen_data_list.push(format!("Y{}", ser_bn_dn(dn))),
            V1::N1(n1) => gen_data_list.push(format!("N{}", n1)),
            // No pad bytes in ATDF
            _ => {
                continue;
            }
        }
    }
    gen_data_list
}

#[inline(always)]
pub(crate) fn atdf_data_from_dtr(rec: &DTR) -> Vec<String> {
    vec![
        rec.text_dat.clone(), // TEST_DAT
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_tsr(rec: &TSR) -> Vec<String> {
    vec![
        //HEAD_NUM
        if rec.head_num == 255 {
            "".to_string()
        } else {
            rec.head_num.to_string()
        },
        //SITE_NUM
        if rec.site_num == 255 {
            "".to_string()
        } else {
            rec.site_num.to_string()
        },
        rec.test_num.to_string(), //TEST_NUM
        rec.test_nam.clone(),     //TEST_NAM
        rec.test_typ.to_string(), //TEST_TYP
        rec.exec_cnt.to_string(), //EXEC_CNT
        rec.fail_cnt.to_string(), //FAIL_CNT
        rec.alrm_cnt.to_string(), //ALRM_CNT
        rec.seq_name.clone(),     //SEQ_NAME
        rec.test_lbl.clone(),     //TEST_LBL
        rec.test_tim.to_string(), //TEST_TIM
        rec.test_min.to_string(), //TEST_MIN
        rec.test_max.to_string(), //TEST_MAX
        rec.tst_sums.to_string(), //TST_SUMS
        rec.tst_sqrs.to_string(), //TST_SQRS
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_mir(rec: &MIR) -> Vec<String> {
    vec![
        rec.lot_id.clone(),   //LOT_ID
        rec.part_typ.clone(), //PART_TYP
        rec.job_nam.clone(),  //JOB_NAM
        rec.node_nam.clone(), //NODE_NAM
        rec.tstr_typ.clone(), //TSTR_TYP
        NaiveDateTime::from_timestamp_opt(rec.setup_t as i64, 0)
            .expect("invalid or out-of-range datetime")
            .format("%H:%M:%S %d-%b-%Y")
            .to_string(), //SETUP_T
        NaiveDateTime::from_timestamp_opt(rec.start_t as i64, 0)
            .expect("invalid or out-of-range datetime")
            .format("%H:%M:%S %d-%b-%Y")
            .to_string(), //START_T
        rec.oper_nam.clone(), //OPER_NAM
        rec.mode_cod.to_string(), //MODE_COD
        rec.stat_num.to_string(), //STAT_NUM
        rec.sblot_id.clone(), //SBLOT_ID
        rec.test_cod.clone(), //TEST_COD
        rec.rtst_cod.to_string(), //RTST_COD
        rec.job_rev.clone(),  //JOB_REV
        rec.exec_typ.clone(), //EXEC_TYP
        rec.exec_ver.clone(), //EXEC_VER
        rec.prot_cod.to_string(), //PROT_COD
        rec.cmod_cod.to_string(), //CMOD_COD
        rec.burn_tim.to_string(), //BURN_TIM
        rec.tst_temp.clone(), //TST_TEMP
        rec.user_txt.clone(), //USER_TXT
        rec.aux_file.clone(), //AUX_FILE
        rec.pkg_typ.clone(),  //PKG_TYP
        rec.famly_id.clone(), //FAMLY_ID
        rec.date_cod.clone(), //DATE_COD
        rec.facil_id.clone(), //FACIL_ID
        rec.floor_id.clone(), //FLOOR_ID
        rec.proc_id.clone(),  //PROC_ID
        rec.oper_frq.clone(), //OPER_FRQ
        rec.spec_nam.clone(), //SPEC_NAM
        rec.spec_ver.clone(), //SPEC_VER
        rec.flow_id.clone(),  //FLOW_ID
        rec.setup_id.clone(), //SETUP_ID
        rec.dsgn_rev.clone(), //DSGN_REV
        rec.eng_id.clone(),   //ENG_ID
        rec.rom_cod.clone(),  //ROM_COD
        rec.serl_num.clone(), //SERL_NUM
        rec.supr_nam.clone(), //SUPR_NAM
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_mrr(rec: &MRR) -> Vec<String> {
    vec![
        NaiveDateTime::from_timestamp_opt(rec.finish_t as i64, 0)
            .expect("invalid or out-of-range datetime")
            .format("%H:%M:%S %d-%b-%Y")
            .to_string(), //FINISH_T
        rec.disp_cod.to_string(), //DISP_COD
        rec.usr_desc.clone(),     //USR_DESC
        rec.exc_desc.clone(),     //EXC_DESC
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_pcr(rec: &PCR) -> Vec<String> {
    vec![
        if rec.head_num == 255 {
            "".to_string()
        } else {
            rec.head_num.to_string()
        }, //HEAD_NUM
        if rec.site_num == 255 {
            "".to_string()
        } else {
            rec.site_num.to_string()
        }, //SITE_NUM
        rec.part_cnt.to_string(), //PART_CNT
        rec.rtst_cnt.to_string(), //RTST_CNT
        rec.abrt_cnt.to_string(), //ABRT_CNT
        rec.good_cnt.to_string(), //GOOD_CNT
        rec.func_cnt.to_string(), //FUNC_CNT
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_hbr(rec: &HBR) -> Vec<String> {
    vec![
        if rec.head_num == 255 {
            "".to_string()
        } else {
            rec.head_num.to_string()
        }, //HEAD_NUM
        if rec.site_num == 255 {
            "".to_string()
        } else {
            rec.site_num.to_string()
        }, //SITE_NUM
        rec.hbin_num.to_string(), //HBIN_NUM
        rec.hbin_cnt.to_string(), //HBIN_CNT
        rec.hbin_pf.to_string(),  //HBIN_PF
        rec.hbin_nam.clone(),     //HBIN_NAM
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_sbr(rec: &SBR) -> Vec<String> {
    vec![
        if rec.head_num == 255 {
            "".to_string()
        } else {
            rec.head_num.to_string()
        }, //HEAD_NUM
        if rec.site_num == 255 {
            "".to_string()
        } else {
            rec.site_num.to_string()
        }, //SITE_NUM
        rec.sbin_num.to_string(), //SBIN_NUM
        rec.sbin_cnt.to_string(), //SBIN_CNT
        rec.sbin_pf.to_string(),  //SBIN_PF
        rec.sbin_nam.clone(),     //SBIN_NAM
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_pmr(rec: &PMR) -> Vec<String> {
    vec![
        rec.pmr_indx.to_string(), //PMR_INDX
        rec.chan_typ.to_string(), //CHAN_TYP
        rec.chan_nam.clone(),     //CHAN_NAM
        rec.phy_nam.clone(),      //PHY_NAM
        rec.log_nam.clone(),      //LOG_NAM
        rec.head_num.to_string(), //HEAD_NUM
        rec.site_num.to_string(), //SITE_NUM
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_pgr(rec: &PGR) -> Vec<String> {
    vec![
        rec.grp_indx.to_string(),        //GRP_INDX
        rec.grp_nam.clone(),             //GRP_NAM
        ser_stdf_kx_data(&rec.pmr_indx), //PMR_INDX
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_plr(rec: &PLR) -> Vec<String> {
    // convert radx to ASCII symbol
    let radx_func = |x: u8| match x {
        2 => "B",
        8 => "O",
        10 => "D",
        16 => "H",
        20 => "S",
        _ => "",
    };
    let grp_mode = rec
        .grp_mode
        .iter()
        .map(|&x| format!("{:X}", x))
        .collect::<Vec<String>>()
        .join(",");
    let grp_radx = rec
        .grp_radx
        .iter()
        .map(|&x| radx_func(x).to_string())
        .collect::<Vec<String>>()
        .join(",");

    // check if string length is > 0
    fn not_empty(v: &[String]) -> bool {
        v.iter().map(|s| s.len()).fold(0, std::cmp::max) > 0
    }

    fn combine_l_r(cha_l: &[String], cha_r: &[String]) -> String {
        let chal_not_empty = not_empty(cha_l);
        let char_not_empty = not_empty(cha_r);

        if chal_not_empty | char_not_empty {
            let mut state_list = vec![];
            // if cha_l is not empty, two characters will be in `state`
            if chal_not_empty {
                // e.g. l = ["1111", "2222"], r = ["3333", "4444"]
                for (string_l, string_r) in cha_l.iter().zip(cha_r.iter()) {
                    // e.g. "1111", "2222"
                    let mut tmp_pin_state = "".to_string();
                    for (ind, (c_l, c_r)) in string_l.chars().zip(string_r.chars()).enumerate() {
                        // e.g. '1', '2'
                        if ind != 0 {
                            tmp_pin_state.push(',');
                        }
                        tmp_pin_state.push(c_l);
                        tmp_pin_state.push(c_r);
                    }
                    // e.g. "12,12,12,12"
                    state_list.push(tmp_pin_state);
                }
            } else {
                // only cha_r
                cha_r
                    .iter()
                    .map(|s| {
                        s.chars()
                            .map(|c| c.to_string())
                            .collect::<Vec<String>>()
                            .join(",")
                    })
                    .map(|pin_state| state_list.push(pin_state))
                    .count();
            }
            state_list.join("/")
        } else {
            "".to_string()
        }
    }

    vec![
        ser_stdf_kx_data(&rec.grp_indx),           // ("GRP_INDX", true),
        grp_mode,                                  // ("GRP_MODE", false),
        grp_radx,                                  // ("GRP_RADX", false),
        combine_l_r(&rec.pgm_chal, &rec.pgm_char), // ("PGM_CHAL,PGM_CHAR", false),
        combine_l_r(&rec.rtn_chal, &rec.rtn_char), // ("RTN_CHAL,RTN_CHAR", false),
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_rdr(rec: &RDR) -> Vec<String> {
    vec![
        ser_stdf_kx_data(&rec.rtst_bin), //RTST_BIN
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_sdr(rec: &SDR) -> Vec<String> {
    vec![
        rec.head_num.to_string(),        //HEAD_NUM
        rec.site_grp.to_string(),        //SITE_GRP
        ser_stdf_kx_data(&rec.site_num), //SITE_NUM
        rec.hand_typ.clone(),            //HAND_TYP
        rec.hand_id.clone(),             //HAND_ID
        rec.card_typ.clone(),            //CARD_TYP
        rec.card_id.clone(),             //CARD_ID
        rec.load_typ.clone(),            //LOAD_TYP
        rec.load_id.clone(),             //LOAD_ID
        rec.dib_typ.clone(),             //DIB_TYP
        rec.dib_id.clone(),              //DIB_ID
        rec.cabl_typ.clone(),            //CABL_TYP
        rec.cabl_id.clone(),             //CABL_ID
        rec.cont_typ.clone(),            //CONT_TYP
        rec.cont_id.clone(),             //CONT_ID
        rec.lasr_typ.clone(),            //LASR_TYP
        rec.lasr_id.clone(),             //LASR_ID
        rec.extr_typ.clone(),            //EXTR_TYP
        rec.extr_id.clone(),             //EXTR_ID
    ]
}

// pub(crate) fn atdf_data_from_psr(_rec: &PSR) -> Vec<String>  {vec![]}
// pub(crate) fn atdf_data_from_nmr(_rec: &NMR) -> Vec<String>  {vec![]}
// pub(crate) fn atdf_data_from_cnr(_rec: &CNR) -> Vec<String>  {vec![]}
// pub(crate) fn atdf_data_from_ssr(_rec: &SSR) -> Vec<String>  {vec![]}
// pub(crate) fn atdf_data_from_cdr(_rec: &CDR) -> Vec<String>  {vec![]}

#[inline(always)]
pub(crate) fn atdf_data_from_far(rec: &FAR) -> Vec<String> {
    vec![
        "A".to_string(),          // File type, ATDF
        rec.stdf_ver.to_string(), // STDF Version
        "2".to_string(),          // ATDF Version
        "U".to_string(),          // Unscale
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_atr(rec: &ATR) -> Vec<String> {
    vec![
        NaiveDateTime::from_timestamp_opt(rec.mod_tim as i64, 0)
            .expect("invalid or out-of-range datetime")
            .format("%H:%M:%S %d-%b-%Y")
            .to_string(), // MOD_TIM
        rec.cmd_line.clone(), // CMD_LINE
    ]
}

// pub(crate) fn atdf_data_from_vur(_rec: &VUR) -> Vec<String>  {vec![]}

#[inline(always)]
pub(crate) fn atdf_data_from_bps(rec: &BPS) -> Vec<String> {
    vec![
        rec.seq_name.clone(), // SEQ_NAME
    ]
}

#[inline(always)]
pub(crate) fn atdf_data_from_eps(_rec: &EPS) -> Vec<String> {
    vec![]
}

/// generate ATDF hashmap for records ***other than GDR***
#[inline(always)]
fn create_atdf_map_from_fields_and_data(
    fields: &[(&str, bool)],
    data_list: Vec<String>,
) -> HashMap<String, String> {
    fields
        .iter()
        .zip(data_list)
        .map(|(&(fname, _), d)| (fname.to_string(), d))
        .collect::<HashMap<String, String>>()
}

/// generate ATDF hashmap for GDR record
#[inline(always)]
fn create_atdf_gdr_map(data_list: Vec<String>) -> HashMap<String, String> {
    (0..data_list.len())
        .zip(data_list)
        .map(|(num, d)| (num.to_string(), d))
        .collect::<HashMap<String, String>>()
}

/// serialize STDF kx type data to String
#[inline(always)]
fn ser_stdf_kx_data<T: ToString>(kx: &[T]) -> String {
    kx.iter()
        .map(|x| x.to_string())
        .collect::<Vec<String>>()
        .join(",")
}

/// serialize vector of u8 to hex digit String
#[inline(always)]
fn ser_kx_digit_hex(kx: &[u8]) -> String {
    kx.iter()
        .map(|&x| format!("{:X}", x))
        .collect::<Vec<String>>()
        .join(",")
}

/// convert a 1 byte STDF flag to vector of u8
#[inline(always)]
fn flag_to_array(flag: &[u8; 1]) -> Vec<u8> {
    let mut flag = flag[0];
    let mut bits = Vec::with_capacity(8);
    for _ in 0..8 {
        bits.push(flag & 1u8);
        flag >>= 1;
    }
    bits
}

/// serialize bit data
#[inline(always)]
fn ser_bn_dn(d: &[u8]) -> String {
    hex::encode_upper(d)
}