summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2022-12-26 21:57:03 +0100
committerFxQnLr <[email protected]>2022-12-26 21:57:03 +0100
commit0515548682a95db643a008146105d8ecdb446e58 (patch)
tree6622c5e6b0f97fe83aa559bda128d13947fe92b5
parent43206da6f40027715582599baa0ad1c91477539e (diff)
downloadmodlist-0515548682a95db643a008146105d8ecdb446e58.tar
modlist-0515548682a95db643a008146105d8ecdb446e58.tar.gz
modlist-0515548682a95db643a008146105d8ecdb446e58.zip
changed config to toml, autocreate config;
better error handling
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock310
-rw-r--r--Cargo.toml4
-rw-r--r--config.ini7
-rw-r--r--src/config.rs40
-rw-r--r--src/error.rs33
-rw-r--r--src/main.rs2
7 files changed, 124 insertions, 273 deletions
diff --git a/.gitignore b/.gitignore
index 6aebc8a..f0303eb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@
6.planmodlist.autosave.xopp 6.planmodlist.autosave.xopp
7data.db.cp 7data.db.cp
8export.toml 8export.toml
9config.toml
diff --git a/Cargo.lock b/Cargo.lock
index 6fb495c..5cb5308 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3,6 +3,21 @@
3version = 3 3version = 3
4 4
5[[package]] 5[[package]]
6name = "addr2line"
7version = "0.19.0"
8source = "registry+https://github.com/rust-lang/crates.io-index"
9checksum = "a76fd60b23679b7d19bd066031410fb7e458ccc5e958eb5c325888ce4baedc97"
10dependencies = [
11 "gimli",
12]
13
14[[package]]
15name = "adler"
16version = "1.0.2"
17source = "registry+https://github.com/rust-lang/crates.io-index"
18checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
19
20[[package]]
6name = "ahash" 21name = "ahash"
7version = "0.7.6" 22version = "0.7.6"
8source = "registry+https://github.com/rust-lang/crates.io-index" 23source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -23,23 +38,27 @@ dependencies = [
23] 38]
24 39
25[[package]] 40[[package]]
26name = "async-trait"
27version = "0.1.59"
28source = "registry+https://github.com/rust-lang/crates.io-index"
29checksum = "31e6e93155431f3931513b243d371981bb2770112b370c82745a1d19d2f99364"
30dependencies = [
31 "proc-macro2",
32 "quote",
33 "syn",
34]
35
36[[package]]
37name = "autocfg" 41name = "autocfg"
38version = "1.1.0" 42version = "1.1.0"
39source = "registry+https://github.com/rust-lang/crates.io-index" 43source = "registry+https://github.com/rust-lang/crates.io-index"
40checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 44checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
41 45
42[[package]] 46[[package]]
47name = "backtrace"
48version = "0.3.67"
49source = "registry+https://github.com/rust-lang/crates.io-index"
50checksum = "233d376d6d185f2a3093e58f283f60f880315b6c60075b01f36b3b85154564ca"
51dependencies = [
52 "addr2line",
53 "cc",
54 "cfg-if",
55 "libc",
56 "miniz_oxide",
57 "object",
58 "rustc-demangle",
59]
60
61[[package]]
43name = "base64" 62name = "base64"
44version = "0.13.1" 63version = "0.13.1"
45source = "registry+https://github.com/rust-lang/crates.io-index" 64source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -52,15 +71,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
52checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 71checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
53 72
54[[package]] 73[[package]]
55name = "block-buffer"
56version = "0.10.3"
57source = "registry+https://github.com/rust-lang/crates.io-index"
58checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e"
59dependencies = [
60 "generic-array",
61]
62
63[[package]]
64name = "bumpalo" 74name = "bumpalo"
65version = "3.11.1" 75version = "3.11.1"
66source = "registry+https://github.com/rust-lang/crates.io-index" 76source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -110,25 +120,6 @@ dependencies = [
110] 120]
111 121
112[[package]] 122[[package]]
113name = "config"
114version = "0.13.3"
115source = "registry+https://github.com/rust-lang/crates.io-index"
116checksum = "d379af7f68bfc21714c6c7dea883544201741d2ce8274bb12fa54f89507f52a7"
117dependencies = [
118 "async-trait",
119 "json5",
120 "lazy_static",
121 "nom",
122 "pathdiff",
123 "ron",
124 "rust-ini",
125 "serde",
126 "serde_json",
127 "toml",
128 "yaml-rust",
129]
130
131[[package]]
132name = "core-foundation" 123name = "core-foundation"
133version = "0.9.3" 124version = "0.9.3"
134source = "registry+https://github.com/rust-lang/crates.io-index" 125source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -145,25 +136,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
145checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" 136checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
146 137
147[[package]] 138[[package]]
148name = "cpufeatures"
149version = "0.2.5"
150source = "registry+https://github.com/rust-lang/crates.io-index"
151checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320"
152dependencies = [
153 "libc",
154]
155
156[[package]]
157name = "crypto-common"
158version = "0.1.6"
159source = "registry+https://github.com/rust-lang/crates.io-index"
160checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
161dependencies = [
162 "generic-array",
163 "typenum",
164]
165
166[[package]]
167name = "cxx" 139name = "cxx"
168version = "1.0.83" 140version = "1.0.83"
169source = "registry+https://github.com/rust-lang/crates.io-index" 141source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -208,28 +180,22 @@ dependencies = [
208] 180]
209 181
210[[package]] 182[[package]]
211name = "digest" 183name = "encoding_rs"
212version = "0.10.6" 184version = "0.8.31"
213source = "registry+https://github.com/rust-lang/crates.io-index" 185source = "registry+https://github.com/rust-lang/crates.io-index"
214checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" 186checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b"
215dependencies = [ 187dependencies = [
216 "block-buffer", 188 "cfg-if",
217 "crypto-common",
218] 189]
219 190
220[[package]] 191[[package]]
221name = "dlv-list" 192name = "error-chain"
222version = "0.3.0" 193version = "0.12.4"
223source = "registry+https://github.com/rust-lang/crates.io-index"
224checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257"
225
226[[package]]
227name = "encoding_rs"
228version = "0.8.31"
229source = "registry+https://github.com/rust-lang/crates.io-index" 194source = "registry+https://github.com/rust-lang/crates.io-index"
230checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b" 195checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc"
231dependencies = [ 196dependencies = [
232 "cfg-if", 197 "backtrace",
198 "version_check",
233] 199]
234 200
235[[package]] 201[[package]]
@@ -336,16 +302,6 @@ dependencies = [
336] 302]
337 303
338[[package]] 304[[package]]
339name = "generic-array"
340version = "0.14.6"
341source = "registry+https://github.com/rust-lang/crates.io-index"
342checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9"
343dependencies = [
344 "typenum",
345 "version_check",
346]
347
348[[package]]
349name = "getrandom" 305name = "getrandom"
350version = "0.2.8" 306version = "0.2.8"
351source = "registry+https://github.com/rust-lang/crates.io-index" 307source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -357,6 +313,12 @@ dependencies = [
357] 313]
358 314
359[[package]] 315[[package]]
316name = "gimli"
317version = "0.27.0"
318source = "registry+https://github.com/rust-lang/crates.io-index"
319checksum = "dec7af912d60cdbd3677c1af9352ebae6fb8394d165568a2234df0fa00f87793"
320
321[[package]]
360name = "h2" 322name = "h2"
361version = "0.3.15" 323version = "0.3.15"
362source = "registry+https://github.com/rust-lang/crates.io-index" 324source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -548,17 +510,6 @@ dependencies = [
548] 510]
549 511
550[[package]] 512[[package]]
551name = "json5"
552version = "0.4.1"
553source = "registry+https://github.com/rust-lang/crates.io-index"
554checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1"
555dependencies = [
556 "pest",
557 "pest_derive",
558 "serde",
559]
560
561[[package]]
562name = "lazy_static" 513name = "lazy_static"
563version = "1.4.0" 514version = "1.4.0"
564source = "registry+https://github.com/rust-lang/crates.io-index" 515source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -591,12 +542,6 @@ dependencies = [
591] 542]
592 543
593[[package]] 544[[package]]
594name = "linked-hash-map"
595version = "0.5.6"
596source = "registry+https://github.com/rust-lang/crates.io-index"
597checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
598
599[[package]]
600name = "lock_api" 545name = "lock_api"
601version = "0.4.9" 546version = "0.4.9"
602source = "registry+https://github.com/rust-lang/crates.io-index" 547source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -628,10 +573,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
628checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" 573checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
629 574
630[[package]] 575[[package]]
631name = "minimal-lexical" 576name = "miniz_oxide"
632version = "0.2.1" 577version = "0.6.2"
633source = "registry+https://github.com/rust-lang/crates.io-index" 578source = "registry+https://github.com/rust-lang/crates.io-index"
634checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" 579checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa"
580dependencies = [
581 "adler",
582]
635 583
636[[package]] 584[[package]]
637name = "mio" 585name = "mio"
@@ -647,10 +595,10 @@ dependencies = [
647 595
648[[package]] 596[[package]]
649name = "modlist" 597name = "modlist"
650version = "0.6.0" 598version = "0.6.2"
651dependencies = [ 599dependencies = [
652 "chrono", 600 "chrono",
653 "config", 601 "error-chain",
654 "futures-util", 602 "futures-util",
655 "reqwest", 603 "reqwest",
656 "rusqlite", 604 "rusqlite",
@@ -679,16 +627,6 @@ dependencies = [
679] 627]
680 628
681[[package]] 629[[package]]
682name = "nom"
683version = "7.1.1"
684source = "registry+https://github.com/rust-lang/crates.io-index"
685checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36"
686dependencies = [
687 "memchr",
688 "minimal-lexical",
689]
690
691[[package]]
692name = "num-integer" 630name = "num-integer"
693version = "0.1.45" 631version = "0.1.45"
694source = "registry+https://github.com/rust-lang/crates.io-index" 632source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -718,6 +656,15 @@ dependencies = [
718] 656]
719 657
720[[package]] 658[[package]]
659name = "object"
660version = "0.30.0"
661source = "registry+https://github.com/rust-lang/crates.io-index"
662checksum = "239da7f290cfa979f43f85a8efeee9a8a76d0827c356d37f9d3d7254d6b537fb"
663dependencies = [
664 "memchr",
665]
666
667[[package]]
721name = "once_cell" 668name = "once_cell"
722version = "1.16.0" 669version = "1.16.0"
723source = "registry+https://github.com/rust-lang/crates.io-index" 670source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -769,16 +716,6 @@ dependencies = [
769] 716]
770 717
771[[package]] 718[[package]]
772name = "ordered-multimap"
773version = "0.4.3"
774source = "registry+https://github.com/rust-lang/crates.io-index"
775checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a"
776dependencies = [
777 "dlv-list",
778 "hashbrown",
779]
780
781[[package]]
782name = "parking_lot" 719name = "parking_lot"
783version = "0.12.1" 720version = "0.12.1"
784source = "registry+https://github.com/rust-lang/crates.io-index" 721source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -802,62 +739,12 @@ dependencies = [
802] 739]
803 740
804[[package]] 741[[package]]
805name = "pathdiff"
806version = "0.2.1"
807source = "registry+https://github.com/rust-lang/crates.io-index"
808checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
809
810[[package]]
811name = "percent-encoding" 742name = "percent-encoding"
812version = "2.2.0" 743version = "2.2.0"
813source = "registry+https://github.com/rust-lang/crates.io-index" 744source = "registry+https://github.com/rust-lang/crates.io-index"
814checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" 745checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
815 746
816[[package]] 747[[package]]
817name = "pest"
818version = "2.5.1"
819source = "registry+https://github.com/rust-lang/crates.io-index"
820checksum = "cc8bed3549e0f9b0a2a78bf7c0018237a2cdf085eecbbc048e52612438e4e9d0"
821dependencies = [
822 "thiserror",
823 "ucd-trie",
824]
825
826[[package]]
827name = "pest_derive"
828version = "2.5.1"
829source = "registry+https://github.com/rust-lang/crates.io-index"
830checksum = "cdc078600d06ff90d4ed238f0119d84ab5d43dbaad278b0e33a8820293b32344"
831dependencies = [
832 "pest",
833 "pest_generator",
834]
835
836[[package]]
837name = "pest_generator"
838version = "2.5.1"
839source = "registry+https://github.com/rust-lang/crates.io-index"
840checksum = "28a1af60b1c4148bb269006a750cff8e2ea36aff34d2d96cf7be0b14d1bed23c"
841dependencies = [
842 "pest",
843 "pest_meta",
844 "proc-macro2",
845 "quote",
846 "syn",
847]
848
849[[package]]
850name = "pest_meta"
851version = "2.5.1"
852source = "registry+https://github.com/rust-lang/crates.io-index"
853checksum = "fec8605d59fc2ae0c6c1aefc0c7c7a9769732017c0ce07f7a9cfffa7b4404f20"
854dependencies = [
855 "once_cell",
856 "pest",
857 "sha1",
858]
859
860[[package]]
861name = "pin-project-lite" 748name = "pin-project-lite"
862version = "0.2.9" 749version = "0.2.9"
863source = "registry+https://github.com/rust-lang/crates.io-index" 750source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -950,17 +837,6 @@ dependencies = [
950] 837]
951 838
952[[package]] 839[[package]]
953name = "ron"
954version = "0.7.1"
955source = "registry+https://github.com/rust-lang/crates.io-index"
956checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a"
957dependencies = [
958 "base64",
959 "bitflags",
960 "serde",
961]
962
963[[package]]
964name = "rusqlite" 840name = "rusqlite"
965version = "0.28.0" 841version = "0.28.0"
966source = "registry+https://github.com/rust-lang/crates.io-index" 842source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -975,14 +851,10 @@ dependencies = [
975] 851]
976 852
977[[package]] 853[[package]]
978name = "rust-ini" 854name = "rustc-demangle"
979version = "0.18.0" 855version = "0.1.21"
980source = "registry+https://github.com/rust-lang/crates.io-index" 856source = "registry+https://github.com/rust-lang/crates.io-index"
981checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df" 857checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
982dependencies = [
983 "cfg-if",
984 "ordered-multimap",
985]
986 858
987[[package]] 859[[package]]
988name = "ryu" 860name = "ryu"
@@ -1079,17 +951,6 @@ dependencies = [
1079] 951]
1080 952
1081[[package]] 953[[package]]
1082name = "sha1"
1083version = "0.10.5"
1084source = "registry+https://github.com/rust-lang/crates.io-index"
1085checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3"
1086dependencies = [
1087 "cfg-if",
1088 "cpufeatures",
1089 "digest",
1090]
1091
1092[[package]]
1093name = "signal-hook-registry" 954name = "signal-hook-registry"
1094version = "1.4.0" 955version = "1.4.0"
1095source = "registry+https://github.com/rust-lang/crates.io-index" 956source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1158,26 +1019,6 @@ dependencies = [
1158] 1019]
1159 1020
1160[[package]] 1021[[package]]
1161name = "thiserror"
1162version = "1.0.37"
1163source = "registry+https://github.com/rust-lang/crates.io-index"
1164checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e"
1165dependencies = [
1166 "thiserror-impl",
1167]
1168
1169[[package]]
1170name = "thiserror-impl"
1171version = "1.0.37"
1172source = "registry+https://github.com/rust-lang/crates.io-index"
1173checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb"
1174dependencies = [
1175 "proc-macro2",
1176 "quote",
1177 "syn",
1178]
1179
1180[[package]]
1181name = "time" 1022name = "time"
1182version = "0.1.45" 1023version = "0.1.45"
1183source = "registry+https://github.com/rust-lang/crates.io-index" 1024source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1300,18 +1141,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1300checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" 1141checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
1301 1142
1302[[package]] 1143[[package]]
1303name = "typenum"
1304version = "1.16.0"
1305source = "registry+https://github.com/rust-lang/crates.io-index"
1306checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
1307
1308[[package]]
1309name = "ucd-trie"
1310version = "0.1.5"
1311source = "registry+https://github.com/rust-lang/crates.io-index"
1312checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81"
1313
1314[[package]]
1315name = "unicode-bidi" 1144name = "unicode-bidi"
1316version = "0.3.8" 1145version = "0.3.8"
1317source = "registry+https://github.com/rust-lang/crates.io-index" 1146source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1598,12 +1427,3 @@ checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
1598dependencies = [ 1427dependencies = [
1599 "winapi", 1428 "winapi",
1600] 1429]
1601
1602[[package]]
1603name = "yaml-rust"
1604version = "0.4.5"
1605source = "registry+https://github.com/rust-lang/crates.io-index"
1606checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
1607dependencies = [
1608 "linked-hash-map",
1609]
diff --git a/Cargo.toml b/Cargo.toml
index 13280a1..c1de0fc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
1[package] 1[package]
2name = "modlist" 2name = "modlist"
3version = "0.6.1" 3version = "0.6.2"
4edition = "2021" 4edition = "2021"
5 5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -10,8 +10,8 @@ reqwest = { version = "0.11", features = ["json", "stream"] }
10tokio = { version = "1", features = ["full"] } 10tokio = { version = "1", features = ["full"] }
11serde = { version = "1.0", features = ["derive"] } 11serde = { version = "1.0", features = ["derive"] }
12serde_json = "1.0.87" 12serde_json = "1.0.87"
13config = "0.13.2"
14rusqlite = { version = "0.28.0", features = ["bundled"] } 13rusqlite = { version = "0.28.0", features = ["bundled"] }
15futures-util = "0.3.14" 14futures-util = "0.3.14"
16chrono = "0.4.22" 15chrono = "0.4.22"
17toml = "0.5.10" 16toml = "0.5.10"
17error-chain = "0.12.4"
diff --git a/config.ini b/config.ini
deleted file mode 100644
index 345a13a..0000000
--- a/config.ini
+++ /dev/null
@@ -1,7 +0,0 @@
1data = "./"
2clean_remove = false
3downloads = "./dl"
4
5[apis]
6modrinth = "http://localhost:8080/"
7;modrinth = "https://api.modrinth.com/v2/"
diff --git a/src/config.rs b/src/config.rs
index ad59963..99d2ec2 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,27 +1,39 @@
1use config::{Config, File, FileFormat}; 1use std::{fs::File, io::{Read, Write}};
2use serde::Deserialize;
3 2
4#[derive(Debug, Clone, Deserialize)] 3use serde::{Serialize, Deserialize};
4
5use crate::error::MLE;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct Cfg { 8pub struct Cfg {
6 pub data: String, 9 pub data: String,
7 pub downloads: String,
8 pub clean_remove: bool,
9 pub apis: Apis, 10 pub apis: Apis,
10} 11}
11 12
12#[derive(Debug, Clone, Deserialize)] 13#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct Apis { 14pub struct Apis {
14 pub modrinth: String, 15 pub modrinth: String,
15} 16}
16 17
17impl Cfg { 18impl Cfg {
18 pub fn init(path: &str) -> Self { 19 pub fn init(path: &str) -> MLE<Self> {
19 //TODO Error Handling 20 let mut file = match File::open(path) {
20 Config::builder() 21 Ok(file) => file,
21 .add_source(File::new(path, FileFormat::Ini)) 22 Err(err) => {
22 .build() 23 if err.kind() == std::io::ErrorKind::NotFound {
23 .unwrap() 24 println!("No config file found, creating one");
24 .try_deserialize() 25 let default_cfg = Cfg { data: String::from("./"), apis: Apis { modrinth: String::from("https://api.modrinth.com/v2/") } };
25 .unwrap() 26 let mut file = File::create(path)?;
27 file.write_all(&toml::to_string(&default_cfg)?.as_bytes())?;
28 File::open(path)?
29 } else {
30 return Err(err.into());
31 }
32 }
33 };
34 let mut content = String::new();
35 file.read_to_string(&mut content)?;
36 let config = toml::from_str::<Cfg>(&content)?;
37 Ok(config)
26 } 38 }
27} 39}
diff --git a/src/error.rs b/src/error.rs
index c82688c..1ac2a48 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,16 +1,20 @@
1use core::fmt; 1use core::fmt;
2use serde::Deserialize;
2 3
3pub type MLE<T> = Result<T, MLError>; 4pub type MLE<T> = Result<T, MLError>;
4 5
5#[derive(Debug)] 6#[derive(Debug, Deserialize)]
6pub struct MLError { 7pub struct MLError {
7 etype: ErrorType, 8 etype: ErrorType,
8 message: String, 9 message: String,
9} 10}
10 11
11#[derive(Debug)] 12#[derive(Debug, Deserialize)]
12pub enum ErrorType { 13pub enum ErrorType {
13 ArgumentError 14 ArgumentError,
15 ConfigError,
16 LibToml,
17 IoError,
14} 18}
15 19
16impl std::error::Error for MLError { 20impl std::error::Error for MLError {
@@ -22,11 +26,32 @@ impl std::error::Error for MLError {
22impl fmt::Display for MLError { 26impl fmt::Display for MLError {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 27 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 match self.etype { 28 match self.etype {
25 ErrorType::ArgumentError => write!(f, "ARGS") 29 ErrorType::ArgumentError => write!(f, "ARGS"),
30 ErrorType::ConfigError => write!(f, "CONFIG"),
31 ErrorType::LibToml => write!(f, "TOML"),
32 ErrorType::IoError => write!(f, "IO")
26 } 33 }
27 } 34 }
28} 35}
29 36
37impl From<toml::de::Error> for MLError {
38 fn from(error: toml::de::Error) -> Self {
39 Self { etype: ErrorType::LibToml, message: error.to_string() }
40 }
41}
42
43impl From<toml::ser::Error> for MLError {
44 fn from(error: toml::ser::Error) -> Self {
45 Self { etype: ErrorType::LibToml, message: error.to_string() }
46 }
47}
48
49impl From<std::io::Error> for MLError {
50 fn from(error: std::io::Error) -> Self {
51 Self { etype: ErrorType::IoError, message: error.to_string() }
52 }
53}
54
30impl MLError { 55impl MLError {
31 pub fn new(etype: ErrorType, message: &str) -> Self { 56 pub fn new(etype: ErrorType, message: &str) -> Self {
32 Self { etype, message: String::from(message) } 57 Self { etype, message: String::from(message) }
diff --git a/src/main.rs b/src/main.rs
index 957e5aa..a093bb7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,7 +2,7 @@ use modlist::{config::Cfg, input::get_input};
2 2
3#[tokio::main] 3#[tokio::main]
4async fn main() { 4async fn main() {
5 let config = Cfg::init("config.ini"); 5 let config = Cfg::init("config.toml").unwrap();
6 //TODO Error Handling 6 //TODO Error Handling
7 get_input(config).await.unwrap(); 7 get_input(config).await.unwrap();
8} 8}