codemelted 25.1.2

The aim of this project is to deliver a swiss army knife module to aid software engineers for building full stack solutions for their applications. Utilizing the Rust programming language, the module serves as a backbone to engineer solutions for multiple build targets.
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
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
/*
===============================================================================
MIT License

© 2025 Mark Shaffer. All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
===============================================================================
*/

#![doc(html_favicon_url = "https://codemelted.com/favicon.png")]
#![doc(html_logo_url =
  "https://codemelted.com/assets/favicon/apple-touch-icon.png")]
#![doc = include_str!("README.md")]

// ============================================================================
// [MODULE DATA DEFINITION] ===================================================
// ============================================================================

/// Defines a trait for the "rules" of objects that will setup a protocol that
/// directly exchanges data with an external item, will continuously run until
/// terminated, requires the ability to know it is running and get any errors
/// that have occurred during it run.
pub trait CProtocolHandler<T> {
  /// Identifies the protocol for debugging / reporting purposes.
  fn id(&mut self) -> String;
  /// Retrieves any currently processed messages. Also provide support for
  /// string commands to further support the given protocol you build.
  fn get_message(&mut self, request: Option<&str>) -> Result<T, std::io::Error>;
  /// Signals if the protocol is running or not.
  fn is_running(&self) -> bool;
  /// Handles the sending of the message to the protocol for processing.
  fn post_message(&mut self, data: T) -> Result<(), std::io::Error>;
  /// Signals for the protocol to terminate.
  fn terminate(&mut self);
}

// ============================================================================
// [ASYNC UC IMPLEMENTATION] ==================================================
// ============================================================================

/// The task that runs as part of the [CTaskResult] internal thread.
pub type CTaskCB<T> = fn(Option<T>) -> Option<T>;

/// The result of a [async_task] call. This holds an internal thread that will
/// call the [CTaskCB] to process specified data and return the result.
/// The [CTaskResult::has_completed] will let you know when the thread
/// has completed so you can then call [CTaskResult::value] for the
/// processed value.
pub struct CTaskResult<T> {
  /// Holds the the handle for the internal thread.
  handle: std::thread::JoinHandle<()>,

  /// Holds the receiver to wait for the process result from the [CTaskCB].
  recv: std::sync::mpsc::Receiver<Option<T>>,
}
impl<T: std::marker::Send + 'static> CTaskResult<T> {
  /// Private constructor to support the [task] function.
  fn new(task: CTaskCB<T>, data: Option<T>, delay: u64) -> CTaskResult<T> {
    // Setup our message channel.
    let (tx, rx) = std::sync::mpsc::channel::<Option<T>>();

    // Kick-off the thread to process the task.
    let handle = std::thread::spawn(move || {
      async_sleep(delay);
      let result = task(data);
      let _ = tx.send(result);
    });

    // Return the constructed object.
    CTaskResult { handle, recv: rx }
  }

  /// Indicator to whether the task thread has completed the [CTaskCB]
  pub fn has_completed(&self) -> bool {
    self.handle.is_finished()
  }

  /// Retrieves the value processed by this task. Will block if the
  /// task thread has not completed.
  pub fn value(&self) -> Option<T> {
    let result = self.recv.recv();
    match result {
      Ok(v) => {
        crate::async_sleep(100);
        v
      },
      Err(_) => None,
    }
  }
}

/// The task that runs within the [CTimerResult] thread.
pub type CTimerCB = fn();

/// The result of a [async_timer] function call. This holds the internals of the
/// thread running the [CTimerCB] until the [CTimerResult::stop] is
/// called.
pub struct CTimerResult {
  /// The handle to the internally spawned thread.
  handle: std::thread::JoinHandle<()>,
  /// The sender to support the [CTimerResult::stop] call to terminate
  /// the thread.
  sender: std::sync::mpsc::Sender<bool>
}
impl CTimerResult {
  /// Private function to create the object via the [timer] function.
  fn new(task: CTimerCB, interval: u64) -> CTimerResult {
    // Setup our channel
    let (tx, rx) = std::sync::mpsc::channel::<bool>();

    // Kick-off the internal thread loop.
    let handle = std::thread::spawn(move || {
      loop {
        async_sleep(interval);
        task();
        let result = rx.try_recv();
        if result.is_ok() {
          let stop_thread = result.ok().unwrap();
          if stop_thread {
            break;
          }
        }
      }
    });

    // Return the result object.
    CTimerResult { handle, sender: tx }
  }

  /// Indicates if the timer is running (true) or not (false).
  pub fn is_running(&self) -> bool {
    !self.handle.is_finished()
  }

  /// Terminates the internal timer thread. This blocks until fully
  /// stopped.
  pub fn stop(&self) {
    self.sender.send(true).unwrap();
    loop {
      async_sleep(100);
      if !self.is_running() {
        break;
      }
    }
  }
}

/// The task that runs within the [CWorkerProtocol] thread.
pub type CWorkerCB<T> = fn(T) -> T;

/// The result of a [async_worker] call. This holds a dedicated thread that is
/// waiting for data via the [CWorkerProtocol::post_message]. This will
/// queue of data for processing and the internal thread will process that
/// data in accordance with the [CWorkerCB]. Any processed data is available
/// via the [CProtocolHandler] bound trait functions.
pub struct CWorkerProtocol<T> {
  id: String,
  handle: std::thread::JoinHandle<()>,
  protocol_tx: Option<std::sync::mpsc::Sender<T>>,
  protocol_rx: std::sync::mpsc::Receiver<T>,
}
impl<T: std::marker::Send + 'static> CWorkerProtocol<T> {
  /// Constructs the new [CWorkerProtocol] implementing bi-directional
  /// communication with the background thread.
  fn new(id: &str, task: CWorkerCB<T>) -> CWorkerProtocol<T> {
    let (protocol_tx, thread_rx) = std::sync::mpsc::channel::<T>();
    let (thread_tx, protocol_rx) = std::sync::mpsc::channel::<T>();

    // Setup our thread and kick it off.
    let handle = std::thread::spawn(move || {
      loop {
        // Block until we get data via post_message.
        match thread_rx.recv() {
          Ok(v) => {
            let result = task(v);
            let _ = thread_tx.send(result);
          },
          // The channel was disconnected via terminate.
          // Break thread loop.
          Err(_) => break,
        }
      }
    });

    // Return the worker protocol.
    CWorkerProtocol::<T> {
      id: id.to_string(),
      handle,
      protocol_tx: Some(protocol_tx),
      protocol_rx,
    }
  }
}
impl<T> CProtocolHandler<Option<T>> for CWorkerProtocol<Option<T>> {
  fn id(&mut self) -> String {
    self.id.to_string()
  }

  fn get_message(
    &mut self,
    _request: Option<&str>
  ) -> Result<Option<T>, std::io::Error> {
    match self.protocol_rx.try_recv() {
      Ok(v) => Ok(v),
      Err(why) => {
        match why {
            std::sync::mpsc::TryRecvError::Empty => Ok(None),
            std::sync::mpsc::TryRecvError::Disconnected => {
              Err(std::io::Error::new(
                std::io::ErrorKind::BrokenPipe,
                why.to_string()
              ))
            },
        }
      },
    }
  }

  fn is_running(&self) -> bool {
    !self.handle.is_finished()
  }

  fn post_message(&mut self, data: Option<T>) -> Result<(), std::io::Error> {
    match self.protocol_tx.as_ref().unwrap().send(data) {
      Ok(_) => Ok(()),
      Err(why) => {
        Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        ))
      },
    }
  }

  fn terminate(&mut self) {
    self.protocol_tx = None;
    loop {
      async_sleep(100);
      if !self.is_running() {
        break;
      }
    }
  }
}

/// Will put a currently running thread (main or background) to sleep for
/// a specified delay in milliseconds.
///
/// **Example:**
/// ```
/// let now = std::time::Instant::now();
/// codemelted::async_sleep(2000);
/// assert!(now.elapsed() >= std::time::Duration::from_millis(2000));
/// ```
pub fn async_sleep(delay: u64) {
  let delay = std::time::Duration::from_millis(delay);
  std::thread::sleep(delay);
}

/// Creates a [CTaskResult] which runs a background thread to
/// eventually retrieve the value of the task. The data in the examples
/// are [`Option<T>`] to represent the optional data for the task and
/// optional data returned. The function is templated so you can use any
/// data type.
///
/// **Example:**
/// ```
/// use codemelted::CObject;
///
/// fn task_cb(data: Option<CObject>) -> Option<CObject> {
///   codemelted::async_sleep(1000);
///   let data_to_process = match data {
///     Some(v) => v.as_i64().unwrap(),
///     None => panic!("Why did this fail!"),
///   };
///
///   let answer = data_to_process + 42;
///   Some(CObject::from(answer))
/// }
///
/// let async_task = codemelted::async_task(
///   task_cb,
///   Some(CObject::from(24)),
///   500
/// );
/// assert!(!async_task.has_completed());
/// let answer = async_task.value();
/// assert!(async_task.has_completed());
/// assert!(answer.is_some());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_async.mmd")]
pub fn async_task<T: std::marker::Send + 'static>(
  task: CTaskCB<T>,
  data: Option<T>,
  delay: u64
) -> CTaskResult<T> {
  CTaskResult::new(task, data, delay)
}

/// Creates a repeating [CTimerCB] on the specified interval
/// (in milliseconds). The task is completed when the [CTimerResult::stop]
/// is called.
///
/// **Example:**
/// ```
/// fn timer_cb() {
///   println!("Hello");
/// }
///
/// let timer_task = codemelted::async_timer( timer_cb, 250);
/// codemelted::async_sleep(100);
/// assert!(timer_task.is_running());
/// codemelted::async_sleep(1000);
/// timer_task.stop();
/// assert!(!timer_task.is_running());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_async.mmd")]
pub fn async_timer(task: CTimerCB, interval: u64) -> CTimerResult {
  CTimerResult::new(task, interval)
}

/// Creates a [CWorkerProtocol] object that has a dedicated background
/// thread for processing any data type you specify for the [CWorkerCB]
/// loop. The task is ran when data is received via the
/// [CWorkerProtocol::post_message] call. Messages are processed in the
/// order received (First In First Out). The thread is blocked
/// (i.e. no data, not doing anything) until the protocol receives a
/// message. To get process data simply call [CWorkerProtocol::get_message].
///
/// *NOTE: This is more efficient then constant creations of [async_task]
/// calls so use this construct when you know you need a dedicated thread vs.
/// a one off task that is not frequent.*
///
/// **Example:**
/// ```
/// use codemelted::CObject;
/// use codemelted::CProtocolHandler;
///
/// fn worker_cb(_data: Option<CObject>) -> Option<CObject> {
///   // DO SOMETHING IN THE BACKGROUND
///   Some(CObject::new_object())
/// }
///
/// let mut worker = codemelted::async_worker::<Option<CObject>>(
///   "test_worker",
///   worker_cb,
/// );
///
/// assert!(worker.is_running());
/// worker.post_message(Some(CObject::new_object()));
/// codemelted::async_sleep(100);
///
/// let data = worker.get_message(None);
/// assert!(data.is_ok());
///
/// let data = worker.get_message(None);
/// assert!(data.is_ok());
///
/// worker.terminate();
/// assert!(!worker.is_running());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_async.mmd")]
pub fn async_worker<T: std::marker::Send + 'static>(
  id: &str,
  task: CWorkerCB<T>,
) -> CWorkerProtocol<T> {
  CWorkerProtocol::new(id, task)
}

// ============================================================================
// [CONSOLE UC IMPLEMENTATION] ================================================
// ============================================================================

/// Utility function to read from stdin with a specified prompt.
fn console_read(prompt: &str) -> String {
  let mut answer = String::new();
  print!("{}", prompt);
  let _ = std::io::Write::flush(&mut std::io::stdout());
  let _ = std::io::stdin().read_line(&mut answer);
  String::from(answer.trim())
}

/// Utility function to write to stdout with or without a new line.
fn console_write_stdout(message: &str, use_new_line: bool) {
  if use_new_line {
    println!("{}", message);
  } else {
    print!("{}", message);
  }
}

/// Puts out an alert to STDOUT awaiting for the user to press the ENTER
/// key.
///
/// **Example:**
/// ```no_run
/// codemelted::console_alert("Oh no it exploded!");
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_console.mmd")]
pub fn console_alert(message: &str) {
  let msg = match message {
    "" => "[ENTER]: ",
    _ => &format!("{} [ENTER]: ", message),
  };
  console_read(msg);
}

/// Prompts a user via STDIN to confirm a choice. The response will be a
/// true / false based on [CObject::is_truthy] testing of the response.
///
/// **Example:**
/// ```no_run
/// let answer = codemelted::console_confirm(
///   "Are you sure you want to do this"
/// );
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_console.mmd")]
pub fn console_confirm(message: &str) -> bool {
  let msg = match message {
    "" => "CONFIRM [y/N]: ",
    _ => &format!("{} CONFIRM [y/N]: ", message),
  };
  let answer: String = console_read(msg);
  CObject::is_truthy(&answer)
}

/// Prompts a user via STDIN to choose from a set of choices. The response
/// will be a u32 based on the selection made. Entering invalid data will
/// repeat the menu of choices until a valid selection is made.
///
/// **Example:**
/// ```no_run
/// let answer = codemelted::console_choose(
///   "Best Pet",
///   &["bird", "cat", "dog", "fish"],
/// );
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_console.mmd")]
pub fn console_choose(message: &str, choices: &[&str]) -> u32 {
  let msg = match message {
    "" => "CHOOSE",
    _ => &format!("{}", message),
  };
  let options = choices;
  let answer: u32;
  loop {
    println!("{}", "-".repeat(msg.chars().count()));
    println!("{}", msg);
    println!("{}", "-".repeat(msg.chars().count()));
    let mut x = -1;
    for option in options {
      x += 1;
      println!("{}. {}", x, option);
    }
    println!("");
    let selection = console_read("Make a Selection: ");
    match selection.trim().parse::<u32>() {
      Ok(n) => {
        if n < options.len().try_into().unwrap() {
          answer = n;
          break;
        } else {
          println!("");
          println!("ERROR: Invalid selection, please try again.");
          println!("");
        }
      },
      Err(_e) => {
        println!("");
        println!("ERROR: Invalid selection, please try again.");
        println!("");
      }
    }
  }
  answer
}

/// Prompts a user via STDIN to enter their password. The password will not
/// be reflected as they type.
///
/// **Example:**
/// ```no_run
/// let answer = codemelted::console_password(
///   "Whats Your Password",
/// );
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_console.mmd")]
pub fn console_password(message: &str) -> String {
  let msg = match message {
    "" => "PASSWORD: ",
    _ => &format!("{}: ", message),
  };
  let password = rpassword::prompt_password(msg).unwrap();
  String::from(password)
}

/// Prompts a user via STDIN to answer a question.
///
/// **Example:**
/// ```no_run
/// let answer = codemelted::console_prompt(
///   "DC or Marvel",
/// );
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_console.mmd")]
pub fn console_prompt(message: &str) -> String {
  let msg = match message {
    "" => "PROMPT: ",
    _ => &format!("{}: ", message),
  };
  let answer: String = console_read(msg);
  String::from(answer)
}

/// Will put a string to STDOUT without the new line character.
///
/// **Example:**
/// ```no_run
/// codemelted::console_write("Oh Know!");
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_console.mmd")]
pub fn console_write(message: &str) {
  console_write_stdout(message, false);
}

/// Will put a string to STDOUT with a new line character.
///
/// **Example:**
/// ```no_run
/// codemelted::console_writeln("Oh Know!");
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_console.mmd")]
pub fn console_writeln(message: &str) {
  console_write_stdout(message, true);
}

// ============================================================================
// [DB UC IMPLEMENTATION] =====================================================
// ============================================================================

/// Ensures a database exists as any of the [db_query] and [db_update]
/// functions will panic if the specified database does not exist. So this
/// will aid in ensuring expected configurations.
///
/// **Example:**
/// ```no_run
/// let db_file = format!("{}/test.db", codemelted::runtime_temp_path());
/// assert!(!codemelted::db_exists(&db_file, false));
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_db.mmd")]
pub fn db_exists(db_path: &str, should_panic: bool) -> bool {
  match disk_exists(db_path, CDiskType::File) {
    true => true,
    false => {
      if should_panic {
        panic!("SyntaxError: {} database does not exist!", db_path);
      }
      false
    },
  }
}

/// Manages the sqlite3 database on disk by creating the initial database
/// if it does not already exist along with execute Data Definition Language
/// SQL statements to manage the database.
///
/// **Example:**
/// ```no_run
/// let db_file = format!("{}/test.db", codemelted::runtime_temp_path());
/// assert!(!codemelted::db_exists(&db_file, false));
/// codemelted::db_manage(&db_file, true, None);
/// assert!(codemelted::db_exists(&db_file, false));
/// let _ = codemelted::disk_rm(&db_file);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_db.mmd")]
pub fn db_manage(db_path: &str, create_db: bool, sql: Option<&str>) {
  // If not creating the database on connection, check to make sure
  // it is where we expect.
  if !create_db {
    db_exists(db_path, true);
  }

  // Alrighty, time to go carry out the DDL statement and panic if
  // something about it fails.
  match rusqlite::Connection::open(db_path) {
    Ok(conn) => {
      match sql {
        // Welp, we were asked to perform the DDL statement. Go do it
        // and see what happens.
        Some(stmt) => {
          match conn.execute(stmt, ()) {
            Ok(_) => {},
            Err(why) => panic!("SyntaxError: db_manage {}", why),
          }
        },
        // The sql was None so it may have been to just create the
        // database.
        None => {},
      }
    },
    Err(why) => panic!("SyntaxError: db_manage {}", why),
  };
}

/// Will query a table specified by the db_path to retrieve the data
/// as a vector of the given type. This represents the Data Query
/// Language (DQL) SELECT statement.
///
/// **Example:**
/// ```
/// // Create the database file and table.
/// let db_file = format!("{}/test.db", codemelted::runtime_temp_path());
/// assert!(!codemelted::db_exists(&db_file, false));
/// let sql = "CREATE TABLE person (
///   id    INTEGER PRIMARY KEY,
///   name  TEXT NOT NULL,
///   data  BLOB
/// )";
/// codemelted::db_manage(&db_file, true, Some(sql));
/// assert!(codemelted::db_exists(&db_file, false));
///
/// // Go add a person to the database.
/// #[derive(Debug)]
/// struct Person {
///   id: i32,
///   name: String,
///   data: Option<Vec<u8>>,
/// }
///
/// let sql = "INSERT INTO person (name, data) VALUES (?1, ?2)";
/// let me = Person {
///    id: 0,
///    name: "Steven".to_string(),
///    data: None,
/// };
/// let rows = codemelted::db_update(
///   &db_file,
///   sql,
///   (&me.name, &me.data)
/// );
/// assert!(rows == 1);
///
/// // Now lets query the data back
/// let sql = "SELECT id, name, data FROM person";
/// let row_data = codemelted::db_query(
///   &db_file,
///   sql,
///   [],
///   |row| {
///     Ok(Person {
///       id: row.get(0)?,
///       name: row.get(1)?,
///       data: row.get(2)?,
///     })
///   }
/// );
/// assert!(row_data.len() == 1);
/// let _ = codemelted::disk_rm(&db_file);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_db.mmd")]
pub fn db_query<
    P: rusqlite::Params,
    T,
    F: FnMut(&rusqlite::Row<'_>) -> rusqlite::Result<T>>(
      db_path: &str,
      sql: &str,
      params: P,
      f: F
) -> Vec<T> {
  // Ensure database exists and we can connect to it.
  db_exists(db_path, true);
  match rusqlite::Connection::open(db_path) {
    Ok(conn) => {
      // Prepare the SQL statement and binding parameters.
      match conn.prepare(sql) {
        Ok(mut stmt) => {
          // Execute the query based on the params and function binding.
          match stmt.query_map(params, f) {
            Ok(rows) => {
              // Capture the results into from the query to return
              // to the caller.
              let mut results = Vec::<T>::new();
              for row in rows {
                match row {
                  Ok(v) => results.push(v),
                  // Something happened with the bindings
                  Err(why) => panic!(
                    "SyntaxError: db_query {}",
                    why
                  ),
                }
              }
              results
            },
            // Unable to do the query.
            Err(why) => panic!("SyntaxError: db_query {}", why),
          }
        },
        // Unable to prepare the SQL
        Err(why) => panic!("SyntaxError: db_query {}", why),
      }
    },
    // Failed to make the database connection.
    Err(why) => panic!("SyntaxError: db_query {}", why),
  }
}

/// Provides the ability Data Manipulation Language (DML) statements
/// (i.e. INSERT, DELETE, or UPDATE) with the number of rows updated
/// based on the transaction.
///
/// **Example:**
/// ```no_run
/// // Create the database file and table.
/// let db_file = format!("{}/test.db", codemelted::runtime_temp_path());
/// assert!(!codemelted::db_exists(&db_file, false));
/// let sql = "CREATE TABLE person (
///   id    INTEGER PRIMARY KEY,
///   name  TEXT NOT NULL,
///   data  BLOB
/// )";
/// codemelted::db_manage(&db_file, true, Some(sql));
/// assert!(codemelted::db_exists(&db_file, false));
///
/// // Go add a person to the database.
/// #[derive(Debug)]
/// struct Person {
///   id: i32,
///   name: String,
///   data: Option<Vec<u8>>,
/// }
///
/// let sql = "INSERT INTO person (name, data) VALUES (?1, ?2)";
/// let me = Person {
///    id: 0,
///    name: "Steven".to_string(),
///    data: None,
/// };
/// let rows = codemelted::db_update(
///   &db_file,
///   sql,
///   (&me.name, &me.data)
/// );
/// assert!(rows == 1);
///
/// // Now lets query the data back
/// let sql = "SELECT id, name, data FROM person";
/// let row_data = codemelted::db_query(
///   &db_file,
///   sql,
///   [],
///   |row| {
///     Ok(Person {
///       id: row.get(0)?,
///       name: row.get(1)?,
///       data: row.get(2)?,
///     })
///   }
/// );
/// assert!(row_data.len() == 1);
/// let _ = codemelted::disk_rm(&db_file);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_db.mmd")]
pub fn db_update<P: rusqlite::Params>(
  db_path: &str,
  sql: &str,
  params: P
) -> usize {
  // Ensure database exists and connect to it.
  db_exists(db_path, true);
  match rusqlite::Connection::open(db_path) {
    Ok(conn) => {
      // Prepare the SQL statement for execution.
      match conn.prepare(sql) {
        Ok(mut stmt) => {
          // Execute the prepared SQL statement and return the #
          // of rows updated
          match stmt.execute(params) {
            Ok(v) => v,
            // Failed binding of parameters to prepared statement
            Err(why) => panic!("SyntaxError: db_update {}", why),
          }
        },
        // Failed to prepare the SQL statement
        Err(why) => panic!("SyntaxError: db_update {}", why),
      }
    },
    // Failed to Connect
    Err(why) => panic!("SyntaxError: db_update {}", why),
  }
}

/// Determines the setup version of sqlite supported by the module
///
/// **Example:**
/// ```
/// assert!(codemelted::db_version().len() > 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_db.mmd")]
pub fn db_version() -> String {
  rusqlite::version().to_string()
}

// ============================================================================
// [DISK UC IMPLEMENTATION] ===================================================
// ============================================================================

/// Identifies the type of src on the disk when attempting to see if it
/// [disk_exists] or not.
pub enum CDiskType {
  /// Does not matter the type, just does it exist or not.
  Either,
  /// Only true if it is a directory.
  Directory,
  /// Only true if it is a file.
  File,
}

/// Supports the [disk_read_file] and [disk_write_file] functions for
/// reading / writing file contents from the host operating system disk.
pub enum CFileContents {
  /// Read / write files as Bytes.
  Bytes(Vec<u8>),
  /// Read / write files as String.
  String(String),
}
impl CFileContents {
  /// Utility method to retrieve the [CFileContents::Bytes] data.
  pub fn as_bytes(&self) -> Option<Vec<u8>> {
    match self {
      CFileContents::Bytes(items) => Some(items.to_owned()),
      CFileContents::String(_) => None,
    }
  }
  /// Utility method to retrieve the [CFileContents::String] data.
  pub fn as_string(&self) -> Option<String> {
    match self {
      CFileContents::Bytes(_) => None,
      CFileContents::String(v) => Some(v.to_owned()),
    }
  }
}

/// Will copy a file / directory from one location on the host operating
/// system disk to the other.
///
/// **Example:**
/// ```
/// use codemelted::CFileContents;
///
/// let temp_filename = format!(
///   "{}/test.txt",
///   codemelted::runtime_temp_path()
/// );
/// let home_filename = format!(
///   "{}/test.txt",
///   codemelted::runtime_home_path()
/// );
///
/// let _ = codemelted::disk_write_file(
///   &home_filename,
///   CFileContents::String("Hello".to_string()),
///   false
/// );
/// let result = codemelted::disk_cp(&home_filename, &temp_filename);
/// assert!(result.is_ok());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_disk.mmd")]
pub fn disk_cp(src: &str, dest: &str) -> Result<(), std::io::Error> {
  let result = std::fs::copy(src, dest);
  match result {
    Ok(_) => Ok(()),
    Err(why) => Err(why),
  }
}

/// Determines if a directory or file exists on the host operating system
/// and will further determine if it is of the expected [CDiskType].
///
/// **Example:**
/// ```
/// use codemelted::CDiskType;
///
/// let path = codemelted::runtime_home_path();
/// assert_eq!(codemelted::disk_exists(&path, CDiskType::Either), true);
/// assert_eq!(codemelted::disk_exists(&path, CDiskType::Directory), true);
/// assert_eq!(codemelted::disk_exists(&path, CDiskType::File), false);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_disk.mmd")]
pub fn disk_exists(src: &str, disk_type: CDiskType) -> bool {
  match disk_type {
    CDiskType::Either => std::path::Path::new(src).is_dir()
      || std::path::Path::new(src).is_file(),
    CDiskType::Directory => std::path::Path::new(src).is_dir(),
    CDiskType::File => std::path::Path::new(src).is_file(),
  }
}

/// Will list the files / directories in a given location on the host
/// operating system.
///
/// **Example:**
/// ```
/// let temp_path = codemelted::runtime_temp_path();
/// let result = codemelted::disk_ls(&temp_path);
/// assert!(result.is_ok());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_disk.mmd")]
pub fn disk_ls(src: &str) -> Result<std::fs::ReadDir, std::io::Error> {
  std::fs::read_dir(src)
}

/// Retrieves metadata about the specified directory or stored on the
/// host operating system.
///
/// **Example:**
/// ```
/// let path = codemelted::runtime_home_path();
/// let answer = codemelted::disk_metadata(&path);
/// assert!(answer.is_ok());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_disk.mmd")]
pub fn disk_metadata(
  src: &str
) -> Result<std::fs::Metadata, std::io::Error> {
  std::path::Path::new(src).metadata()
}

/// Will create a directory and sub-directories in a given location on the
/// host operating system.
///
/// **Example:**
/// ```
/// let temp_path = codemelted::runtime_temp_path();
/// let new_path = format!("{}/1/2/ready/go", temp_path);
/// let result = codemelted::disk_mkdir(&new_path);
/// assert!(result.is_ok());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_disk.mmd")]
pub fn disk_mkdir(src: &str) -> Result<(), std::io::Error> {
  std::fs::create_dir_all(src)
}

/// Will move a file / directory from one location on the host operating
/// system disk to the other.
///
/// **Example:**
/// ```
/// use codemelted::CFileContents;
///
/// let temp_filename = format!(
///   "{}/test.txt",
///   codemelted::runtime_temp_path()
/// );
/// let new_filename = format!(
///   "{}/test_new.txt",
///   codemelted::runtime_temp_path()
/// );
///
/// let _ = codemelted::disk_write_file(
///   &temp_filename,
///   CFileContents::String("Hello".to_string()),
///   false
/// );
/// let result = codemelted::disk_mv(&temp_filename, &new_filename);
/// assert!(result.is_ok());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_disk.mmd")]
pub fn disk_mv(src: &str, dest: &str) -> Result<(), std::io::Error> {
  let result = std::fs::rename(src, dest);
  match result {
    Ok(_) => Ok(()),
    Err(why) => Err(why),
  }
}

/// Reads a binary file from the host operating system.
///
/// **Example:**
/// ```
/// use codemelted::CFileContents;
///
/// let data = Vec::<u8>::from([0x01, 0x02]);
/// let filename = format!("{}/test.bin", codemelted::runtime_temp_path());
/// let result = codemelted::disk_write_file(
///   &filename,
///   CFileContents::Bytes(data.to_owned()),
///   false
/// );
/// assert!(result.is_ok());
/// let data = codemelted::disk_read_file(
///   &filename,
///   false,
/// );
/// assert!(data.is_ok());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_disk.mmd")]
pub fn disk_read_file(
  filename: &str,
  is_string: bool,
) -> Result<CFileContents, std::io::Error> {
  let file = std::fs::File::open(filename);
  match file {
    Ok(_) => {
      let mut data = Vec::new();
      let result = std::io::Read::read_to_end(&mut file?, &mut data);
      match result {
        Ok(_) => {
          if !is_string {
            Ok(CFileContents::Bytes(data.to_owned()))
          } else {
            match String::from_utf8(data) {
              Ok(v) => Ok(CFileContents::String(v)),
              Err(why) => Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                why.to_string()
              )),
            }
          }
        },
        Err(why) => Err(why),
      }
    },
    Err(why) => Err(why),
  }
}

/// Will remove a file / directory from one location on the host operating
/// system disk to the other.
///
/// **Example:**
/// ```
/// use codemelted::CFileContents;
///
/// let temp_filename = format!(
///   "{}/test.txt",
///   codemelted::runtime_temp_path()
/// );
///
/// let _ = codemelted::disk_write_file(
///   &temp_filename,
///   CFileContents::String("Hello".to_string()),
///   false
/// );
/// let result = codemelted::disk_rm(&temp_filename);
/// assert!(result.is_ok());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_db.mmd")]
pub fn disk_rm(src: &str) -> Result<(), std::io::Error> {
  let is_file = disk_exists(src, CDiskType::File);
  if is_file {
    let result = std::fs::remove_file(src);
    match result {
      Ok(_) => return Ok(()),
      Err(why) => return Err(why),
    }
  } else {
    let result = std::fs::remove_dir_all(src,);
    match result {
      Ok(_) => return Ok(()),
      Err(why) => return Err(why),
    }
  }
}

/// Writes a binary file to the host operating system.
///
/// **Example:**
/// ```
/// use codemelted::CFileContents;
///
/// let data = Vec::<u8>::from([0x01, 0x02]);
/// let filename = format!("{}/test.bin", codemelted::runtime_temp_path());
/// let result = codemelted::disk_write_file(
///   &filename,
///   CFileContents::Bytes(data.to_owned()),
///   false
/// );
/// assert!(result.is_ok());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_disk.mmd")]
pub fn disk_write_file(
  filename: &str,
  data: CFileContents,
  append: bool,
) -> Result<(), std::io::Error> {
  let mut file = if append {
    std::fs::OpenOptions::new()
      .append(true)
      .create(true)
      .open(filename)?
  } else {
    std::fs::OpenOptions::new()
      .write(true)
      .create(true)
      .open(filename)?
  };
  match data {
    CFileContents::Bytes(data) => {
      let _ = std::io::Write::write_all(&mut file, &data)?;
    },
    CFileContents::String(data) => {
      let _ = std::io::Write::write_all(&mut file, data.as_bytes())?;
    },
  }
  Ok(())
}

// ============================================================================
// [HW UC IMPLEMENTATION] =====================================================
// ============================================================================

/// Result of the [hw_available_bluetooth_devices] call identified devices one
/// can open to exchange data via the [hw_open_bluetooth_device] call.
pub struct CBluetoothInfo {
  peripheral: btleplug::platform::Peripheral,
}
impl CBluetoothInfo {
  /// Helper function to facilitate creating this object.
  fn new(peripheral: btleplug::platform::Peripheral) -> CBluetoothInfo {
    CBluetoothInfo { peripheral }
  }

  /// Identifies the MAC address of the device.
  pub fn address(&self) -> String {
    btleplug::api::Peripheral::address(&self.peripheral).to_string()
  }

  /// Identifies the id of the device.
  pub fn id(&self) -> String {
    btleplug::api::Peripheral::id(&self.peripheral).to_string()
  }
}

/// Provides the [CSerialPortProtocol] ability to both
/// [CSerialPortProtocol::post_message] utilizing the [CSerialPortData]
/// as the data wrapper and as the [CSerialPortProtocol::get_message]
/// request via the [CSerialPortData::get_message_request] function to
/// query the port for data.
#[derive(Debug)]
pub enum CSerialPortData {
  /// Configurable [CSerialPortProtocol] item.
  BaudRate(Option<u32>),
  /// Configurable [CSerialPortProtocol] item.
  DataBits(Option<serialport::DataBits>),
  /// Configurable [CSerialPortProtocol] item.
  FlowControl(Option<serialport::FlowControl>),
  /// Configurable [CSerialPortProtocol] item.
  Parity(Option<serialport::Parity>),
  /// Configurable [CSerialPortProtocol] item.
  StopBits(Option<serialport::StopBits>),
  /// Configurable [CSerialPortProtocol] item.
  Timeout(Option<std::time::Duration>),
  /// Line control status of the [CSerialPortProtocol].
  Break(Option<bool>),
  /// Line control status of the [CSerialPortProtocol].
  ClearBuffer(Option<serialport::ClearBuffer>),
  /// Line control status of the [CSerialPortProtocol].
  CarrierDetect(Option<bool>),
  /// Line control status of the [CSerialPortProtocol].
  ClearToSend(Option<bool>),
  /// Line control status of the [CSerialPortProtocol].
  DataSetReady(Option<bool>),
  /// Line control status of the [CSerialPortProtocol].
  DataTerminalReady(Option<bool>),
  /// Line control status of the [CSerialPortProtocol].
  RequestToSend(Option<bool>),
  /// Line control status of the [CSerialPortProtocol].
  RingIndicator(Option<bool>),
  /// Reads a buffer from a [CSerialPortProtocol].
  DataBytes(Option<Vec<u8>>),
}
/// Specialization of the [CSerialPortData] to support the two way nature
/// of this enumeration as both a request / data write object.
impl CSerialPortData {
  /// Extracts the held Option bool values by those enum types.
  /// Returns None otherwise.
  pub fn as_bool(&self) -> Option<bool> {
    match self {
      CSerialPortData::Break(v) => *v,
      CSerialPortData::CarrierDetect(v) => *v,
      CSerialPortData::ClearToSend(v) => *v,
      CSerialPortData::DataSetReady(v) => *v,
      CSerialPortData::DataTerminalReady(v) => *v,
      CSerialPortData::RequestToSend(v) => *v,
      CSerialPortData::RingIndicator(v) => *v,
      _ => None
    }
  }

  /// Extracts the held Option Vec u8 values by those enum types.
  /// Returns None otherwise.
  pub fn as_bytes(&self) -> Option<Vec<u8>> {
    match self {
      CSerialPortData::DataBytes(items) => items.clone(),
      _ => None,
    }
  }

  /// Extracts the held Option DataBits values by those enum types.
  /// Returns None otherwise.
  pub fn as_data_bits(&self) -> Option<serialport::DataBits> {
    match self {
      CSerialPortData::DataBits(data_bits) => *data_bits,
      _ => None,
    }
  }

  /// Extracts the held Option FlowControl values by those enum types.
  /// Returns None otherwise.
  pub fn as_flow_control(&self) -> Option<serialport::FlowControl> {
    match self {
      CSerialPortData::FlowControl(flow_control) => *flow_control,
      _ => None,
    }
  }

  /// Extracts the held Option Parity values by those enum types.
  /// Returns None otherwise.
  pub fn as_parity(&self) -> Option<serialport::Parity> {
    match self {
      CSerialPortData::Parity(parity) => *parity,
      _ => None,
    }
  }

  /// Extracts the held Option StopBits values by those enum types.
  /// Returns None otherwise.
  pub fn as_stop_bits(&self) -> Option<serialport::StopBits> {
    match self {
      CSerialPortData::StopBits(stop_bits) => *stop_bits,
      _ => None,
    }
  }

  /// Extracts the held Option Duration values by those enum types.
  /// Returns None otherwise.
  pub fn as_timeout(&self) -> Option<std::time::Duration> {
    match self {
      CSerialPortData::Timeout(v) => *v,
      _ => None,
    }
  }

  /// Extracts the held Option u8 by those enum types.
  /// Returns None otherwise.
  pub fn as_u32(&self) -> Option<u32> {
    match self {
      CSerialPortData::BaudRate(v) => *v,
      _ => None,
    }
  }

  /// Provides the string representation to support the
  /// [CSerialPortProtocol::get_message] request string to get specific
  /// types of data from an open port.
  pub fn get_message_request(&self) -> Option<&str> {
    match self {
      CSerialPortData::BaudRate(_) => Some("baud_rate"),
      CSerialPortData::DataBits(_) => Some("data_bits"),
      CSerialPortData::FlowControl(_) => Some("flow_control"),
      CSerialPortData::Parity(_) => Some("parity"),
      CSerialPortData::StopBits(_) => Some("stop_bits"),
      CSerialPortData::Timeout(_) => Some("duration"),
      CSerialPortData::CarrierDetect(_) => Some("carrier_detect"),
      CSerialPortData::ClearToSend(_) => Some("clear_to_send"),
      CSerialPortData::DataSetReady(_) => Some("data_set_ready"),
      CSerialPortData::RingIndicator(_) => Some("ring_indicator"),
      CSerialPortData::DataBytes(_) => Some("data_bytes"),
      _ => None,
    }
  }
}

/// Created via the [hw_open_serial_port] function of the module. This
/// implements [CProtocolHandler] to support a bi-directional communication
/// with an attached device via a serial port.
pub struct CSerialPortProtocol {
  /// Holds the wrapped [SerialPort] of the module until closed.
  port: Option<Box<dyn serialport::SerialPort>>,
}

/// Implements a series of helper functions for the [CSerialPortProtocol].
impl CSerialPortProtocol {
  /// Opens a [CSerialPortProtocol] with a default baud rate of 9600. A failure
  /// will result in a panic.
  fn new(port_info: &serialport::SerialPortInfo) -> CSerialPortProtocol {
    let port = serialport::new(
      port_info.port_name.to_string(),
      9600
    ).open().expect("CSerialPortProtocol: Failed to open!");
    CSerialPortProtocol { port: Some(port) }
  }

  /// Helper function to unwrap the port for access of the
  /// [CProtocolHandler] implemented functions. Will panic once
  /// the protocol is terminated.
  fn port_ref(&mut self) -> &mut Box<dyn serialport::SerialPort + 'static> {
    self.port.as_mut().unwrap()
  }
}
/// The [CSerialPortProtocol] implementation of the [CProtocolHandler]
/// utilizing the [CSerialPortData] enumeration as the bi-directional
/// read / write method of the protocol definition rules.
impl CProtocolHandler<CSerialPortData> for CSerialPortProtocol {
  fn id(&mut self) -> String {
    match self.port_ref().name() {
      Some(v) => v,
      None => String::from(""),
    }
  }

  /// Will query the [CSerialPortProtocol] for the latest status / data
  /// associated/ via the request. Utilize the
  /// [CSerialPortData::get_message_request] to utilize the proper string.
  ///
  /// Will panic if an invalid request is received.
  fn get_message(
    &mut self,
    request: Option<&str>
  ) -> Result<CSerialPortData, std::io::Error> {
    if request.unwrap() == "baud_rate" {
      match self.port_ref().baud_rate() {
        Ok(v) => Ok(CSerialPortData::BaudRate(Some(v))),
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    } else if request.unwrap() == "data_bits" {
      match self.port_ref().baud_rate() {
        Ok(v) => Ok(CSerialPortData::BaudRate(Some(v))),
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    } else if request.unwrap() == "flow_control" {
      match self.port_ref().flow_control() {
        Ok(v) => Ok(CSerialPortData::FlowControl(Some(v))),
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    } else if request.unwrap() == "parity" {
      match self.port_ref().parity() {
        Ok(v) => Ok(CSerialPortData::Parity(Some(v))),
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    } else if request.unwrap() == "stop_bits" {
      match self.port_ref().stop_bits() {
        Ok(v) => Ok(CSerialPortData::StopBits(Some(v))),
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    } else if request.unwrap() == "timeout" {
      Ok(CSerialPortData::Timeout(Some(self.port_ref().timeout())))
    } else if request.unwrap() == "carrier_detect" {
      match self.port_ref().read_carrier_detect() {
        Ok(v) => Ok(CSerialPortData::CarrierDetect(Some(v))),
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    } else if request.unwrap() == "clear_to_send" {
      match self.port_ref().read_clear_to_send() {
        Ok(v) => Ok(CSerialPortData::ClearToSend(Some(v))),
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    } else if request.unwrap() == "data_set_ready" {
      match self.port_ref().read_data_set_ready() {
        Ok(v) => Ok(CSerialPortData::DataSetReady(Some(v))),
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    } else if request.unwrap() == "ring_indicator" {
      match self.port_ref().read_ring_indicator() {
        Ok(v) => Ok(CSerialPortData::RingIndicator(Some(v))),
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    } else if request.unwrap() == "data_bytes" {
      match self.port_ref().bytes_to_read() {
        Ok(buffer_size) => {
          let mut buffer = vec![0; buffer_size as usize];
          match self.port_ref().read_exact(&mut buffer) {
            Ok(_) => Ok(CSerialPortData::DataBytes(Some(buffer))),
            Err(why) => Err(std::io::Error::new(
              std::io::ErrorKind::BrokenPipe,
              why.to_string()
            )),
          }
        },
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    } else {
      panic!(
        "CSerialPortProtocol::get_message() received an invalid request!"
      );
    }
  }

  /// Signals the [CSerialPortProtocol::terminate] has not been called.
  fn is_running(&self) -> bool {
    self.port.is_some()
  }

  /// Will write the [CSerialPortData] to the open serial port. The result
  /// will reflect any errors if the transmit fails. Will panic if a
  /// [CSerialPortData] not writable is specified or the port has been
  /// terminated.
  fn post_message(
    &mut self,
    data: CSerialPortData
  ) -> Result<(), std::io::Error> {
    // Go carry out the post_message request.
    let result = match data {
      CSerialPortData::BaudRate(v) => {
        self.port_ref().set_baud_rate(v.unwrap())
      },
      CSerialPortData::DataBits(data_bits) => {
        self.port_ref().set_data_bits(data_bits.unwrap())
      },
      CSerialPortData::FlowControl(flow_control) => {
        self.port_ref().set_flow_control(flow_control.unwrap())
      },
      CSerialPortData::Parity(parity) => {
        self.port_ref().set_parity(parity.unwrap())
      },
      CSerialPortData::StopBits(stop_bits) => {
        self.port_ref().set_stop_bits(stop_bits.unwrap())
      },
      CSerialPortData::Timeout(duration) => {
        self.port_ref().set_timeout(duration.unwrap())
      },
      CSerialPortData::DataTerminalReady(v) => {
        self.port_ref().write_data_terminal_ready(v.unwrap())
      },
      CSerialPortData::RequestToSend(v) => {
        self.port_ref().write_request_to_send(v.unwrap())
      },
      CSerialPortData::DataBytes(data) => {
        match self.port_ref().write_all(&data.unwrap()) {
          Ok(_) => {
            match self.port_ref().flush() {
              Ok(_) => Ok(()),
              Err(why) => {
                Err(serialport::Error::new(
                  serialport::ErrorKind::Io(why.kind()),
                  why.to_string())
                )
              }
            }
          },
          Err(why) => {
            Err(serialport::Error::new(
              serialport::ErrorKind::Io(why.kind()),
              why.to_string())
            )
          }
        }
      },
      CSerialPortData::Break(v) =>{
        if v.unwrap() {
          self.port_ref().set_break()
        } else {
          self.port_ref().clear_break()
        }
      },
      CSerialPortData::ClearBuffer(v) => {
        self.port_ref().clear(v.unwrap())
      },
      // All other enumerations do no support post_message
      _ => {
        panic!(
          "CSerialPortProtocol::post_message(): received invalid data!"
        );
      }
    };

    match result {
      Ok(_) => Ok(()),
      Err(why) => Err(std::io::Error::new(
        std::io::ErrorKind::BrokenPipe,
        why.to_string()
      ))
    }
  }

  /// Closes the port connection. All transaction with the
  /// [CSerialPortProtocol] object will panic after this is performed.
  fn terminate(&mut self) {
    self.port = None;
  }
}

/// Scans for available bluetooth devices in the area to allow for later
/// connection via the [hw_open_bluetooth_device] function.
///
/// **Example:**
/// ```no_run
/// println!("Now scanning for available bluetooth devices...");
/// match codemelted::hw_available_bluetooth_devices(5000) {
///   Ok(devices) => {
///     println!("Found {} devices...", devices.len());
///     for device in devices {
///       println!("{} / {}", device.id(), device.address());
///     }
///   },
///   Err(why) => println!("Error Detected = {}", why),
/// }
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_hw.mmd")]
pub fn hw_available_bluetooth_devices(scan_time_milliseconds: u64)
    -> Result<Vec<CBluetoothInfo>, btleplug::Error> {
  // Create the runtime
  let rt = tokio::runtime::Runtime::new().unwrap();
  let devices: Result<Vec<CBluetoothInfo>, btleplug::Error> =
      rt.block_on(async {
    // Grab the operating system adapter to sus out bluetooth devices.
    let manager = btleplug::platform::Manager::new().await?;
    let adapters = btleplug::api::Manager::adapters(&manager).await?;
    let mut devices = Vec::<CBluetoothInfo>::new();
    let central = adapters.into_iter().nth(0);
    if central.is_none() {
      return Ok(devices);
    }

    // Start scan for the devices and add them to our list.
    let adapter = central.unwrap();
    btleplug::api::Central::start_scan(
      &adapter,
      btleplug::api::ScanFilter::default()
    ).await?;

    // Give some time for the scan to find devices.
    async_sleep(scan_time_milliseconds);
    let peripherals = btleplug::api::Central::peripherals(&adapter).await?;
    for p in peripherals {
      devices.push(CBluetoothInfo::new(p));
    }
    btleplug::api::Central::stop_scan(&adapter).await?;
    Ok(devices)
  });
  devices
}

/// Scans for available serial ports returning a Vector of SerialPortInfo
/// objects that can be utilized with [hw_open_serial_port] function for a
/// selected port.
///
/// **Example:**
/// ```no_run
/// match codemelted::hw_available_serial_ports() {
///   Ok(v) => {
///     println!("Available Ports = {}", v.len());
///     for el in v {
///       println!("{} / {:?}", el.port_name, el.port_type);
///     }
///   },
///   Err(why) => println!("{:?}", why),
/// }
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_hw.mmd")]
pub fn hw_available_serial_ports() -> Result<
    Vec<serialport::SerialPortInfo>, serialport::Error> {
  serialport::available_ports()
}

/// <center><b><mark>FUTURE IMPLEMENTATION. DON'T USE</mark></b></center>
pub fn hw_open_bluetooth_device() {
  unimplemented!("FUTURE IMPLEMENTATION!");
}

/// Takes a SerialPortInfo object to create a [CSerialPortProtocol] to
/// interact with a device attached to the host operating system.
///
/// **Example:**
/// ```no_run
/// use codemelted::CProtocolHandler;
/// use codemelted::CSerialPortData;
///
/// // Open and configure the port
/// let port_info = &codemelted::hw_available_serial_ports().unwrap()[0];
/// let mut port = codemelted::hw_open_serial_port(port_info);
/// port.post_message(CSerialPortData::BaudRate(Some(115200)));
/// port.post_message(CSerialPortData::DataTerminalReady(Some(true)));
/// port.post_message(CSerialPortData::RequestToSend(Some(true)));
/// port.post_message(CSerialPortData::DataBits(
///   Some(serialport::DataBits::Eight)));
/// port.post_message(CSerialPortData::StopBits(
///   Some(serialport::StopBits::One)));
/// port.post_message(CSerialPortData::Parity(
///   Some(serialport::Parity::None)));
///
/// // Get the name of the port and process the data.
/// let name = port.id();
/// println!("{} is {}", name, port.is_running());
///
/// // Read and print buffer until <ctrl>+c is hit.
/// loop {
///   let buffer = port.get_message(
///     CSerialPortData::DataBytes(None).get_message_request()
///   ).unwrap().as_bytes().unwrap();
///   println!("buffer len = {}", buffer.len());
///   let data = String::from_utf8(buffer);
///   if data.is_ok() {
///     println!("{}", data.unwrap());
///   }
///   codemelted::async_sleep(1000);
/// }
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_hw.mmd")]
pub fn hw_open_serial_port(
  port_info: &serialport::SerialPortInfo
) -> CSerialPortProtocol {
  CSerialPortProtocol::new(port_info)
}

// ============================================================================
// [JSON UC IMPLEMENTATION] ===================================================
// ============================================================================

/// Enumeration to support the [json_check_type] function for checking if a
/// [CObject] holds the specified data type.
pub enum CDataType {
  /// Check if the data type is an array.
  Array,
  /// Check if the data type is a bool.
  Boolean,
  /// Check if the data type is not set to anything.
  Empty,
  /// Check if the data type is a null type.
  Null,
  /// Check if the data type is a Rust number (i.e. u / f types)
  Number,
  /// Check if the data type is another [CObject] type.
  Object,
  /// Check if the data type is a String type.
  String,
}

/// Defines a trait to attach to the [CObject] providing utility function
/// definitions to make a bool based on a series of strings that can be
/// considered true in nature.
pub trait CTruthyString {
  /// Provides a binding to attach the ability for a [CObject] to determine
  /// if its held str value is truthy or not.
  fn as_truthy(&self) -> bool;
  /// Static function that implements the truthy string logic.
  fn is_truthy(data: &str) -> bool;
}

/// The binding that provides the codemelted module's "dynamic" data allowing
/// for full support of JSON along with holding general Rust data types that
/// can be returned from different use case functions.
pub type CObject = json::JsonValue;

/// Implements the [CTruthyString] trait for our [CObject] dynamic type.
impl CTruthyString for CObject {
  fn as_truthy(&self) -> bool {
    CObject::is_truthy(self.as_str().unwrap())
  }

  fn is_truthy(data: &str) -> bool {
    let true_strings: [&'static str; 9] = [
      "true",
      "1",
      "t",
      "y",
      "yes",
      "yeah",
      "yup",
      "certainly",
      "uh-huh",
    ];
    let data_check = String::from(data.to_lowercase());
    for el in true_strings {
      if el.contains(&data_check) {
        return true;
      }
    }
    false
  }
}

/// Will convert the given [CObject] to its equivalent bool value. This will
/// also be false if not a valid value.
///
/// **Example (Successful Conversion):**
/// ```
/// use codemelted::CObject;
///
/// let obj = CObject::Boolean(true);
/// assert_eq!(codemelted::json_as_bool(&obj), true);
/// ```
///
/// **Example (Convert false with actual data or invalid):**
/// ```
/// use codemelted::CObject;
///
/// let obj1 = CObject::Boolean(false);
/// assert_eq!(codemelted::json_as_bool(&obj1), false);
///
/// let obj2 = CObject::String("Oh Know!".to_string());
/// assert_eq!(codemelted::json_as_bool(&obj2), false);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_json.mmd")]
pub fn json_as_bool(data: &CObject) -> bool {
  let answer = data.as_bool();
  match answer {
    Some(v) => v,
    _ => false,
  }
}

/// Will convert the given [CObject] to its equivalent number value. This
/// will be set to None if the conversion fails.
///
/// **Example (Successful Conversion):**
/// ```
/// use codemelted::CObject;
///
/// let obj = CObject::from(542.65);
/// assert_eq!(codemelted::json_as_number(&obj).is_some(), true);
/// ```
///
/// **Example (Failed Conversion):**
/// ```
/// use codemelted::CObject;
///
/// let obj = CObject::from("Hello Boss");
/// assert_eq!(codemelted::json_as_number(&obj).is_some(), false);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_json.mmd")]
pub fn json_as_number(data: &CObject) -> Option<json::number::Number>{
  data.as_number()
}

/// Will convert the given [CObject] to its equivalent string value. This
/// will be None if the conversion fails.
///
/// **Example (Successful Conversion):**
/// ```
/// use codemelted::CObject;
///
/// let obj = CObject::from("Hello");
/// assert_eq!(codemelted::json_as_string(&obj).is_some(), true);
/// ```
///
/// **Example (Failed Conversion):**
/// ```
/// use codemelted::CObject;
///
/// let obj = CObject::Null;
/// assert_eq!(codemelted::json_as_string(&obj).is_some(), false);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_json.mmd")]
pub fn json_as_string(data: &CObject) -> Option<String> {
  let answer = data.as_str();
  match answer {
    Some(v) => Some(String::from(v)),
    _ => None,
  }
}

/// Will determine if the specified [CObject] is of the specified
/// [CDataType] enumeration.
///
/// **Example:**
/// ```
/// use codemelted::CObject;
/// use codemelted::CDataType;
///
/// let obj = codemelted::json_create_object();
/// assert_eq!(
///   codemelted::json_check_type(CDataType::Object, &obj, false),
///   true
/// );
/// assert_eq!(
///   codemelted::json_check_type(CDataType::Number, &obj, false),
///   false
/// );
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_json.mmd")]
pub fn json_check_type(
  data_type: CDataType,
  data: &CObject,
  should_panic: bool,
) -> bool {
  let answer = match data_type {
    CDataType::Array => data.is_array(),
    CDataType::Boolean => data.is_boolean(),
    CDataType::Empty => data.is_empty(),
    CDataType::Null => data.is_null(),
    CDataType::Number => data.is_number(),
    CDataType::Object => data.is_object(),
    CDataType::String => data.is_string(),
  };

  if should_panic && !answer {
    panic!("ERROR: json_check_type() failed.");
  }
  answer
}

/// Creates a JSON compliant [CObject] for working with array JSON data.
///
/// **Example:**
/// ```
/// use codemelted::CObject;
/// use codemelted::CDataType;
///
/// let obj = codemelted::json_create_array();
/// assert_eq!(
///   codemelted::json_check_type(CDataType::Array, &obj, false),
///   true
/// );
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_json.mmd")]
pub fn json_create_array() -> CObject {
  CObject::new_array()
}

/// Creates a JSON compliant [CObject] for working with object JSON data.
///
/// **Example:**
/// ```
/// use codemelted::CObject;
/// use codemelted::CDataType;
///
/// let obj = codemelted::json_create_object();
/// assert_eq!(
///   codemelted::json_check_type(CDataType::Object, &obj, false),
///   true
/// );
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_json.mmd")]
pub fn json_create_object() -> CObject {
  CObject::new_object()
}

/// Will check if a [CObject] object data JSON type has the specified key
/// property before working with it.
///
/// **Example:**
/// ```
/// use codemelted::CObject;
///
/// let mut obj = codemelted::json_create_object();
/// let _ = obj.insert("field1", 42);
/// assert_eq!(codemelted::json_has_key(&obj, "field1", false), true);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_json.mmd")]
pub fn json_has_key(
  data: &CObject,
  key: &str,
  should_panic: bool,
) -> bool {
  let answer = data.has_key(key);
  if should_panic && !answer {
    panic!("ERROR: json_has_key() failed.");
  }
  answer
}

/// Takes a JSON serialized string and parses it to create a [CObject].
/// Returns None if the parse fails.
///
/// **Example (Successful Parse):**
/// ```
/// use codemelted::CObject;
///
/// let mut obj1 = codemelted::json_create_object();
/// let _ = obj1.insert("field1", 42);
/// let stringified_data = codemelted::json_stringify(obj1.clone());
/// let obj2 = codemelted::json_parse(&stringified_data).unwrap();
/// assert_eq!(obj1, obj2);
/// ```
///
/// **Example (Failed Parse):**
/// ```
/// use codemelted::CObject;
///
/// let obj = codemelted::json_parse("{,},");
/// assert_eq!(obj.is_some(), false);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_json.mmd")]
pub fn json_parse(data: &str) -> Option<CObject> {
  let answer = json::parse(&data);
  match answer {
    Ok(v) => Some(v),
    _ => None,
  }
}

/// Takes a [CObject] and converts it the serialized JSON string. See
/// [json_parse] for examples.
#[doc = simple_mermaid::mermaid!("models/codemelted_json.mmd")]
pub fn json_stringify(data: CObject) -> String {
  json::stringify(data)
}

/// Validates if a specified data str represents a valid URL. True if it is
/// and false if not.
///
/// **Example:**
/// ```
/// use codemelted::CObject;
///
/// assert_eq!(
///   codemelted::json_valid_url("https://google.com", false),
///   true
/// );
/// assert_eq!(codemelted::json_valid_url("{230924!!}}|}", false), false);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_json.mmd")]
pub fn json_valid_url(
  data: &str,
  should_panic: bool,
) -> bool {
  let answer = !(url::Url::parse(data).is_err());
  if should_panic && !answer {
    panic!("ERROR: json_valid_url() failed.");
  }
  answer
}

// ============================================================================
// [LOGGER UC IMPLEMENTATION] =================================================
// ============================================================================

/// Holds the log level for the logger module.
static LOG_LEVEL: std::sync::Mutex<CLogLevel> = std::sync::Mutex::new(
  CLogLevel::Warning
);

/// Holds the log handler reference for post log processing.
static LOG_HANDLER: std::sync::Mutex<Option<CLoggedEventHandler>> =
  std::sync::Mutex::new(None);

/// Represents the log levels for the logging module.
#[derive(Clone, PartialEq, Debug)]
pub enum CLogLevel {
  /// Ideal for debugging the running application.
  Debug,
  /// Something good has happened in your app.
  Info,
  /// Something recoverable happened within the app.
  Warning,
  /// Something really bad happened within the app.
  Error,
  /// We don't care what happens within the app.
  Off,
}
impl CLogLevel {
  /// Retrieves the string representation of the logging level.
  pub fn as_string(&self) -> String {
    match self {
      CLogLevel::Debug => String::from("DEBUG"),
      CLogLevel::Info => String::from("INFO"),
      CLogLevel::Warning => String::from("WARNING"),
      CLogLevel::Error => String::from("ERROR"),
      CLogLevel::Off => String::from("OFF"),
    }
  }

  /// Retrieves the int representation of the logging level.
  pub fn as_int(&self) -> u8 {
    match self {
      CLogLevel::Debug => 0,
      CLogLevel::Info => 1,
      CLogLevel::Warning => 2,
      CLogLevel::Error => 3,
      CLogLevel::Off => 4,
    }
  }
}

/// The record created to represent the logged event.
pub struct CLogRecord {
  time_stamp: chrono::DateTime<chrono::Utc>,
  log_level: CLogLevel,
  data: String,
}
/// The attached support functions for the [CLogRecord] struct.
impl CLogRecord {
  /// Creates the [CLogRecord] when a [log] event occurs with the module.
  fn new(log_level: CLogLevel, data: &str) -> CLogRecord {
    CLogRecord {
      time_stamp: chrono::Utc::now(),
      log_level,
      data: String::from(data),
    }
  }

  /// Retrieves when the logged event was handled.
  pub fn get_time_stamp(&self) -> &chrono::DateTime<chrono::Utc> {
    &self.time_stamp
  }

  /// Gets the log level of the event.
  pub fn get_log_level(&self) -> &CLogLevel {
    &self.log_level
  }

  /// Gets the data associated with the event.
  pub fn get_data(&self) -> &str {
    &self.data
  }

  /// Translates a string representation of the [CLogRecord] struct.
  pub fn as_string(&self) -> String {
    format!(
      "{} [{}]: {}",
      self.get_time_stamp().format("%Y-%b-%d %H:%M:%S.%3f"),
      self.get_log_level().as_string(),
      self.get_data()
    )
  }
}

/// Function type definition for post processing logged events.
pub type CLoggedEventHandler = fn(CLogRecord);

/// Gets the [CLogLevel] for the logging module.
///
/// **Example:**
/// ```
/// use codemelted::CLogLevel;
///
/// codemelted::logger_set_log_level(CLogLevel::Debug);
/// assert_eq!(codemelted::logger_get_log_level(), CLogLevel::Debug);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_logger.mmd")]
pub fn logger_get_log_level() -> CLogLevel {
  let data = LOG_LEVEL.lock().unwrap();
  data.clone()
}

/// Sets the [CLogLevel] for the logging module.
///
/// **Example:**
/// ```
/// use codemelted::CLogLevel;
///
/// codemelted::logger_set_log_level(CLogLevel::Debug);
/// assert_eq!(codemelted::logger_get_log_level(), CLogLevel::Debug);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_logger.mmd")]
pub fn logger_set_log_level(log_level: CLogLevel) {
  let mut data = LOG_LEVEL.lock().unwrap();
  *data = log_level;
}

/// Gets the [CLoggedEventHandler] for the logging module.
///
/// **Example:**
/// ```
/// use codemelted::CLogRecord;
/// use codemelted::CLoggedEventHandler;
///
/// fn log_handler(data: CLogRecord) {
///   // Do something
/// }
///
/// codemelted::logger_set_log_handler(Some(log_handler));
/// assert_eq!(codemelted::logger_get_log_handler().is_some(), true);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_logger.mmd")]
pub fn logger_get_log_handler() -> Option<CLoggedEventHandler> {
  let data = LOG_HANDLER.lock().unwrap();
  *data
}

/// Sets the [CLoggedEventHandler] for the logging module.
///
/// **Example:**
/// ```
/// use codemelted::CLogRecord;
/// use codemelted::CLoggedEventHandler;
///
/// fn log_handler(data: CLogRecord) {
///   // Do something
/// }
///
/// codemelted::logger_set_log_handler(Some(log_handler));
/// assert_eq!(codemelted::logger_get_log_handler().is_some(), true);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_logger.mmd")]
pub fn logger_set_log_handler(handler: Option<CLoggedEventHandler>) {
  let mut data = LOG_HANDLER.lock().unwrap();
  *data = handler;
}

/// Will log an event via the logger module so long as it meets the
/// currently set [CLogLevel]. Once logged to STDOUT, if a
/// [CLoggedEventHandler], will pass the [CLogRecord] along for further
/// processing.
///
/// **Example:**
/// ```no_run
/// use codemelted::CLogLevel;
///
/// codemelted::logger_log(CLogLevel::Error, "Oh Know!");
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_logger.mmd")]
pub fn logger_log(level: CLogLevel, data: &str) {
  // See if we are logging this somewhere
  let logger_level = logger_get_log_level();
  if logger_level == CLogLevel::Off {
    return
  }

  // Create the log record.
  let record = CLogRecord::new(level, data);

  if record.get_log_level().as_int() >= logger_level.as_int() {
    println!("{}", record.as_string())
  }

  // Now to send it to the log handler
  let log_handler = logger_get_log_handler();
  if let Some(v) = log_handler {
    v(record);
  }
}

// ============================================================================
// [MONITOR UC IMPLEMENTATION] ================================================
// ============================================================================

// TODO:
//   Connection monitoring of TCP / UDP of open connections / and actual
//   connections
// } elseif ($action -eq "stats_tcp") {
//   return $IsLinux ? (netstat -na | grep "tcp") : (netstat -nap tcp)
// } elseif ($action -eq "stats_udp") {
//   return $IsLinux ? (netstat -na | grep "udp") : (netstat -nap tcp)

// TODO:
//   Trace Route
//   Ping
//   ipconfig /all ifconfig -na stuff

/// Defines a trait to allow a struct to transform its data into a CSV format.
pub trait CCsvFormat {
  /// Utility method to get the system id for a given monitor.
  fn system_id() -> String {
    format!(
      "{} / {} / {}",
      runtime_os_name(),
      runtime_os_version(),
      runtime_kernel_version(),
    )
  }
  /// Identifies the header associated with the data.
  fn csv_header(&self) -> String;
  /// Transforms the data into a CSV format.
  fn as_csv(&self) -> String;
}

/// The result of a [monitor_components] call providing a view of a system's
/// internal components and their current health (i.e. temperature in °C)
/// along with their failure points.
pub struct CComponentMonitor {
  components: sysinfo::Components
}
impl CComponentMonitor {
  /// Creates a new instance of the [CComponentMonitor] object.
  fn new() -> CComponentMonitor {
    let mut components = sysinfo::Components::new_with_refreshed_list();
    components.refresh(true);
    CComponentMonitor { components }
  }

  /// Refreshes the current set of tracked component data.
  pub fn refresh(&mut self) {
    self.components.refresh(true);
  }

  /// The total of components being tracked after a refresh.
  pub fn len(&self) -> usize {
    self.components.len()
  }

  /// The identifier of the given component at the specified index.
  pub fn label(&self, index: usize) -> String {
    match self.components.get(index) {
      Some(v) => {
        v.label().to_string()
      },
      None => panic!("SyntaxError: invalid index specified."),
    }
  }

  /// The current temperature °C of the given component at the specified
  /// index.
  pub fn temp_current_c(&self, index: usize) -> f32 {
    match self.components.get(index) {
      Some(v) => {
        match v.temperature() {
          Some(v) => v,
          None => f32::NAN,
        }
      },
      None => panic!("SyntaxError: invalid index specified."),
    }
  }

  /// The max temperature °C of the given component at the specified index.
  pub fn temp_max_c(&self, index: usize) -> f32 {
    match self.components.get(index) {
      Some(v) => {
        match v.max() {
          Some(v) => v,
          None => f32::NAN,
        }
      },
      None => panic!("SyntaxError: invalid index specified."),
    }
  }

  /// The critical temperature °C of the given component at the specified
  /// index.
  pub fn temp_critical_c(&self, index: usize) -> f32 {
    match self.components.get(index) {
      Some(v) => {
        match v.critical() {
          Some(v) => v,
          None => f32::NAN,
        }
      },
      None => panic!("SyntaxError: invalid index specified."),
    }
  }
}

impl CCsvFormat for CComponentMonitor {
  fn csv_header(&self) -> String {
    format!(
      "{},{},{},{},{}",
      String::from("system_id"),
      String::from("label"),
      String::from("temp_current_c"),
      String::from("temp_max_c"),
      String::from("temp_critical_c"),
    )
  }

  fn as_csv(&self) -> String {
    let mut csv_data = String::new();
    let mut x = 0;
    while x < self.len() {
      let data = format!(
        "{},{},{},{},{}",
        Self::system_id(),
        self.label(x),
        self.temp_current_c(x),
        self.temp_max_c(x),
        self.temp_critical_c(x),
      );
      csv_data.push_str(&data);
      csv_data.push('\n');
      x += 1
    }
    csv_data
  }
}

/// The result of the [monitor_disk] call provide a view of the host operating
/// systems available data disk storage. This is a self contained array of
/// disks and their associated information. Any index call beyond the
/// available disks will panic your application.
pub struct CDiskMonitor {
  disks: sysinfo::Disks
}
impl CDiskMonitor {
  /// Creates a new instance of the [CDiskMonitor] struct.
  fn new() -> CDiskMonitor {
    let mut disks = sysinfo::Disks::new_with_refreshed_list();
    disks.refresh(true);
    CDiskMonitor { disks }
  }

  /// Identifies how many disks are attached to the host operating system.
  pub fn len(&self) -> usize {
    self.disks.len()
  }

  /// Refreshes the disk data to get the latest information.
  pub fn refresh(&mut self) {
    self.disks.refresh(true);
  }

  /// Identifies how the disk is known to the host operating system.
  pub fn name(&self, index: usize) -> String {
    match self.disks.get(index) {
      Some(v) => {
        match v.name().to_str() {
          Some(v) => String::from(v),
          None => String::from("UNDETERMINED"),
        }
      },
      None => panic!("SyntaxError: index out of range."),
    }
  }

  /// Identifies the available disk space in bytes.
  pub fn disk_available_bytes(&self, index: usize) -> u64 {
    match self.disks.get(index) {
      Some(v) => v.available_space(),
      None => panic!("SyntaxError: index out of range."),
    }
  }

  /// Identifies the used space in bytes of the disk.
  pub fn disk_used_bytes(&self, index: usize) -> u64 {
    self.disk_total_bytes(index) - self.disk_available_bytes(index)
  }

  /// Identifies how big the disk is in bytes.
  pub fn disk_total_bytes(&self, index: usize) -> u64 {
    match self.disks.get(index) {
      Some(v) => v.total_space(),
      None => panic!("SyntaxError: index out of range."),
    }
  }

  /// Identifies how much of the disk is used.
  pub fn disk_load(&self, index: usize) -> f32 {
    let used_space = self.disk_used_bytes(index) as f32;
    let total_space = self.disk_total_bytes(index) as f32;
    (used_space / total_space) * 100.0
  }

  /// Identifies how the disk is formatted.
  pub fn file_system(&self, index: usize) -> String {
    match self.disks.get(index) {
      Some(v) => {
        match v.file_system().to_str() {
          Some(v) => String::from(v),
          None => String::from("UNDETERMINED"),
        }
      },
      None => panic!("SyntaxError: index out of range."),
    }
  }

  /// Determines if this disk is readonly or writeable.
  pub fn is_read_only(&self, index: usize) -> bool {
    match self.disks.get(index) {
      Some(v) => v.is_read_only(),
      None => panic!("SyntaxError: index out of range."),
    }
  }

  /// Determines if this disk is removable or not.
  pub fn is_removable(&self, index: usize) -> bool {
    match self.disks.get(index) {
      Some(v) => v.is_removable(),
      None => panic!("SyntaxError: index out of range."),
    }
  }

  /// Determines what type of disk this is.
  pub fn kind(&self, index: usize) -> String {
    match self.disks.get(index) {
      Some(v) => v.kind().to_string(),
      None => panic!("SyntaxError: index out of range."),
    }
  }

  /// Identifies the mount point of the disk.
  pub fn mount_point(&self, index: usize) -> String {
    match self.disks.get(index) {
      Some(v) => {
        match v.mount_point().to_str() {
          Some(v) => String::from(v),
          None => String::from("UNDETERMINED"),
        }
      },
      None => panic!("SyntaxError: index out of range."),
    }
  }
}
impl CCsvFormat for CDiskMonitor {
  fn csv_header(&self) -> String {
    format!(
      "{},{},{},{},{},{},{},{},{},{},{}",
      String::from("system_id"),
      String::from("name"),
      String::from("disk_available_bytes"),
      String::from("disk_used_bytes"),
      String::from("disk_total_bytes"),
      String::from("disk_load"),
      String::from("file_system"),
      String::from("is_readonly"),
      String::from("is_removable"),
      String::from("kind"),
      String::from("mount_point"),
    )
  }

  fn as_csv(&self) -> String {
    let mut csv_data = String::new();
    let mut x = 0;
    while x < self.len() {
      let data = format!(
        "{},{},{},{},{},{},{},{},{},{},{}",
        Self::system_id(),
        self.name(x),
        self.disk_available_bytes(x),
        self.disk_used_bytes(x),
        self.disk_total_bytes(x),
        self.disk_load(x),
        self.file_system(x),
        self.is_read_only(x),
        self.is_removable(x),
        self.kind(x),
        self.mount_point(x)
      );
      csv_data.push_str(&data);
      csv_data.push('\n');
      x += 1
    }
    csv_data
  }
}

/// The result of the [monitor_network] call provide a view of the host
/// operating systems available network interfaces. This is a self contained
/// hashtable of network interfaces with statistics related to overall
/// received / transmitted bytes, packets, and encountered errors.
pub struct CNetworkMonitor {
  networks: sysinfo::Networks
}
impl CNetworkMonitor {
  /// Creates the new [CNetworkMonitor] object.
  fn new() -> CNetworkMonitor {
    let networks = sysinfo::Networks::new_with_refreshed_list();
    CNetworkMonitor { networks }
  }

  /// Refreshes the currently held hashtable of network interfaces.
  pub fn refresh(&mut self) {
    self.networks.refresh(true);
  }

  /// Gets the name of the network interface.
  pub fn names(&self) -> Vec<String> {
    let mut names = Vec::<String>::new();
    for (name, _network) in &self.networks {
      names.push(name.to_string());
    }
    names
  }

  /// Gets the MAC address associated with the network interface.
  pub fn mac_address(&self, name: &str) -> String{
    match self.networks.get(name) {
      Some(v) => {
        v.mac_address().to_string()
      },
      None => panic!("SyntaxError: Unknown name specified."),
    }
  }

  /// Gets the Maximum Transfer Unit of the network interface.
  pub fn mtu(&self, name: &str) -> u64{
    match self.networks.get(name) {
      Some(v) => {
        v.mtu()
      },
      None => panic!("SyntaxError: Unknown name specified."),
    }
  }

  /// Gets the total received bytes on the network interface.
  pub fn network_total_rx_bytes(&self, name: &str) -> u64 {
    match self.networks.get(name) {
      Some(v) => {
        v.total_received()
      },
      None => panic!("SyntaxError: Unknown name specified."),
    }
  }

  /// Gets the total received errors on the network interface.
  pub fn network_total_rx_errors(&self, name: &str) -> u64 {
    match self.networks.get(name) {
      Some(v) => {
        v.total_errors_on_received()
      },
      None => panic!("SyntaxError: Unknown name specified."),
    }
  }

  /// Gets the total received packets on the network interface.
  pub fn network_total_rx_packets(&self, name: &str) -> u64 {
    match self.networks.get(name) {
      Some(v) => {
        v.total_packets_received()
      },
      None => panic!("SyntaxError: Unknown name specified."),
    }
  }

  /// Gets the total transmitted bytes on the network interface.
  pub fn network_total_tx_bytes(&self, name: &str) -> u64 {
    match self.networks.get(name) {
      Some(v) => {
        v.total_transmitted()
      },
      None => panic!("SyntaxError: Unknown name specified."),
    }
  }

  /// Gets the total transmitted errors on the network interface.
  pub fn network_total_tx_errors(&self, name: &str) -> u64 {
    match self.networks.get(name) {
      Some(v) => {
        v.total_errors_on_received()
      },
      None => panic!("SyntaxError: Unknown name specified."),
    }
  }

  /// Gets the total transmitted packets on the network interface.
  pub fn network_total_tx_packets(&self, name: &str) -> u64 {
    match self.networks.get(name) {
      Some(v) => {
        v.total_packets_received()
      },
      None => panic!("SyntaxError: Unknown name specified."),
    }
  }
}
impl CCsvFormat for CNetworkMonitor {
  fn csv_header(&self) -> String {
    format!(
      "{},{},{},{},{},{},{},{},{},{}",
      String::from("system_id"),
      String::from("name"),
      String::from("mac_address"),
      String::from("mtu"),
      String::from("network_total_rx_bytes"),
      String::from("network_total_rx_errors"),
      String::from("network_total_rx_packets"),
      String::from("network_total_tx_bytes"),
      String::from("network_total_tx_errors"),
      String::from("network_total_tx_packets"),
    )
  }
  fn as_csv(&self) -> String {
    let mut csv_data = String::new();
    let names = self.names();
    for name in names {
      let data = format!(
        "{},{},{},{},{},{},{},{},{},{}",
        Self::system_id(),
        name,
        self.mac_address(&name),
        self.mtu(&name),
        self.network_total_rx_bytes(&name),
        self.network_total_rx_errors(&name),
        self.network_total_rx_packets(&name),
        self.network_total_tx_bytes(&name),
        self.network_total_tx_errors(&name),
        self.network_total_tx_packets(&name),
      );
      csv_data.push_str(&data);
      csv_data.push('\n');
    }
    csv_data
  }
}

/// Provides a monitoring object to measure the platform's overall CPU /
/// memory utilization. This is accomplished by calling the
/// [monitor_performance] function to get a reference of the object. From
/// here you have the ability to sample the data of interest, refresh to
/// get the latest update, and if necessary capture a [CCsvFormat] of the
/// data.
pub struct CPerformanceMonitor {
  sys: sysinfo::System
}
impl CPerformanceMonitor {
  /// Creates an instance of the monitor object. Only accessible via the
  /// [monitor] function.
  fn new() -> CPerformanceMonitor {
    let sys = sysinfo::System::new_all();
    CPerformanceMonitor { sys }
  }

  /// Refreshes the CPU and memory statistics.
  pub fn refresh(&mut self) {
    self.sys.refresh_memory();
    self.sys.refresh_cpu_usage();
  }

  /// Identifies the current CPU % utilization distributed across the whole
  /// of the available CPUs of the operating system.
  pub fn cpu_load(&self) -> f32 {
    let mut count: f32 = 0.0;
    let mut total: f32 = 0.0;
    for cpu in self.sys.cpus() {
      total += cpu.cpu_usage();
      count += 1.0;
    }
    total / count
  }

  /// Identifies the current available memory in bytes.
  pub fn memory_available_bytes(&self) -> u64 {
    self.sys.available_memory()
  }

  /// Identifies the current free memory in bytes.
  pub fn memory_free_bytes(&self) -> u64 {
    self.sys.free_memory()
  }

  /// Identifies the current used memory in bytes.
  pub fn memory_used_bytes(&self) -> u64 {
    self.sys.used_memory()
  }

  /// Identifies the total memory in bytes.
  pub fn memory_total_bytes(&self) -> u64 {
    self.sys.total_memory()
  }

  /// Determines the percentage of used memory vs. total bytes.
  pub fn memory_load(&self) -> f32 {
    let used_bytes = self.memory_used_bytes() as f32;
    let total_bytes = self.memory_total_bytes() as f32;
    (used_bytes / total_bytes) * 100.0
  }

  /// Identifies the swap free space in bytes.
  pub fn swap_free_bytes(&self) -> u64 {
    self.sys.free_swap()
  }

  /// Identifies the swap used space in bytes.
  pub fn swap_used_bytes(&self) -> u64 {
    self.sys.used_swap()
  }

  /// Identifies the swap space total bytes.
  pub fn swap_total_bytes(&self) -> u64 {
    self.sys.total_swap()
  }

  /// Identifies the swap load percentage.
  pub fn swap_load(&self) -> f32 {
    let used_bytes = self.swap_used_bytes() as f32;
    let total_bytes = self.swap_total_bytes() as f32;
    (used_bytes / total_bytes) * 100.0
  }
}
impl CCsvFormat for CPerformanceMonitor {
  fn csv_header(&self) -> String {
    format!(
      "{},{},{},{},{},{},{},{},{},{},{},{},{}",
      String::from("system_id"),
      String::from("cpu_arch"),
      String::from("cpu_count"),
      String::from("cpu_load"),
      String::from("memory_available_bytes"),
      String::from("memory_free_bytes"),
      String::from("memory_used_bytes"),
      String::from("memory_total_bytes"),
      String::from("memory_load"),
      String::from("swap_free_bytes"),
      String::from("swap_used_bytes"),
      String::from("swap_total_bytes"),
      String::from("swap_load")
    )
  }

  fn as_csv(&self) -> String {
    format!(
      "{},{},{},{},{},{},{},{},{},{},{},{},{}",
      Self::system_id(),
      runtime_cpu_arch(),
      runtime_cpu_count(),
      self.cpu_load(),
      self.memory_available_bytes(),
      self.memory_free_bytes(),
      self.memory_used_bytes(),
      self.memory_total_bytes(),
      self.memory_load(),
      self.swap_free_bytes(),
      self.swap_used_bytes(),
      self.swap_total_bytes(),
      self.swap_load()
    )
  }
}

/// The result of the [monitor_processes] call provide a view of the host
/// operating systems currently running processes. It allows for gathering
/// data about the processes (statistics, originating application, etc.)
/// while also providing the ability to know the current run status, the
/// ability to kill, and wait for the process to exit.
///
/// - _NOTE 1: This level of monitoring may require elevated privileges of
/// the user running the application. If a panic occurs that may be the
/// reason._
/// - _NOTE 2: Any item that can't be determined will either be UNDETERMINED
/// if a String return type or 0 if a integer based field._
pub struct CProcessMonitor {
  sys: sysinfo::System
}
impl CProcessMonitor {
  /// Function that supports the [monitor] call of the module.
  fn new() -> CProcessMonitor {
    let sys = sysinfo::System::new_all();
    CProcessMonitor { sys }
  }

  /// Refreshes the held processes by this object removing any processes
  /// no longer running.
  pub fn refresh(&mut self) {
    self.sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
  }

  /// Gets the operating system currently running Process Ids from the host
  /// operating system.
  pub fn pids(&self) -> Vec<u32> {
    let mut pids = Vec::<u32>::new();
    for (pid, _process) in self.sys.processes() {
      pids.push(pid.as_u32());
    }
    pids
  }

  /// Gets the CPU% for the running process. This takes into account the
  /// number of CPUs so the value will be between 0.0 / 100.0%.
  pub fn cpu_usage(&self, pid: u32) -> f32 {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => {
        v.cpu_usage() / runtime_cpu_count() as f32
      },
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// The current working directory of the given pid.
  pub fn cwd(&self, pid: u32) -> String {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => {
        match v.cwd() {
          Some(v) => {
            match v.to_str() {
              Some(v) => String::from(v),
              None => String::from("UNDETERMINED"),
            }
          },
          None => String::from("UNDETERMINED"),
        }
      },
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns number of bytes read to disk for the given pid.
  pub fn disk_total_read_bytes(&self, pid: u32) -> u64 {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => v.disk_usage().total_read_bytes,
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns number of bytes written to disk for the given pid.
  pub fn disk_total_written_bytes(&self, pid: u32) -> u64 {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => v.disk_usage().total_written_bytes,
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// The path to the process being run for the given pid.
  pub fn exe(&self, pid: u32) -> String {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => {
        match v.exe() {
          Some(v) => {
            match v.to_str() {
              Some(v) => String::from(v),
              None => String::from("UNDETERMINED"),
            }
          },
          None => String::from("UNDETERMINED"),
        }
      },
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns the group ID of the process. For Windows this will always
  /// return UNDETERMINED.
  pub fn group_id(&self, pid: u32) -> String {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => {
        match v.group_id() {
          Some(v) => v.to_string(),
          None => String::from("UNDETERMINED"),
        }
      },
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns the memory usage (in bytes) of the specified pid.
  pub fn memory_usage_bytes(&self, pid: u32) -> u64 {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => v.memory(),
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns the virtual memory usage (in bytes) of the specified pid.
  pub fn memory_virtual_bytes(&self, pid: u32) -> u64 {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => v.virtual_memory(),
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns the name of the process of the specified pid.
  pub fn name(&self, pid: u32) -> String {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => {
        match v.name().to_str() {
          Some(v) => String::from(v),
          None => String::from("UNDETERMINED"),
        }
      },
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns the number of open files in the current process for
  /// the given pid.
  pub fn open_files(&self, pid: u32) -> u32 {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => {
        match v.open_files() {
          Some(v) => v,
          None => 0,
        }
      },
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns the parent PID of the specified pid.
  pub fn parent_pid(&self, pid: u32) -> u32 {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => {
        match v.parent() {
          Some(v) => v.as_u32(),
          None => 0,
        }
      },
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns the path of the root directory for the given pid.
  pub fn root(&self, pid: u32) -> String {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => {
        match v.root() {
          Some(v) => {
            match v.to_str() {
              Some(v) => String::from(v),
              None => String::from("UNDETERMINED"),
            }
          },
          None => String::from("UNDETERMINED"),
        }
      },
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns the session ID for the current process or 0 if it couldn't
  /// be retrieved for the given pid.
  pub fn session_id(&self, pid: u32) -> u32 {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => {
        match v.session_id() {
          Some(v) => v.as_u32(),
          None => 0,
        }
      },
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns the status of the process for the given pid.
  pub fn status(&self, pid: u32) -> String {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => v.status().to_string(),
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns the time where the process was started (in seconds)
  /// from epoch of the specified pid.
  pub fn time_started_seconds(&self, pid: u32) -> u64 {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => v.start_time(),
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns for how much time the process has been running (in seconds)
  /// of the given pid.
  pub fn time_running_seconds(&self, pid: u32) -> u64 {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => v.run_time(),
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Returns the ID of the owner user of this process or UNDETERMINED
  /// for the given pid.
  pub fn user_id(&self, pid: u32) -> String {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => {
        match v.user_id()  {
          Some(v) => v.to_string(),
          None => String::from("UNDETERMINED"),
        }
      },
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Kills the specified pid returning true if the signal was sent. This
  /// does not wait for the given pid to be killed. Call
  /// [CProcessMonitor::wait] after calling this to wait.
  pub fn kill(&self, pid: u32) -> bool {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => v.kill(),
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }

  /// Waits for a given pid to complete and returns its status code from the
  /// operating system. `-1` is returned if it could not be determined.
  pub fn wait(&self, pid: u32) -> i32 {
    match self.sys.process(sysinfo::Pid::from_u32(pid)) {
      Some(v) => {
        match v.wait() {
          Some(v) => {
            match v.code() {
              Some(v) => v,
              None => -1 as i32,
            }
          },
          None => -1 as i32,
        }
      },
      None => panic!("SyntaxError: Unknown pid specified."),
    }
  }
}
impl CCsvFormat for CProcessMonitor {
  fn csv_header(&self) -> String {
    format!(
      "{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}",
      String::from("system_id"),
      String::from("pid"),
      String::from("cpu_usage"),
      String::from("cwd"),
      String::from("disk_total_read_bytes"),
      String::from("disk_total_written_bytes"),
      String::from("exe"),
      String::from("group_id"),
      String::from("memory_usage_bytes"),
      String::from("memory_virtual_bytes"),
      String::from("name"),
      String::from("open_files"),
      String::from("parent_pid"),
      String::from("root"),
      String::from("session_id"),
      String::from("status"),
      String::from("time_started_seconds"),
      String::from("time_running_seconds"),
      String::from("user_id"),
    )
  }

  fn as_csv(&self) -> String {
    let mut csv_data = String::new();
    let pids = self.pids();
    for pid in pids {
      let data = format!(
        "{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}",
        Self::system_id(),
        pid,
        self.cpu_usage(pid),
        self.cwd(pid),
        self.disk_total_read_bytes(pid),
        self.disk_total_written_bytes(pid),
        self.exe(pid),
        self.group_id(pid),
        self.memory_usage_bytes(pid),
        self.memory_virtual_bytes(pid),
        self.name(pid),
        self.open_files(pid),
        self.parent_pid(pid),
        self.root(pid),
        self.session_id(pid),
        self.status(pid),
        self.time_started_seconds(pid),
        self.time_running_seconds(pid),
        self.user_id(pid),
      );
      csv_data.push_str(&data);
      csv_data.push('\n');
    }
    csv_data
  }
}

/// Constructs a [CComponentMonitor] to monitor attached hardware component
/// temperatures in °C.
///
/// _NOTE: Only certain operating systems will support this. If they don't
///        the [CComponentMonitor::len] will always be 0._
///
/// **Example:**
/// ```
/// use codemelted::CComponentMonitor;
///
/// let mut monitor = codemelted::monitor_components();
/// monitor.refresh();
/// let len = monitor.len();
/// assert!(len >= 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_monitor.mmd")]
pub fn monitor_components() -> CComponentMonitor {
  CComponentMonitor::new()
}

/// Constructs a [CDiskMonitor] to monitor the attached disk drives to
/// the host operating system.
///
/// **Example:**
/// ```
/// use codemelted::CDiskMonitor;
///
/// let mut monitor = codemelted::monitor_disk();
/// monitor.refresh();
/// let len = monitor.len();
/// let disk_load = monitor.disk_load(0);
/// assert!(len > 0);
/// assert!(disk_load > 0.0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_monitor.mmd")]
pub fn monitor_disk() -> CDiskMonitor {
  CDiskMonitor::new()
}

/// Constructs a [CNetworkMonitor] to monitor the network interfaces to the
/// host operating system.
///
/// **Example:**
/// ```
/// use codemelted::CNetworkMonitor;
///
/// let mut monitor = codemelted::monitor_network();
/// monitor.refresh();
/// let len = monitor.names().len();
/// assert!(len >= 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_monitor.mmd")]
pub fn monitor_network() -> CNetworkMonitor {
  CNetworkMonitor::new()
}

/// Constructs a [CPerformanceMonitor] to monitor the overall CPU / memory
/// utilization.
///
/// ```
/// use codemelted::CPerformanceMonitor;
///
/// let mut monitor = codemelted::monitor_performance();
/// monitor.refresh();
/// let cpu_load1 = monitor.cpu_load();
/// codemelted::async_sleep(500);
/// monitor.refresh();
/// let cpu_load2 = monitor.cpu_load();
/// assert!(cpu_load1 != cpu_load2);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_monitor.mmd")]
pub fn monitor_performance() -> CPerformanceMonitor {
  CPerformanceMonitor::new()
}

/// Constructs a [CProcessMonitor] to monitor the the different running
/// processes on the host operating system.
///
/// **Example:**
/// ```
/// use codemelted::CProcessMonitor;
///
/// let mut monitor = codemelted::monitor_processes();
/// monitor.refresh();
/// let len = monitor.pids().len();
/// assert!(len >= 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_monitor.mmd")]
pub fn monitor_processes() -> CProcessMonitor {
  CProcessMonitor::new()
}

// ============================================================================
// [NETWORK UC IMPLEMENTATION] ================================================
// ============================================================================

/// Supports the [CFetchRequest::new] object construction in order to set
/// the appropriate action to take with the URL specified.
pub enum CFetchAction {
  /// Performs a DELETE (as stated 🙂)request to a backend REST API.
  Delete,
  /// Performs a GET (query) request to a backend REST API.
  Get,
  /// Performs a POST (update) request to a backend REST API.
  Post,
  /// Performs a PUT (add) request to a backend REST API.
  Put,
}

/// Object utilized with the [network_fetch] function to configure the
/// request to be made to a backend server REST API.
pub struct CFetchRequest {
  /// Wrapped request builder to support our object API.
  client: reqwest::RequestBuilder
}
impl CFetchRequest {
  /// Creates a new [CFetchRequest] object for the [network_fetch] function
  /// specifying the [CFetchAction] along with the URL endpoint of the
  /// REST API.
  pub fn new(action: CFetchAction, url: &str) -> CFetchRequest {
    let client = match action {
      CFetchAction::Delete => reqwest::Client::new().delete(url),
      CFetchAction::Get => reqwest::Client::new().get(url),
      CFetchAction::Post => reqwest::Client::new().post(url),
      CFetchAction::Put => reqwest::Client::new().put(url),
    };
    CFetchRequest { client }
  }

  /// Sets the basic auth for the request.
  pub fn basic_auth(self, username: &str, password: Option<&str>) {
    let _ = self.client.basic_auth(username, password);
  }

  /// Sets a bearer token for the request.
  pub fn bearer_auth(self, token: &str) {
    let _ = self.client.bearer_auth(token);
  }

  /// Sets the JSON body of the request.
  pub fn body(self, data: CObject) {
    let serialized = match json_as_string(&data) {
      Some(v) => v,
      None => panic!("CFetchRequest::body(): data was not valid CObject!"),
    };
    let _ = self.client.body(serialized);
  }

  /// Sets the form body of the request.
  pub fn form(self, params: std::collections::HashMap::<String, String>) {
    let _ = self.client.form(&params);
  }

  /// Sets a header parameter for the request. Call this multiple times
  /// to set multiple values.
  pub fn header(self, key: &str, value: &str) {
    let _ = self.client.header(key, value);
  }

  /// Handles the async call of the request to get the server
  /// [CFetchResponse] with the results of the call.
  async fn send(self) -> CFetchResponse {
    // Await for the response send to the API
    match self.client.send().await {
      // It says it is ok. Double check the status code.
      Ok(resp) => {
        // Make sure we are in the 200 range of HTTP status codes.
        let status = resp.status().as_u16();
        if status >= 200 || status <= 299 {
          // We are, go get the content type to determine what data was
          // received.
          println!("We got a {}", status);
          println!("Headers are {:?}", resp.headers());
          let content_type = match resp.headers().get("Content-Type") {
            Some(v) =>  {
              match v.to_str() {
                Ok(v) => v.to_string(),
                Err(_) => String::from(""),
              }
            },
            None => String::from(""),
          };

          // Was the data a byte array or blob?
          if content_type.to_lowercase().contains("application/octet-stream") ||
              content_type.to_lowercase().contains("image/") {
            let data_as_bytes = match resp.bytes().await {
              Ok(v) => Some(v.to_vec()),
              Err(_) => None,
            };
            CFetchResponse::new(status, "OK", data_as_bytes, None, None)
          // Was it JSON data?
          } else if content_type.to_lowercase().contains("application/json") {
            let data_as_json = match resp.text().await {
              Ok(data_as_string) => {
                match json_parse(&data_as_string) {
                  Some(v) => Some(v),
                  None => None,
                }
              }
              Err(_) => None,
            };
            CFetchResponse::new(status, "OK", None, data_as_json, None)
          // Welp was not JSON or bytes, assume it is text then.
          } else {
            let data_as_string = match resp.text().await {
              Ok(v) => Some(v),
              Err(_) => None,
            };
            CFetchResponse::new(status, "OK", None, None, data_as_string)
          }
        } else {
          // We might have had an OK but the status code returned was not 2XX range.
          CFetchResponse::new(status, "UNDETERMINED", None, None, None)
        }
      },
      // We had an error, go determine what the issue was.
      Err(why) => {
        let status = match why.status() {
          Some(v) => v.as_u16(),
          None => 418,
        };
        let status_text = why.without_url().to_string();
        CFetchResponse::new(status, &status_text, None, None, None)
      },
    }
  }
}

/// The response of the [network_fetch] call holding the result and
/// associated data of the request.
pub struct CFetchResponse {
  status: u16,
  status_text: String,
  data_as_bytes: Option<Vec<u8>>,
  data_as_json: Option<CObject>,
  data_as_string: Option<String>,
}
impl CFetchResponse {
  /// Creates a new [CFetchResponse] from the [CFetchRequest::send] call.
  fn new(
    status: u16,
    status_text: &str,
    data_as_bytes: Option<Vec<u8>>,
    data_as_json: Option<CObject>,
    data_as_string: Option<String>
  ) -> CFetchResponse {
    CFetchResponse {
      status,
      status_text:
      status_text.to_string(),
      data_as_bytes,
      data_as_json,
      data_as_string,
    }
  }

  /// The final HTTP Status Code associated with the request. 2XX good,
  /// 4XX - 5XX bad.
  pub fn status(&self) -> u16 {
    self.status
  }

  /// Any associated text with the status code if it was provided.
  pub fn status_text(&self) -> String {
    self.status_text.to_string()
  }

  /// The data as bytes if the request results in a blob / byte array
  /// data. None returned if not.
  pub fn data_as_bytes(&self) -> Option<Vec<u8>> {
    self.data_as_bytes.clone()
  }

  /// The data as JSON if the request results in a JSON return type
  /// data. None returned if not.
  pub fn data_as_json(&self) -> Option<CObject> {
    self.data_as_json.clone()
  }

  /// The data as string if the request results in a string return type
  /// data. None returned if not.
  pub fn data_as_string(&self) -> Option<String> {
    self.data_as_string.clone()
  }
}

/// Wrapper over the rouille::Request struct to prevent name collisions.
pub type CServerRequest = rouille::Request;

/// Wrapper over the rouille::Response struct to prevent name collisions.
pub type CServerResponse = rouille::Response;

/// Supports the [CWebSocketProtocol::post_message] to hold data of an
/// appropriate type along with the [CWebSocketProtocol::get_message] to
/// get read messages if available.
pub enum CWebSocketData {
  /// Signals no data was available on the [CWebSocketProtocol::get_message]
  /// call.
  NoData,
  /// Holds byte data from either message posting / getting.
  Bytes(Vec<u8>),
  /// Holds byte data from either message posting / getting.
  String(String),
}
impl CWebSocketData {
  /// Retrieves the bytes held by the [CWebSocketData::Bytes] or None
  /// if not that enumerated type.
  pub fn data_as_bytes(&self) -> Option<Vec<u8>> {
    match self {
      CWebSocketData::Bytes(items) => Some(items.to_owned()),
      _ => None,
    }
  }

  /// Retrieves the bytes held by the [CWebSocketData::String] or None
  /// if not that enumerated type.
  pub fn data_as_string(&self) -> Option<String> {
    match self {
      CWebSocketData::String(v) => Some(v.to_owned()),
      _ => None,
    }
  }
}

/// Object created from the [network_upgrade_web_socket] call when a web
/// socket client request is handled in the [network_serve] function call of http
/// requests.
pub struct CWebSocketProtocol {
  id: String,
  socket: Option<rouille::websocket::Websocket>,
}
impl CWebSocketProtocol {
  /// Supports the [network_upgrade_web_socket] in creating a bi-directional
  /// server-side web socket.
  fn new(
    id: &str,
    socket: rouille::websocket::Websocket
  ) -> CWebSocketProtocol {
    CWebSocketProtocol {
      id: id.to_string(),
      socket: Some(socket),
    }
  }
}
/// Implementation of the [CProtocolHandler] to wrap the
/// rouille::websocket::Websocket into this modules protocol rules. There
/// usage further explained.
impl CProtocolHandler<CWebSocketData> for CWebSocketProtocol {
  /// id given as part of the [network_upgrade_web_socket] function call.
  fn id(&mut self) -> String {
    self.id.to_string()
  }

  /// Peeks to see if a message is available on the socket and then reads
  /// it if one is available. The [CWebSocketData] will hold the given data
  /// if available.
  fn get_message(
    &mut self,
    _request: Option<&str>
  ) -> Result<CWebSocketData, std::io::Error> {
    // If socket closed by terminate, return socket closed.
    if self.socket.is_none() {
      return Err(std::io::Error::new(
        std::io::ErrorKind::BrokenPipe,
        "CWebSocketProtocol has been closed."
      ));
    }

    // Nope we still have socket, go see if we have data.
    let socket = self.socket.as_mut().unwrap();
    if socket.by_ref().peekable().peek().is_some() {
      match socket.by_ref().next() {
        // We have something, go get it.
        Some(data) => {
          match data {
            rouille::websocket::Message::Text(v) =>
              Ok(CWebSocketData::String(v.to_owned())
            ),
            rouille::websocket::Message::Binary(
              items
            ) => Ok(CWebSocketData::Bytes(
              items.to_owned()
            )),
          }
        },
        // No data available, signal as such.
        None => Ok(CWebSocketData::NoData),
      }
    } else {
      // We have nothing, signal as such.
      Ok(CWebSocketData::NoData)
    }
  }

  /// Returns true if socket is not closed, once closed, the socket is
  /// considered dead from a server perspective.
  fn is_running(&self) -> bool {
    if self.socket.is_none() {
      return false;
    }
    self.socket.as_ref().unwrap().is_closed()
  }

  /// Post a message to a client from the server.
  fn post_message(
    &mut self,
    data: CWebSocketData
  ) -> Result<(), std::io::Error> {
    // If server terminated connection, signal as such.
    if self.socket.is_none() {
      return Err(std::io::Error::new(
        std::io::ErrorKind::BrokenPipe,
        "CWebSocketProtocol has been closed."
      ));
    }

    // We still have connection, go post message and check the result.
    let socket = self.socket.as_mut().unwrap();
    let result = match data {
      // We are transmitting bytes.
      CWebSocketData::Bytes(data) => {
        socket.send_binary(&data)
      },
      // We are transmitting string.
      CWebSocketData::String(data) =>{
        socket.send_text(&data)
      },
      // An invalid enumerated value used, panic to flag how to use this
      // object properly.
      _ => {
        panic!(
          "CWebSocketProtocol::post_message(): Invalid data received"
        );
      }
    };

    // See if we have an error to handle or not.
    match result {
      Ok(_) => Ok(()),
      Err(_) => Err(std::io::Error::new(
        std::io::ErrorKind::BrokenPipe,
        "Unable to transmit data"
      )),
    }
  }

  /// Sets the internal socket reference to None thereby no longer being
  /// able to use the socket.
  ///
  /// _NOTE: Make sure when implementing this, properly shutdown sockets
  /// from client side._
  fn terminate(&mut self) {
    self.socket = None;
  }
}

/// Executes a [CFetchRequest] to a REST API endpoint resulting in a
/// [CFetchResponse] from that given server endpoint.
///
/// **Example:*
/// ```
/// use codemelted::{CFetchRequest, CFetchAction};
///
/// let resp = codemelted::network_fetch(
///   CFetchRequest::new(
///     CFetchAction::Get,
///     "https://codemelted.com/favicon.png"
///   )
/// );
/// assert!(resp.status() == 200);
/// assert!(resp.data_as_bytes().is_some());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_network.mmd")]
pub fn network_fetch(request: CFetchRequest) -> CFetchResponse {
  let rt = tokio::runtime::Runtime::new().unwrap();
  rt.block_on(async {
    request.send().await
  })
}

/// Starts a http server listener to service http socket requests via the
/// passed to the function. This function is a light wrapper around the
/// <a href="https://docs.rs/rouille/latest/rouille/index.html"
/// target="_blank">rouille crate</a> building a similar API as other
/// CodeMelted DEV modules for serving HTTP endpoints. This is also a
/// blocking call so once started, it blocks until quit so setup appropriate
/// threading if your app is doing other things besides acting as a REST API
/// endpoint.
///
/// **Example:**
/// ```no_run
/// use codemelted::{
///   CServerRequest,
///   CServerResponse
/// };
///
/// // Setup the handler
/// fn http_server_handler(_request: &CServerRequest) -> CServerResponse {
///   // Handle the request and return a response.
///   CServerResponse::text("Hello World")
/// }
///
/// // Serve it up, this is a blocking call.
/// codemelted::network_serve("127.0.0.1:80", &http_server_handler);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_network.mmd")]
pub fn network_serve<F>(bind_addr: &str, handler: F)
where
  F: Send + Sync + 'static + Fn(&CServerRequest) -> CServerResponse,
{
  rouille::start_server(bind_addr, move |request| {
    handler(request)
  });
}

/// Provides the mechanism for upgrading a to a web socket from the
/// [network_serve] function call. The result will either Websocket with
/// response or an error if the upgrade could not be carried out. The
/// Receiver is held open until the server closes the connection or the
/// connection is lost meaning the client must re-connect.
///
/// **Example (NOT TESTED):**
/// ```no_run
/// use codemelted::{
///   CServerRequest,
///   CServerResponse
/// };
///
/// // Setup the handler
/// fn http_server_handler(request: &CServerRequest) -> CServerResponse {
///   // Determine you have a web socket request...
///   match codemelted::network_upgrade_web_socket(
///       "web_socket", request, None) {
///     Ok(socket) => {
///       // Do something with the socket. This is bi-directional socket
///       // usable until the socket is terminated or closed by the client.
///     },
///     Err(why) => {
///       // However you are handling error handling.
///     }
///   }
///   // Remember, you always must return a response after you get the
///   // socket.
///   CServerResponse::text("Hello World")
/// }
///
/// // Serve it up, this is a blocking call.
/// codemelted::network_serve("127.0.0.1:80", &http_server_handler);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_network.mmd")]
pub fn network_upgrade_web_socket(
  id: &str,
  request: &CServerRequest,
  subprotocol: Option<&'static str>
) -> Result<CWebSocketProtocol, rouille::websocket::WebsocketError> {
  match rouille::websocket::start(request, subprotocol) {
    Ok(v) => {
      let socket = v.1.recv().unwrap();
      Ok(CWebSocketProtocol::new(id, socket))
    },
    Err(why) => Err(why),
  }
}

/// <center><b><mark>FUTURE IMPLEMENTATION. DON'T CALL.</mark></b></center>
pub fn network_web_rtc() {
  unimplemented!("FUTURE IMPLEMENTATION!");
}

// ============================================================================
// [NPU UC IMPLEMENTATION] ====================================================
// ============================================================================

/// Collection of mathematical formulas that support the [npu_math] function.
/// Simply specify the formula, pass the parameters, and get the answer.
pub enum CMathFormula {
  /// Distance in meters between two WGS84 points.
  GeodeticDistance,
  /// Heading in °N true North 0 - 359.
  GeodeticHeading,
  /// Speed in meters per second between two WGS84 points.
  GeodeticSpeed,
  /// `°F = (°C x 9/5) + 32`
  TemperatureCelsiusToFahrenheit,
  /// `°K = °C + 273.15`
  TemperatureCelsiusToKelvin,
  /// `°C = (°F − 32) × 5/9`
  TemperatureFahrenheitToCelsius,
  /// `°K = (°F − 32) × 5/9 + 273.15`
  TemperatureFahrenheitToKelvin,
  /// `°C = °K − 273.15`
  TemperatureKelvinToCelsius,
  /// `°F = (°K − 273.15) × 9/5 + 32`
  TemperatureKelvinToFahrenheit
}

/// Collection of constants and supporting functions to support executing
/// the run function which houses the associated formula.
impl CMathFormula {
  /// Holds the constant of PI for working with circles.
  const PI: f64 = std::f64::consts::PI;

  /// Implementation of a float based modulus function.
  fn fmod(a: f64, b: f64) -> f64 {
    // Handling negative values
    let mut answer: f64 = if a < 0.0 { -a  } else { a };
    let use_of_b: f64 = if b < 0.0 { -b } else { b };

    // Finding mod by repeated subtraction
    while answer >= use_of_b {
      answer -= b;
    }

    // Sign of result typically depends on sign of a.
    // Return answer
    if a < 0.0 {
      answer = -answer
    }

    answer
  }

  /// Helper method for calculating the geodetic distance between two
  /// points.
  fn geodetic_distance(start_latitude: f64, start_longitude: f64,
      end_latitude: f64, end_longitude: f64) -> f64 {
    // Convert degrees to radians
    let lat1 = start_latitude * CMathFormula::PI / 180.0;
    let lon1 = start_longitude * CMathFormula::PI / 180.0;

    let lat2 = end_latitude * CMathFormula::PI / 180.0;
    let lon2 = end_longitude * CMathFormula::PI / 180.0;

    // radius of earth in metres
    let r = 6378100.0;

    // P
    let rho1 = r * lat1.cos();
    let z1 = r * lat1.sin();
    let x1 = rho1 * lon1.cos();
    let y1 = rho1 * lon1.sin();

    // Q
    let rho2 = r * lat2.cos();
    let z2 = r * lat2.sin();
    let x2 = rho2 * lon2.cos();
    let y2 = rho2 * lon2.sin();

    // Dot product
    let dot = x1 * x2 + y1 * y2 + z1 * z2;
    let cos_theta = dot / (r * r);
    let theta = cos_theta.acos();

    // Distance in meters
    r * theta
  }

  /// Helper function for calculating the geodetic heading
  /// between two points.
  fn geodetic_heading(start_latitude: f64, start_longitude: f64,
      end_latitude: f64, end_longitude: f64) -> f64 {
    // Get the initial data from our variables:
    let lat1 = start_latitude * (CMathFormula::PI / 180.0);
    let lon1 = start_longitude * (CMathFormula::PI  / 180.0);
    let lat2 = end_latitude * (CMathFormula::PI  / 180.0);
    let lon2 = end_longitude * (CMathFormula::PI  / 180.0);

    // Set up our calculations
    let y = (lon2 - lon1).sin() * lat2.cos();
    let x = (lat1.cos() * lat2.sin()) -
      (lat1.sin() * lat2.cos() * (lon2 - lon1).cos());
    let rtnval = y.atan2(x) * (180.0 / CMathFormula::PI);
    CMathFormula::fmod(rtnval + 360.0, 360.0)
  }

  /// Helper function for calculating the geodetic speed between two points.
  fn geodetic_speed(start_milliseconds: f64, start_latitude: f64,
    start_longitude: f64, end_milliseconds: f64, end_latitude: f64,
    end_longitude: f64) -> f64 {
      let dist_meters = CMathFormula::geodetic_distance(
        start_latitude, start_longitude,
        end_latitude, end_longitude
      );
      let time_s = (end_milliseconds - start_milliseconds) / 1000.0;
      return dist_meters / time_s;
  }

  /// Function that is the brains for doing all the enumerated calculations.
  fn math(&self, args: &[f64]) -> f64 {
    use std::panic;
    let result = panic::catch_unwind(|| {
      match self {
        CMathFormula::GeodeticDistance => CMathFormula::geodetic_distance(
          args[0],
          args[1],
          args[2],
          args[3]
        ),
        CMathFormula::GeodeticHeading => CMathFormula::geodetic_heading(
          args[0],
          args[1],
          args[2],
          args[3]
        ),
        CMathFormula::GeodeticSpeed => CMathFormula::geodetic_speed(
          args[0],
          args[1],
          args[2],
          args[3],
          args[4],
          args[5]
        ),
        CMathFormula::TemperatureCelsiusToFahrenheit =>
          (args[0] * 9.0 / 5.0) + 32.0,
        CMathFormula::TemperatureCelsiusToKelvin => args[0] + 273.15,
        CMathFormula::TemperatureFahrenheitToCelsius =>
          (args[0] - 32.0) * (5.0 / 9.0),
        CMathFormula::TemperatureFahrenheitToKelvin =>
          (args[0] - 32.0) * (5.0 / 9.0) + 273.15,
        CMathFormula::TemperatureKelvinToCelsius => args[0] - 273.15,
        CMathFormula::TemperatureKelvinToFahrenheit =>
          (args[0] - 273.15) * (9.0 / 5.0) + 32.0,
      }
    });

    Result::expect(
      result,
      "SyntaxError: args did not match the formula selected."
    )
  }
}

/// <center><b><mark>FUTURE IMPLEMENTATION. DON'T CALL.</mark></b></center>
pub fn npu_compute() {
  unimplemented!("FUTURE IMPLEMENTATION!");
}

/// Function to execute the [CMathFormula] enumerated formula by specifying
/// enumerated formula and the arguments for the calculated result.
///
/// **Example:**
/// ```rust
/// use codemelted::CMathFormula;
///
/// let fahrenheit = codemelted::npu_math(
///   CMathFormula::TemperatureCelsiusToFahrenheit,
///   &[0.0]
/// );
/// assert_eq!(fahrenheit, 32.0);
/// ```
///
/// The result will either be the calculated f64, std::f64::NAN if the
/// formula encounters a bad mathematical condition (i.e. -1.0.sqrt()), or
/// a panic if the wrong number of args are specified.
///
/// - _NOTE: The args slice of f64 types go left to right in terms of the
/// associated formula when passing the args._
#[doc = simple_mermaid::mermaid!("models/codemelted_npu.mmd")]
pub fn npu_math(formula: CMathFormula, args: &[f64]) -> f64 {
  formula.math(args)
}

// ============================================================================
// [PROCESS UC IMPLEMENTATION] ================================================
// ============================================================================

/// Represents a bi-directional process able to allow a Rust application to
/// communicate with a hosted operating system process / command via STDIN /
/// STDOUT / STDERR. This continues until the [CProcessProtocol::terminate]
/// is called.
pub struct CProcessProtocol {
  protocol_stderr_rx: std::sync::mpsc::Receiver<u8>,
  protocol_stdout_rx: std::sync::mpsc::Receiver<u8>,
  protocol_rx_thread: std::thread::JoinHandle<()>,
  protocol_shutdown_tx: std::sync::mpsc::Sender<bool>,
  process: std::process::Child,
  protocol_stdin: std::process::ChildStdin,
}
impl CProcessProtocol {
  fn new(command: &str, args: &str) -> CProcessProtocol {
    // Go setup the process and spawn it.
    let cmd = format!("{} {}", command, args);
    let mut process = if cfg!(target_os = "windows") {
      std::process::Command::new("cmd").args(["/c", &cmd])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn().unwrap()
    } else {
      std::process::Command::new("sh").args(["-c", &cmd])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn().unwrap()
    };

    // Setup our receive pipe thread for data
    let mut stdout = process.stdout.take().unwrap();
    let mut stderr = process.stderr.take().unwrap();
    let (
      thread_stdout_tx,
      protocol_stdout_rx
    ) = std::sync::mpsc::channel::<u8>();
    let (
      thread_stderr_tx,
      protocol_stderr_rx
    ) = std::sync::mpsc::channel::<u8>();
    let (
      protocol_shutdown_tx,
      thread_shutdown_rx
    ) = std::sync::mpsc::channel::<bool>();

    let protocol_rx_thread = std::thread::spawn(move || {
      // The items we need to handle a bi-directional process in Rust.
      // Sometimes it blocks and sometimes it does not. Observed behavior
      // suggests we need a buffer of one because if the read pipe cannot
      // fill the available bytes, it will block. So only a buffer of 1 wil
      // suffice.
      let mut backoff_counter = 0;
      let mut buf = [0x00];
      loop {
        // Go read data and see what we have. This may become a blocking read
        // depending on the operating system. If it does then we are good, if
        // not then we utilize the backoff_counter to prevent us from
        // throttling the thread.
        match std::io::Read::read(&mut stdout, &mut buf) {
          Ok(v) => {
            if v != 0 {
              backoff_counter = 0;
              match thread_stdout_tx.send(buf[0]) {
                Ok(_) => continue,
                Err(_) => break, // Something bad happened, break thread.
              }
            } else {
              backoff_counter += 1;
            }
          },
          Err(_) => continue,
        }

        match std::io::Read::read(&mut stderr, &mut buf) {
          Ok(v) => {
            if v != 0 {
              backoff_counter = 0;
              match thread_stderr_tx.send(buf[0]) {
                Ok(_) => continue,
                Err(_) => break, // Something bad happened, break thread.
              }
            } else {
              backoff_counter += 1;
            }
          },
          Err(_) => continue,
        }

        // Determine if we have been called to terminate this thread.
        match thread_shutdown_rx.try_recv() {
          Ok(_) => break, // We were ordered to shutdown.
          Err(_) => {
            // A way to prevent thread throttling if we do not have a blocking
            // pipe on read.
            if backoff_counter >= 10 {
              async_sleep(1000);
            }
            continue
          },
        }
      }
    });

    // Return the created protocol
    let protocol_stdin = process.stdin.take().unwrap();
    CProcessProtocol {
      protocol_stderr_rx,
      protocol_stdout_rx,
      protocol_rx_thread,
      protocol_shutdown_tx,
      process,
      protocol_stdin,
    }
  }
}

/// Implements the [CProtocolHandler] for the [CProcessProtocol]. While the
/// process wrapped by the protocol is in its own operating system process
/// meaning it won't block your application processing, any call to
/// [CProcessProtocol::get_message] and [CProcessProtocol::post_message] are
/// synchronous calls. No thread is implemented as part of this
/// [CProtocolHandler].
impl CProtocolHandler<String> for CProcessProtocol {
  fn id(&mut self) -> String {
    self.process.id().to_string()
  }

  fn get_message(
    &mut self,
    request: Option<&str>
  ) -> Result<String, std::io::Error> {
    let mut rx_buf = Vec::<u8>::new();
    if request == Some("error") {
      loop {
        match self.protocol_stderr_rx.try_recv() {
          Ok(v) => rx_buf.push(v),
          Err(_) => break,
        };
      }
      match String::from_utf8(rx_buf) {
        Ok(v) => Ok(v),
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    } else {
      loop {
        match self.protocol_stdout_rx.try_recv() {
          Ok(v) => rx_buf.push(v),
          Err(_) => break,
        };
      }
      match String::from_utf8(rx_buf) {
        Ok(v) => Ok(v),
        Err(why) => Err(std::io::Error::new(
          std::io::ErrorKind::BrokenPipe,
          why.to_string()
        )),
      }
    }
  }

  fn is_running(&self) -> bool {
    !self.protocol_rx_thread.is_finished()
  }

  fn post_message(
    &mut self,
    data: String
  ) -> Result<(), std::io::Error> {
    let result = std::io::Write::write_all(
      &mut self.protocol_stdin,
      data.as_bytes()
    );
    match result {
      Ok(_) => {
        match std::io::Write::flush(&mut self.protocol_stdin) {
          Ok(_) => Ok(()),
          Err(why) => Err(std::io::Error::new(
            std::io::ErrorKind::BrokenPipe,
            why.to_string()
          )),
        }
      },
      Err(why) => Err(std::io::Error::new(
        std::io::ErrorKind::BrokenPipe,
        why.to_string()
      )),
    }
  }

  fn terminate(&mut self) {
    self.process.kill().expect("Process should have terminated!");
    let _ = self.protocol_shutdown_tx.send(true);
    loop {
      if !self.is_running() {
        break;
      }
      async_sleep(100);
    }
  }
}

/// Determines if a given executable command exists on the host operating
/// system. Indicated with a true / false return.
///
/// **Example:**
/// ```
/// let answer = codemelted::process_exists("duh");
/// assert!(!answer);
/// ```
/// _NOTE: System commands (like dir on windows) will return false. This only
/// works for installed regular executables._
#[doc = simple_mermaid::mermaid!("models/codemelted_process.mmd")]
pub fn process_exists(command: &str) -> bool {
  let mut proc = if cfg!(target_os = "windows") {
    std::process::Command::new("cmd")
      .args(["/c", "where", command])
      .spawn()
      .expect("Expected process to execute.")
  } else {
    std::process::Command::new("sh")
      .args(["-c", "which", command])
      .spawn()
      .expect("Expected process to execute.")
  };
  let rc = proc.wait().expect("Expected process to wait.");
  rc.success()
}

/// Will execute a command with the host operating system and return its
/// reported output. This is a blocking non-interactive call so no
/// communicating with the process via STDIN.
///
/// **Example:**
/// ```
/// let output = if cfg!(target_os = "windows") {
///   codemelted::process_run("dir", "")
/// } else {
///   codemelted::process_run("ls", "")
/// };
/// assert!(output.len() > 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_process.mmd")]
pub fn process_run(command: &str, args: &str) -> String {
  let cmd = format!("{} {}", command, args);
  let proc = if cfg!(target_os = "windows") {
    std::process::Command::new("cmd")
      .args(["/c", &cmd])
      .output()
      .expect("Expected process to execute.")
  } else {
    std::process::Command::new("sh")
      .args(["-c", &cmd])
      .output()
      .expect("Expected process to execute.")
  };
  String::from_utf8(proc.stdout).expect("Should vec<u8> to String")
}

/// Creates a bi-directional [CProcessProtocol] to support communicating
/// with other hosted operating system applications / services via STDIN,
/// STDOUT, and STDERR. You must call [CProcessProtocol::terminate] to
/// properly cleanup the process.
///
/// **Example:**
/// ```
/// use codemelted::{CProtocolHandler, CProcessProtocol};
///
/// let mut protocol = if cfg!(windows) {
///   codemelted::process_spawn("pause", "")
/// } else {
///   codemelted::process_spawn(
///     "echo Press enter to continue; read dummy;",
///     ""
///   )
/// };
/// assert!(protocol.is_running());
/// codemelted::async_sleep(250);
/// let result = protocol.get_message(None);
/// assert!(result.is_ok());
/// protocol.post_message("\n".to_owned());
/// protocol.post_message("\r".to_owned());
/// protocol.terminate();
/// assert!(!protocol.is_running());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_process.mmd")]
pub fn process_spawn(command: &str, args: &str) -> CProcessProtocol {
  CProcessProtocol::new(command, args)
}

// ============================================================================
// [RUNTIME UC IMPLEMENTATION] ================================================
// ============================================================================

/// Will identify the current CPU architecture.
///
/// **Example:**
/// ```
/// let arch = codemelted::runtime_cpu_arch();
/// assert!(!arch.is_empty());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_cpu_arch() -> String {
  sysinfo::System::cpu_arch()
}

/// Will identify the number of available CPUs for background thread
/// processing. If it can't be determined then 1 is returned.
///
/// **Example:**
/// ```
/// let count = codemelted::runtime_cpu_count();
/// assert!(count >= 1);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_cpu_count() -> usize {
  match sysinfo::System::physical_core_count() {
    Some(v) => v,
    None => 1,
  }
}

/// Provides access to the operating system environment settings. Simply
/// specify the key and get the result. If the key is not found then None
/// is returned.
///
/// **Example:**
/// ```
/// let answer = codemelted::runtime_environment("PATH").unwrap();
/// assert!(answer.len() > 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_environment(key: &str) -> Option<String> {
  match std::env::var(key) {
    Ok(val) => Some(val),
    Err(_) => None,
  }
}

/// Gets the home path of the logged in user from the host operating system.
///
/// **Example:**
/// ```
/// use codemelted::CDiskType;
///
/// let path = codemelted::runtime_home_path();
/// assert_eq!(codemelted::disk_exists(&path, CDiskType::Directory), true);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_home_path() -> String {
  let home_path = if cfg!(target_os = "windows") {
    runtime_environment("USERPROFILE")
  } else {
    runtime_environment("HOME")
  };
  match home_path {
    Some(v) => v,
    None => {
      panic!("SyntaxError: disk_home_path() unable to query.")
    },
  }
}

/// Retrieves the hostname of the given computer on the network. Will return
/// UNDETERMINED if it cannot be determined.
///
/// **Example:**
/// ```
/// let hostname = codemelted::runtime_hostname();
/// assert!(hostname.len() > 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_hostname() -> String {
  match sysinfo::System::host_name() {
    Some(v) => v,
    None => String::from("UNDETERMINED"),
  }
}

/// Retrieves the kernel version of the host operating system. UNDETERMINED
/// is returned if it could not be determined.
///
/// **Example:**
/// ```
/// let version = codemelted::runtime_kernel_version();
/// assert!(version.len() > 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_kernel_version() -> String {
  match sysinfo::System::kernel_version() {
    Some(v) => v,
    None => String::from("UNDETERMINED"),
  }
}

/// Retrieves the newline character utilized by the host operating system.
///
/// **Example:**
/// ```
/// let newline = codemelted::runtime_newline();
/// assert!(newline.len() > 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_newline() -> String {
  if cfg!(target_os = "windows") {
    String::from("\r\n")
  } else {
    String::from("\n")
  }
}

/// Determines if the host operating system has access to the open Internet.
///
/// ```
/// let online = codemelted::runtime_online(Some(1));
/// assert!(online == true || online == false);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_online(timeout: Option<u64>) -> bool {
  online::check(timeout).is_ok()
}

/// Retrieves the operating system name of the host operating system.
/// UNDETERMINED is returned if it could not be determined.
///
/// **Example:**
/// ```
/// let name = codemelted::runtime_os_name();
/// assert!(name.len() > 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_os_name() -> String {
  match sysinfo::System::name() {
    Some(v) => v,
    None => String::from("UNDETERMINED"),
  }
}

/// Retrieves the operating system version of the host operating system.
/// UNDETERMINED is returned if it could not be determined.
///
/// **Example:**
/// ```
/// let version = codemelted::runtime_os_version();
/// assert!(version.len() > 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_os_version() -> String {
  match sysinfo::System::os_version() {
    Some(v) => v,
    None => String::from("UNDETERMINED"),
  }
}

/// Gets the path separator slash character based on the host operating
/// system.
///
/// **Example:**
/// ```
/// let separator = codemelted::runtime_path_separator();
/// assert!(separator.len() > 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_path_separator() -> String {
  if cfg!(target_os = "windows") {
    String::from("\\")
  } else {
    String::from("/")
  }
}

/// Retrieves the temporary path for storing data by the host operating
/// system.
///
/// **Example:**
/// ```
/// let path = codemelted::runtime_temp_path();
/// assert!(path.len() > 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_temp_path() -> String {
  match std::env::temp_dir().to_str() {
    Some(v) => String::from(v),
    None => {
      panic!("SyntaxError: disk_temp_path() unable to query.")
    }
  }
}

/// Retrieves the logged in user session for the host operating system.
/// UNDETERMINED is returned if it could not be determined.
///
/// **Example:**
/// ```
/// let user = codemelted::runtime_user();
/// assert!(user.len() > 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_runtime.mmd")]
pub fn runtime_user() -> String {
  let user = if cfg!(target_os = "windows") {
    runtime_environment("USERNAME")
  } else {
    runtime_environment("USER")
  };
  match user {
    Some(v) => v,
    None => String::from("UNDETERMINED"),
  }
}

// ============================================================================
// [STORAGE UC IMPLEMENTATION] ================================================
// ============================================================================

/// Mutex to hold the storage object for tracking items
static STORAGE: std::sync::Mutex<Option<CObject>> = std::sync::Mutex::new(
  None
);

/// Responsible for saving the storage to a private file on disk anytime
/// a change is made to the storage.
#[doc = simple_mermaid::mermaid!("models/codemelted_storage.mmd")]
fn storage_save_file(data: &str) {
  let home_path = runtime_home_path().to_string();
  let filename = format!("{}/{}",
    home_path,
    ".codemelted_storage"
  );
  if disk_exists(&filename, CDiskType::File) {
    let _ = disk_rm(&filename);
  }
  let result = disk_write_file(
    &filename,
    CFileContents::String(data.to_owned()),
    false
  );
  if result.is_err() {
    panic!("SyntaxError: storage_save_file: {}", result.err().unwrap());
  }
}

/// Responsible for initializing the storage for the module.
/// This must be called first before the module can be used or a panic will
/// occur. It will read previous storage from disk and bring it into memory
/// for later access.
///
/// **Example:**
/// ```
/// codemelted::storage_init();
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_storage.mmd")]
pub fn storage_init() {
  let mut storage_mutex = STORAGE.lock().unwrap();
  if storage_mutex.is_none() {
    let home_path = runtime_home_path();
    let filename = format!("{}/{}",
      home_path,
      ".codemelted_storage"
    );
    let data = disk_read_file(&filename, true);
    let storage_obj = match data {
      Ok(v) => {
        let contents = v.as_string().unwrap();
        if contents.len() == 0 {
          CObject::new_object()
        } else {
          match json_parse(&contents) {
            Some(v) => v,
            None => {
              panic!("SyntaxError: .codemelted_storage file is corrupt.")
            },
          }
        }
      },
      Err(_) => CObject::new_object(),
    };
    *storage_mutex = Some(storage_obj);
  }
}

/// Clears the currently held storage in memory and disk.
///
/// **Example:**
/// ```
/// codemelted::storage_init();
/// codemelted::storage_clear();
/// assert!(codemelted::storage_length() == 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_storage.mmd")]
pub fn storage_clear() {
  let mut storage_mutex = STORAGE.lock().unwrap();
  if let Some(storage_obj) = std::ops::DerefMut::deref_mut(
    &mut storage_mutex
  ).as_mut() {
    storage_obj.clear();
    storage_save_file("");
  } else {
    panic!("SyntaxError: storage_init() not yet called!");
  }
}

/// Gets a key from the module storage or None if it don't exist.
///
/// **Example:**
/// ```
/// codemelted::storage_init();
/// codemelted::storage_set("test", "test");
/// let result = codemelted::storage_get("test");
/// assert!(result.is_some());
/// let result = codemelted::storage_get("test2");
/// assert!(result.is_none());
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_storage.mmd")]
pub fn storage_get(key: &str) -> Option<String> {
  let mut storage_mutex = STORAGE.lock().unwrap();
  if let Some(storage_obj) = std::ops::DerefMut::deref_mut(
    &mut storage_mutex
  ).as_mut() {
    match storage_obj.has_key(key) {
      true => {
        Some(storage_obj[key].to_string())
      },
      false => None
    }
  } else {
    panic!("SyntaxError: storage_init() not yet called!");
  }
}

/// Retrieves the currently held key / value pairs by the module.
#[doc = simple_mermaid::mermaid!("models/codemelted_storage.mmd")]
pub fn storage_length() -> usize {
  let mut storage_mutex = STORAGE.lock().unwrap();
  if let Some(storage_obj) = std::ops::DerefMut::deref_mut(
    &mut storage_mutex
  ).as_mut() {
    storage_obj.len()
  } else {
    panic!("SyntaxError: storage_init() not yet called!");
  }
}

/// Removes a key / value in the module storage.
///
/// **Example:**
/// ```
/// codemelted::storage_init();
/// codemelted::storage_set("test", "test");
/// let length1 = codemelted::storage_length();
/// codemelted::storage_remove("test");
/// let length2 = codemelted::storage_length();
/// assert!(length2 < length1);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_storage.mmd")]
pub fn storage_remove(key: &str) {
  let mut storage_mutex = STORAGE.lock().unwrap();
  if let Some(storage_obj) = std::ops::DerefMut::deref_mut(
    &mut storage_mutex
  ).as_mut() {
    storage_obj.remove(key);
    let data = storage_obj.dump();
    storage_save_file(&data);
  } else {
    panic!("SyntaxError: storage_init() not yet called!");
  }
}

/// Sets a key / value in the module storage.
///
/// **Example:**
/// ```
/// codemelted::storage_init();
/// codemelted::storage_set("test", "test");
/// assert!(codemelted::storage_length() != 0);
/// ```
#[doc = simple_mermaid::mermaid!("models/codemelted_storage.mmd")]
pub fn storage_set(key: &str, value: &str) {
  let mut storage_mutex = STORAGE.lock().unwrap();
  if let Some(storage_obj) = std::ops::DerefMut::deref_mut(
    &mut storage_mutex
  ).as_mut() {
    let _ = storage_obj.insert(key, value);
    let data = storage_obj.dump();
    storage_save_file(&data);
  } else {
    panic!("SyntaxError: storage_init() not yet called!");
  }
}

// ============================================================================
// [UI UC IMPLEMENTATION] =====================================================
// ============================================================================

// TBD

// ============================================================================
// [UNIT TEST DEFINITIONS] ====================================================
// ============================================================================

#[cfg(test)]
mod tests {
  // use super::*;

  #[test]
  fn test_is_nan() {
    assert_eq!(true, f64::is_nan((-1.0 as f64).sqrt()));
  }
}

// /// Used to vet logic in the `codemelted.rs` module, build complicated tests,
// /// or aid in fleshing out documentation. This is only to support the module's
// /// development and nothing more. Don't call this for anything.
// pub fn main() {
//   unimplemented!("TEST PURPOSES ONLY!");
// }