tangled
alpha
login
or
join now
sachy.dev
/
sachy-embed-core
0
fork
atom
Repo of no-std crates for my personal embedded projects
0
fork
atom
overview
issues
pulls
pipelines
Yeet facet AND kdl, return to toml
sachy.dev
2 months ago
ddd97963
07539b3e
2/2
miri.yml
success
1min 54s
test.yml
success
1min 53s
+126
-319
3 changed files
expand all
collapse all
unified
split
Cargo.lock
sachy-config
Cargo.toml
src
lib.rs
+41
-231
Cargo.lock
···
464
]
465
466
[[package]]
467
-
name = "facet"
468
-
version = "0.34.0"
469
-
source = "registry+https://github.com/rust-lang/crates.io-index"
470
-
checksum = "72ee49c69f8a398d01d9b160e3e6288c1a5f7d756e8377f0530bbb4019aa1616"
471
-
dependencies = [
472
-
"autocfg",
473
-
"facet-core",
474
-
"facet-macros",
475
-
"static_assertions",
476
-
]
477
-
478
-
[[package]]
479
-
name = "facet-core"
480
-
version = "0.34.0"
481
-
source = "registry+https://github.com/rust-lang/crates.io-index"
482
-
checksum = "87ca85b6f8c289d86e5a0daa6b402ed1edf4001ad9b6ead357cc047fff680e0d"
483
-
dependencies = [
484
-
"autocfg",
485
-
"impls",
486
-
]
487
-
488
-
[[package]]
489
-
name = "facet-kdl"
490
-
version = "0.34.0"
491
-
source = "registry+https://github.com/rust-lang/crates.io-index"
492
-
checksum = "5b52d9f35c93a85109d9d1d9042fbc1b02972c28eaceb36374995e9d166f4019"
493
-
dependencies = [
494
-
"facet",
495
-
"facet-core",
496
-
"facet-reflect",
497
-
"facet-singularize",
498
-
"facet-solver",
499
-
"kdl",
500
-
"log",
501
-
"miette",
502
-
]
503
-
504
-
[[package]]
505
-
name = "facet-macro-parse"
506
-
version = "0.34.0"
507
-
source = "registry+https://github.com/rust-lang/crates.io-index"
508
-
checksum = "294183c810413075f9c3f075c0b3554d04ad06207dad18debc649a48779321f6"
509
-
dependencies = [
510
-
"facet-macro-types",
511
-
"proc-macro2",
512
-
"quote",
513
-
]
514
-
515
-
[[package]]
516
-
name = "facet-macro-types"
517
-
version = "0.34.0"
518
-
source = "registry+https://github.com/rust-lang/crates.io-index"
519
-
checksum = "8335dd3290eb5780aa40fb5b0da6c1a1c08980af6ada54c2e0d8cbbcd52b8f33"
520
-
dependencies = [
521
-
"proc-macro2",
522
-
"quote",
523
-
"unsynn",
524
-
]
525
-
526
-
[[package]]
527
-
name = "facet-macros"
528
-
version = "0.34.0"
529
-
source = "registry+https://github.com/rust-lang/crates.io-index"
530
-
checksum = "54f7c8e20f24f6c933290da20e76ce8b62a28ea7f16ea173a1aa21cb2ebf61f0"
531
-
dependencies = [
532
-
"facet-macros-impl",
533
-
]
534
-
535
-
[[package]]
536
-
name = "facet-macros-impl"
537
-
version = "0.34.0"
538
-
source = "registry+https://github.com/rust-lang/crates.io-index"
539
-
checksum = "fc36ba0859bb5fc539e9fb9ed4dab7a5af3b9dbf080e92adaeb5041c58971fcb"
540
-
dependencies = [
541
-
"facet-macro-parse",
542
-
"facet-macro-types",
543
-
"proc-macro2",
544
-
"quote",
545
-
"strsim",
546
-
"unsynn",
547
-
]
548
-
549
-
[[package]]
550
-
name = "facet-reflect"
551
-
version = "0.34.0"
552
-
source = "registry+https://github.com/rust-lang/crates.io-index"
553
-
checksum = "d6ab47f7ccaed7b782b4cdbfa3482f16720c0e7e31c38bf5f7da8b8f8c988690"
554
-
dependencies = [
555
-
"facet-core",
556
-
"miette",
557
-
]
558
-
559
-
[[package]]
560
-
name = "facet-singularize"
561
-
version = "0.34.0"
562
-
source = "registry+https://github.com/rust-lang/crates.io-index"
563
-
checksum = "cd14cadea48902b862d1f9256f1eac9102680d3fc105e5888008e219f2de6023"
564
-
565
-
[[package]]
566
-
name = "facet-solver"
567
-
version = "0.34.0"
568
-
source = "registry+https://github.com/rust-lang/crates.io-index"
569
-
checksum = "e7a23bcde5d4f562dfed81ca31c15c241536b1b6ef0a6e46cc17d8963b9f9f33"
570
-
dependencies = [
571
-
"facet-core",
572
-
"facet-reflect",
573
-
"strsim",
574
-
]
575
-
576
-
[[package]]
577
name = "fastrand"
578
version = "2.3.0"
579
source = "registry+https://github.com/rust-lang/crates.io-index"
···
734
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
735
736
[[package]]
737
-
name = "impls"
738
-
version = "1.0.3"
739
-
source = "registry+https://github.com/rust-lang/crates.io-index"
740
-
checksum = "7a46645bbd70538861a90d0f26c31537cdf1e44aae99a794fb75a664b70951bc"
741
-
742
-
[[package]]
743
name = "indexmap"
744
version = "2.12.1"
745
source = "registry+https://github.com/rust-lang/crates.io-index"
···
781
checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2"
782
783
[[package]]
784
-
name = "kdl"
785
-
version = "6.5.0"
786
-
source = "registry+https://github.com/rust-lang/crates.io-index"
787
-
checksum = "81a29e7b50079ff44549f68c0becb1c73d7f6de2a4ea952da77966daf3d4761e"
788
-
dependencies = [
789
-
"miette",
790
-
"num",
791
-
"winnow 0.6.24",
792
-
]
793
-
794
-
[[package]]
795
name = "libc"
796
version = "0.2.179"
797
source = "registry+https://github.com/rust-lang/crates.io-index"
···
930
checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084"
931
932
[[package]]
933
-
name = "mutants"
934
-
version = "0.0.3"
935
-
source = "registry+https://github.com/rust-lang/crates.io-index"
936
-
checksum = "bc0287524726960e07b119cebd01678f852f147742ae0d925e6a520dca956126"
937
-
938
-
[[package]]
939
name = "nb"
940
version = "0.1.3"
941
source = "registry+https://github.com/rust-lang/crates.io-index"
···
988
]
989
990
[[package]]
991
-
name = "num"
992
-
version = "0.4.3"
993
-
source = "registry+https://github.com/rust-lang/crates.io-index"
994
-
checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23"
995
-
dependencies = [
996
-
"num-bigint",
997
-
"num-complex",
998
-
"num-integer",
999
-
"num-iter",
1000
-
"num-rational",
1001
-
"num-traits",
1002
-
]
1003
-
1004
-
[[package]]
1005
-
name = "num-bigint"
1006
-
version = "0.4.6"
1007
-
source = "registry+https://github.com/rust-lang/crates.io-index"
1008
-
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
1009
-
dependencies = [
1010
-
"num-integer",
1011
-
"num-traits",
1012
-
]
1013
-
1014
-
[[package]]
1015
-
name = "num-complex"
1016
-
version = "0.4.6"
1017
-
source = "registry+https://github.com/rust-lang/crates.io-index"
1018
-
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
1019
-
dependencies = [
1020
-
"num-traits",
1021
-
]
1022
-
1023
-
[[package]]
1024
name = "num-derive"
1025
version = "0.4.2"
1026
source = "registry+https://github.com/rust-lang/crates.io-index"
···
1032
]
1033
1034
[[package]]
1035
-
name = "num-integer"
1036
-
version = "0.1.46"
1037
-
source = "registry+https://github.com/rust-lang/crates.io-index"
1038
-
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
1039
-
dependencies = [
1040
-
"num-traits",
1041
-
]
1042
-
1043
-
[[package]]
1044
-
name = "num-iter"
1045
-
version = "0.1.45"
1046
-
source = "registry+https://github.com/rust-lang/crates.io-index"
1047
-
checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"
1048
-
dependencies = [
1049
-
"autocfg",
1050
-
"num-integer",
1051
-
"num-traits",
1052
-
]
1053
-
1054
-
[[package]]
1055
name = "num-modular"
1056
version = "0.6.1"
1057
source = "registry+https://github.com/rust-lang/crates.io-index"
···
1064
checksum = "537b596b97c40fcf8056d153049eb22f481c17ebce72a513ec9286e4986d1bb6"
1065
dependencies = [
1066
"num-modular",
1067
-
]
1068
-
1069
-
[[package]]
1070
-
name = "num-rational"
1071
-
version = "0.4.2"
1072
-
source = "registry+https://github.com/rust-lang/crates.io-index"
1073
-
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
1074
-
dependencies = [
1075
-
"num-bigint",
1076
-
"num-integer",
1077
-
"num-traits",
1078
]
1079
1080
[[package]]
···
1352
checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace"
1353
1354
[[package]]
1355
-
name = "rustc-hash"
1356
-
version = "2.1.1"
1357
-
source = "registry+https://github.com/rust-lang/crates.io-index"
1358
-
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
1359
-
1360
-
[[package]]
1361
name = "rustix"
1362
version = "0.38.44"
1363
source = "registry+https://github.com/rust-lang/crates.io-index"
···
1400
name = "sachy-config"
1401
version = "0.1.0"
1402
dependencies = [
1403
-
"facet",
1404
-
"facet-kdl",
1405
"miette",
0
1406
]
1407
1408
[[package]]
···
1444
"defmt 1.0.1",
1445
"embassy-time",
1446
"sachy-fmt",
1447
-
"winnow 0.7.14",
1448
]
1449
1450
[[package]]
···
1584
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
1585
1586
[[package]]
1587
-
name = "static_assertions"
1588
-
version = "1.1.0"
1589
-
source = "registry+https://github.com/rust-lang/crates.io-index"
1590
-
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
1591
-
1592
-
[[package]]
1593
name = "strsim"
1594
version = "0.11.1"
1595
source = "registry+https://github.com/rust-lang/crates.io-index"
···
1710
]
1711
1712
[[package]]
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1713
name = "typenum"
1714
version = "1.19.0"
1715
source = "registry+https://github.com/rust-lang/crates.io-index"
···
1753
version = "0.2.2"
1754
source = "registry+https://github.com/rust-lang/crates.io-index"
1755
checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254"
1756
-
1757
-
[[package]]
1758
-
name = "unsynn"
1759
-
version = "0.3.0"
1760
-
source = "registry+https://github.com/rust-lang/crates.io-index"
1761
-
checksum = "501a7adf1a4bd9951501e5c66621e972ef8874d787628b7f90e64f936ef7ec0a"
1762
-
dependencies = [
1763
-
"mutants",
1764
-
"proc-macro2",
1765
-
"rustc-hash",
1766
-
]
1767
1768
[[package]]
1769
name = "version_check"
···
1984
1985
[[package]]
1986
name = "winnow"
1987
-
version = "0.6.24"
1988
source = "registry+https://github.com/rust-lang/crates.io-index"
1989
-
checksum = "c8d71a593cc5c42ad7876e2c1fda56f314f3754c084128833e64f1345ff8a03a"
1990
dependencies = [
1991
"memchr",
1992
]
1993
-
1994
-
[[package]]
1995
-
name = "winnow"
1996
-
version = "0.7.14"
1997
-
source = "registry+https://github.com/rust-lang/crates.io-index"
1998
-
checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829"
1999
2000
[[package]]
2001
name = "wit-bindgen"
···
464
]
465
466
[[package]]
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
467
name = "fastrand"
468
version = "2.3.0"
469
source = "registry+https://github.com/rust-lang/crates.io-index"
···
624
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
625
626
[[package]]
0
0
0
0
0
0
627
name = "indexmap"
628
version = "2.12.1"
629
source = "registry+https://github.com/rust-lang/crates.io-index"
···
665
checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2"
666
667
[[package]]
0
0
0
0
0
0
0
0
0
0
0
668
name = "libc"
669
version = "0.2.179"
670
source = "registry+https://github.com/rust-lang/crates.io-index"
···
803
checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084"
804
805
[[package]]
0
0
0
0
0
0
806
name = "nb"
807
version = "0.1.3"
808
source = "registry+https://github.com/rust-lang/crates.io-index"
···
855
]
856
857
[[package]]
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
858
name = "num-derive"
859
version = "0.4.2"
860
source = "registry+https://github.com/rust-lang/crates.io-index"
···
866
]
867
868
[[package]]
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
869
name = "num-modular"
870
version = "0.6.1"
871
source = "registry+https://github.com/rust-lang/crates.io-index"
···
878
checksum = "537b596b97c40fcf8056d153049eb22f481c17ebce72a513ec9286e4986d1bb6"
879
dependencies = [
880
"num-modular",
0
0
0
0
0
0
0
0
0
0
0
881
]
882
883
[[package]]
···
1155
checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace"
1156
1157
[[package]]
0
0
0
0
0
0
1158
name = "rustix"
1159
version = "0.38.44"
1160
source = "registry+https://github.com/rust-lang/crates.io-index"
···
1197
name = "sachy-config"
1198
version = "0.1.0"
1199
dependencies = [
0
0
1200
"miette",
1201
+
"toml_edit",
1202
]
1203
1204
[[package]]
···
1240
"defmt 1.0.1",
1241
"embassy-time",
1242
"sachy-fmt",
1243
+
"winnow",
1244
]
1245
1246
[[package]]
···
1380
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
1381
1382
[[package]]
0
0
0
0
0
0
1383
name = "strsim"
1384
version = "0.11.1"
1385
source = "registry+https://github.com/rust-lang/crates.io-index"
···
1500
]
1501
1502
[[package]]
1503
+
name = "toml_datetime"
1504
+
version = "0.7.5+spec-1.1.0"
1505
+
source = "registry+https://github.com/rust-lang/crates.io-index"
1506
+
checksum = "92e1cfed4a3038bc5a127e35a2d360f145e1f4b971b551a2ba5fd7aedf7e1347"
1507
+
dependencies = [
1508
+
"serde_core",
1509
+
]
1510
+
1511
+
[[package]]
1512
+
name = "toml_edit"
1513
+
version = "0.24.0+spec-1.1.0"
1514
+
source = "registry+https://github.com/rust-lang/crates.io-index"
1515
+
checksum = "8c740b185920170a6d9191122cafef7010bd6270a3824594bff6784c04d7f09e"
1516
+
dependencies = [
1517
+
"indexmap",
1518
+
"toml_datetime",
1519
+
"toml_parser",
1520
+
"toml_writer",
1521
+
"winnow",
1522
+
]
1523
+
1524
+
[[package]]
1525
+
name = "toml_parser"
1526
+
version = "1.0.6+spec-1.1.0"
1527
+
source = "registry+https://github.com/rust-lang/crates.io-index"
1528
+
checksum = "a3198b4b0a8e11f09dd03e133c0280504d0801269e9afa46362ffde1cbeebf44"
1529
+
dependencies = [
1530
+
"winnow",
1531
+
]
1532
+
1533
+
[[package]]
1534
+
name = "toml_writer"
1535
+
version = "1.0.6+spec-1.1.0"
1536
+
source = "registry+https://github.com/rust-lang/crates.io-index"
1537
+
checksum = "ab16f14aed21ee8bfd8ec22513f7287cd4a91aa92e44edfe2c17ddd004e92607"
1538
+
1539
+
[[package]]
1540
name = "typenum"
1541
version = "1.19.0"
1542
source = "registry+https://github.com/rust-lang/crates.io-index"
···
1580
version = "0.2.2"
1581
source = "registry+https://github.com/rust-lang/crates.io-index"
1582
checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254"
0
0
0
0
0
0
0
0
0
0
0
1583
1584
[[package]]
1585
name = "version_check"
···
1800
1801
[[package]]
1802
name = "winnow"
1803
+
version = "0.7.14"
1804
source = "registry+https://github.com/rust-lang/crates.io-index"
1805
+
checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829"
1806
dependencies = [
1807
"memchr",
1808
]
0
0
0
0
0
0
1809
1810
[[package]]
1811
name = "wit-bindgen"
+1
-2
sachy-config/Cargo.toml
···
8
rust-version.workspace = true
9
10
[dependencies]
11
-
facet = "0.34"
12
-
facet-kdl = "0.34"
13
miette = { version = "7", features = ["fancy"] }
···
8
rust-version.workspace = true
9
10
[dependencies]
11
+
toml_edit = "0.24.0"
0
12
miette = { version = "7", features = ["fancy"] }
+84
-86
sachy-config/src/lib.rs
···
1
use std::io::Write;
2
3
-
use facet::Facet;
4
-
use facet_kdl as kdl;
5
-
use miette::{IntoDiagnostic, Result};
6
7
-
#[derive(Facet, Default)]
8
-
#[facet(default)]
9
-
pub struct ConfigDoc {
10
-
#[facet(kdl::child, default)]
11
-
pub constants: Constants,
12
-
#[facet(kdl::child, default)]
13
-
pub statics: Statics,
14
-
}
15
16
-
#[derive(Facet, Default)]
17
-
#[facet(default)]
18
-
pub struct Statics {
19
-
#[facet(kdl::children, default)]
20
-
statics: Vec<Static>,
21
-
}
22
23
-
#[derive(Facet, Default)]
24
-
#[facet(default)]
25
-
pub struct Constants {
26
-
#[facet(kdl::children, default)]
27
-
pub constants: Vec<Constant>,
28
-
}
29
-
30
-
#[derive(Facet)]
31
-
pub struct Static {
32
-
#[facet(kdl::node_name)]
33
-
pub name: String,
34
-
#[facet(kdl::argument)]
35
-
pub value: String,
36
-
}
37
-
38
-
#[derive(Facet)]
39
-
pub struct Constant {
40
-
#[facet(kdl::node_name)]
41
-
pub name: String,
42
-
#[facet(kdl::argument)]
43
-
pub kind: String,
44
-
#[facet(kdl::argument)]
45
-
pub value: String,
46
}
47
48
pub fn parse_config(config: &str) -> Result<String> {
49
-
let config_doc: ConfigDoc = kdl::from_str_owned(config)?;
50
let mut output = String::new();
51
52
-
for static_value in config_doc.statics.statics.iter() {
53
-
let static_output = format!(
54
-
"pub static {}: &str = \"{}\";\n",
55
-
&static_value.name, &static_value.value
56
-
);
57
-
output.push_str(&static_output);
58
-
}
59
-
60
-
for constant_value in config_doc.constants.constants.iter() {
61
-
let constant_output = format!(
62
-
"pub const {}: {} = {};\n",
63
-
&constant_value.name, &constant_value.kind, &constant_value.value,
64
-
);
65
-
output.push_str(&constant_output);
66
-
}
67
-
68
-
Ok(output)
69
-
}
70
71
-
pub fn output_config<W: Write>(config: &str, mut writer: W) -> Result<()> {
72
-
let output = parse_config(config)?;
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
73
74
-
writer.write_all(output.as_bytes()).into_diagnostic()?;
0
0
0
0
0
0
0
0
75
76
-
Ok(())
77
}
78
79
#[cfg(test)]
···
84
85
#[test]
86
fn it_parses_config() -> Result<()> {
87
-
let input = r#"constants {
88
-
FIRST "u8" "2"
89
-
VALUE "u64" "42"
90
-
}
91
-
statics {
92
-
EXAMPLE "thing"
93
-
}"#;
94
let expected = r#"pub static EXAMPLE: &str = "thing";
95
pub const FIRST: u8 = 2;
96
-
pub const VALUE: u64 = 42;
97
"#;
98
99
let output = parse_config(input)?;
···
105
106
#[test]
107
fn it_handles_only_statics() -> Result<()> {
108
-
let input = r#"statics {
109
-
EXAMPLE "thing"
110
-
}"#;
111
112
let expected = "pub static EXAMPLE: &str = \"thing\";\n";
113
···
120
121
#[test]
122
fn it_handles_only_constants() -> Result<()> {
123
-
let input = r#"constants {
124
-
FIRST "u8" "42"
125
-
}"#;
126
127
let expected = "pub const FIRST: u8 = 42;\n";
128
···
135
136
#[test]
137
fn it_handles_empty_config() -> Result<()> {
138
-
let input = r#"statics {}
139
-
constants {}"#;
0
0
0
0
0
0
0
0
140
141
let expected = "";
142
···
149
150
#[test]
151
fn it_outputs_to_file() -> Result<()> {
152
-
let input = r#"constants {
153
-
FIRST "u8" "2"
154
-
VALUE "u64" "42"
155
-
}
156
-
statics {
157
-
EXAMPLE "thing"
158
-
}"#;
159
let expected = r#"pub static EXAMPLE: &str = "thing";
160
pub const FIRST: u8 = 2;
161
pub const VALUE: u64 = 42;
···
1
use std::io::Write;
2
3
+
use miette::{IntoDiagnostic, Result, miette};
0
0
4
5
+
pub fn output_config<W: Write>(config: &str, mut writer: W) -> Result<()> {
6
+
let output = parse_config(config)?;
0
0
0
0
0
0
7
8
+
writer.write_all(output.as_bytes()).into_diagnostic()?;
0
0
0
0
0
9
10
+
Ok(())
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
11
}
12
13
pub fn parse_config(config: &str) -> Result<String> {
14
+
let config_doc = config.parse::<toml_edit::DocumentMut>().into_diagnostic()?;
15
let mut output = String::new();
16
17
+
config_doc
18
+
.get("statics")
19
+
.and_then(|item| item.as_table())
20
+
.into_iter()
21
+
.flat_map(|statics| statics.iter())
22
+
.try_for_each(|(name, item)| -> miette::Result<()> {
23
+
let value = item.as_str().ok_or_else(|| {
24
+
miette!(
25
+
"{} is not a string value. Actual type: {}",
26
+
name,
27
+
item.type_name(),
28
+
)
29
+
})?;
30
+
let static_output = format!("pub static {}: &str = \"{}\";\n", name, value);
31
+
output.push_str(&static_output);
32
+
Ok(())
33
+
})?;
0
34
35
+
config_doc
36
+
.get("constants")
37
+
.and_then(|item| item.as_table())
38
+
.into_iter()
39
+
.flat_map(|constants| constants.iter())
40
+
.try_for_each(|(name, constant_value)| -> miette::Result<()> {
41
+
let kind = &constant_value["type"];
42
+
let kind = kind.as_str().ok_or_else(|| {
43
+
miette!(
44
+
"{} is not a string value. Actual type: {}",
45
+
name,
46
+
kind.type_name(),
47
+
)
48
+
})?;
49
+
let value = &constant_value["value"];
50
+
let value = value.as_value().ok_or_else(|| {
51
+
miette!(
52
+
"{} is not a value. Actual type: {}",
53
+
name,
54
+
value.type_name(),
55
+
)
56
+
})?;
57
58
+
let constant_output = format!(
59
+
"pub const {}: {} = {};\n",
60
+
name,
61
+
kind,
62
+
value.clone().decorated("", "")
63
+
);
64
+
output.push_str(&constant_output);
65
+
Ok(())
66
+
})?;
67
68
+
Ok(output)
69
}
70
71
#[cfg(test)]
···
76
77
#[test]
78
fn it_parses_config() -> Result<()> {
79
+
let input = r#"[constants]
80
+
FIRST = { type = "u8", value = 2 }
81
+
VALUE = { type = "f32", value = 42.0 }
82
+
[statics]
83
+
EXAMPLE = "thing"
84
+
"#;
0
85
let expected = r#"pub static EXAMPLE: &str = "thing";
86
pub const FIRST: u8 = 2;
87
+
pub const VALUE: f32 = 42.0;
88
"#;
89
90
let output = parse_config(input)?;
···
96
97
#[test]
98
fn it_handles_only_statics() -> Result<()> {
99
+
let input = r#"[statics]
100
+
EXAMPLE = "thing"
101
+
"#;
102
103
let expected = "pub static EXAMPLE: &str = \"thing\";\n";
104
···
111
112
#[test]
113
fn it_handles_only_constants() -> Result<()> {
114
+
let input = r#"[constants]
115
+
FIRST = { type = "u8", value = 42 }
116
+
"#;
117
118
let expected = "pub const FIRST: u8 = 42;\n";
119
···
126
127
#[test]
128
fn it_handles_empty_config() -> Result<()> {
129
+
let input = r#"[statics]
130
+
[constants]"#;
131
+
132
+
let expected = "";
133
+
134
+
let output = parse_config(input)?;
135
+
136
+
assert_eq!(&output, expected);
137
+
138
+
let input = "";
139
140
let expected = "";
141
···
148
149
#[test]
150
fn it_outputs_to_file() -> Result<()> {
151
+
let input = r#"[constants]
152
+
FIRST = { type = "u8", value = 2 }
153
+
VALUE = { type = "u64", value = 42 }
154
+
[statics]
155
+
EXAMPLE = "thing"
156
+
"#;
0
157
let expected = r#"pub static EXAMPLE: &str = "thing";
158
pub const FIRST: u8 = 2;
159
pub const VALUE: u64 = 42;