summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--Cargo.lock322
-rw-r--r--Cargo.toml2
-rw-r--r--config.ini3
-rw-r--r--src/apis/modrinth.rs101
-rw-r--r--src/commands/add.rs34
-rw-r--r--src/commands/mod.rs3
-rw-r--r--src/config.rs23
-rw-r--r--src/db.rs18
-rw-r--r--src/input.rs59
-rw-r--r--src/lib.rs11
-rw-r--r--src/main.rs7
-rw-r--r--src/update.rs8
13 files changed, 556 insertions, 38 deletions
diff --git a/.gitignore b/.gitignore
index ebdb388..5682417 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
1/target 1/target
2/api-tests \ No newline at end of file 2/api-tests
3data.db \ No newline at end of file
diff --git a/Cargo.lock b/Cargo.lock
index 815536d..4f1f0d6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3,6 +3,28 @@
3version = 3 3version = 3
4 4
5[[package]] 5[[package]]
6name = "ahash"
7version = "0.7.6"
8source = "registry+https://github.com/rust-lang/crates.io-index"
9checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
10dependencies = [
11 "getrandom",
12 "once_cell",
13 "version_check",
14]
15
16[[package]]
17name = "async-trait"
18version = "0.1.58"
19source = "registry+https://github.com/rust-lang/crates.io-index"
20checksum = "1e805d94e6b5001b651426cf4cd446b1ab5f319d27bab5c644f61de0a804360c"
21dependencies = [
22 "proc-macro2",
23 "quote",
24 "syn",
25]
26
27[[package]]
6name = "autocfg" 28name = "autocfg"
7version = "1.1.0" 29version = "1.1.0"
8source = "registry+https://github.com/rust-lang/crates.io-index" 30source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -21,6 +43,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
21checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 43checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
22 44
23[[package]] 45[[package]]
46name = "block-buffer"
47version = "0.10.3"
48source = "registry+https://github.com/rust-lang/crates.io-index"
49checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e"
50dependencies = [
51 "generic-array",
52]
53
54[[package]]
24name = "bumpalo" 55name = "bumpalo"
25version = "3.11.1" 56version = "3.11.1"
26source = "registry+https://github.com/rust-lang/crates.io-index" 57source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -45,6 +76,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
45checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 76checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
46 77
47[[package]] 78[[package]]
79name = "config"
80version = "0.13.2"
81source = "registry+https://github.com/rust-lang/crates.io-index"
82checksum = "11f1667b8320afa80d69d8bbe40830df2c8a06003d86f73d8e003b2c48df416d"
83dependencies = [
84 "async-trait",
85 "json5",
86 "lazy_static",
87 "nom",
88 "pathdiff",
89 "ron",
90 "rust-ini",
91 "serde",
92 "serde_json",
93 "toml",
94 "yaml-rust",
95]
96
97[[package]]
48name = "core-foundation" 98name = "core-foundation"
49version = "0.9.3" 99version = "0.9.3"
50source = "registry+https://github.com/rust-lang/crates.io-index" 100source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -61,6 +111,41 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
61checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" 111checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
62 112
63[[package]] 113[[package]]
114name = "cpufeatures"
115version = "0.2.5"
116source = "registry+https://github.com/rust-lang/crates.io-index"
117checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320"
118dependencies = [
119 "libc",
120]
121
122[[package]]
123name = "crypto-common"
124version = "0.1.6"
125source = "registry+https://github.com/rust-lang/crates.io-index"
126checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
127dependencies = [
128 "generic-array",
129 "typenum",
130]
131
132[[package]]
133name = "digest"
134version = "0.10.5"
135source = "registry+https://github.com/rust-lang/crates.io-index"
136checksum = "adfbc57365a37acbd2ebf2b64d7e69bb766e2fea813521ed536f5d0520dcf86c"
137dependencies = [
138 "block-buffer",
139 "crypto-common",
140]
141
142[[package]]
143name = "dlv-list"
144version = "0.3.0"
145source = "registry+https://github.com/rust-lang/crates.io-index"
146checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257"
147
148[[package]]
64name = "encoding_rs" 149name = "encoding_rs"
65version = "0.8.31" 150version = "0.8.31"
66source = "registry+https://github.com/rust-lang/crates.io-index" 151source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -148,6 +233,27 @@ dependencies = [
148] 233]
149 234
150[[package]] 235[[package]]
236name = "generic-array"
237version = "0.14.6"
238source = "registry+https://github.com/rust-lang/crates.io-index"
239checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9"
240dependencies = [
241 "typenum",
242 "version_check",
243]
244
245[[package]]
246name = "getrandom"
247version = "0.2.8"
248source = "registry+https://github.com/rust-lang/crates.io-index"
249checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
250dependencies = [
251 "cfg-if",
252 "libc",
253 "wasi",
254]
255
256[[package]]
151name = "h2" 257name = "h2"
152version = "0.3.15" 258version = "0.3.15"
153source = "registry+https://github.com/rust-lang/crates.io-index" 259source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -171,6 +277,9 @@ name = "hashbrown"
171version = "0.12.3" 277version = "0.12.3"
172source = "registry+https://github.com/rust-lang/crates.io-index" 278source = "registry+https://github.com/rust-lang/crates.io-index"
173checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" 279checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
280dependencies = [
281 "ahash",
282]
174 283
175[[package]] 284[[package]]
176name = "hermit-abi" 285name = "hermit-abi"
@@ -303,6 +412,17 @@ dependencies = [
303] 412]
304 413
305[[package]] 414[[package]]
415name = "json5"
416version = "0.4.1"
417source = "registry+https://github.com/rust-lang/crates.io-index"
418checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1"
419dependencies = [
420 "pest",
421 "pest_derive",
422 "serde",
423]
424
425[[package]]
306name = "lazy_static" 426name = "lazy_static"
307version = "1.4.0" 427version = "1.4.0"
308source = "registry+https://github.com/rust-lang/crates.io-index" 428source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -315,6 +435,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
315checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" 435checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
316 436
317[[package]] 437[[package]]
438name = "linked-hash-map"
439version = "0.5.6"
440source = "registry+https://github.com/rust-lang/crates.io-index"
441checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
442
443[[package]]
318name = "lock_api" 444name = "lock_api"
319version = "0.4.9" 445version = "0.4.9"
320source = "registry+https://github.com/rust-lang/crates.io-index" 446source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -346,6 +472,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
346checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" 472checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
347 473
348[[package]] 474[[package]]
475name = "minimal-lexical"
476version = "0.2.1"
477source = "registry+https://github.com/rust-lang/crates.io-index"
478checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
479
480[[package]]
349name = "mio" 481name = "mio"
350version = "0.8.5" 482version = "0.8.5"
351source = "registry+https://github.com/rust-lang/crates.io-index" 483source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -361,9 +493,11 @@ dependencies = [
361name = "modlist" 493name = "modlist"
362version = "0.1.0" 494version = "0.1.0"
363dependencies = [ 495dependencies = [
496 "config",
364 "reqwest", 497 "reqwest",
365 "serde", 498 "serde",
366 "serde_json", 499 "serde_json",
500 "sqlite",
367 "tokio", 501 "tokio",
368] 502]
369 503
@@ -386,6 +520,16 @@ dependencies = [
386] 520]
387 521
388[[package]] 522[[package]]
523name = "nom"
524version = "7.1.1"
525source = "registry+https://github.com/rust-lang/crates.io-index"
526checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36"
527dependencies = [
528 "memchr",
529 "minimal-lexical",
530]
531
532[[package]]
389name = "num_cpus" 533name = "num_cpus"
390version = "1.13.1" 534version = "1.13.1"
391source = "registry+https://github.com/rust-lang/crates.io-index" 535source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -447,6 +591,16 @@ dependencies = [
447] 591]
448 592
449[[package]] 593[[package]]
594name = "ordered-multimap"
595version = "0.4.3"
596source = "registry+https://github.com/rust-lang/crates.io-index"
597checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a"
598dependencies = [
599 "dlv-list",
600 "hashbrown",
601]
602
603[[package]]
450name = "parking_lot" 604name = "parking_lot"
451version = "0.12.1" 605version = "0.12.1"
452source = "registry+https://github.com/rust-lang/crates.io-index" 606source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -470,12 +624,62 @@ dependencies = [
470] 624]
471 625
472[[package]] 626[[package]]
627name = "pathdiff"
628version = "0.2.1"
629source = "registry+https://github.com/rust-lang/crates.io-index"
630checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
631
632[[package]]
473name = "percent-encoding" 633name = "percent-encoding"
474version = "2.2.0" 634version = "2.2.0"
475source = "registry+https://github.com/rust-lang/crates.io-index" 635source = "registry+https://github.com/rust-lang/crates.io-index"
476checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" 636checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
477 637
478[[package]] 638[[package]]
639name = "pest"
640version = "2.4.0"
641source = "registry+https://github.com/rust-lang/crates.io-index"
642checksum = "dbc7bc69c062e492337d74d59b120c274fd3d261b6bf6d3207d499b4b379c41a"
643dependencies = [
644 "thiserror",
645 "ucd-trie",
646]
647
648[[package]]
649name = "pest_derive"
650version = "2.4.0"
651source = "registry+https://github.com/rust-lang/crates.io-index"
652checksum = "60b75706b9642ebcb34dab3bc7750f811609a0eb1dd8b88c2d15bf628c1c65b2"
653dependencies = [
654 "pest",
655 "pest_generator",
656]
657
658[[package]]
659name = "pest_generator"
660version = "2.4.0"
661source = "registry+https://github.com/rust-lang/crates.io-index"
662checksum = "f4f9272122f5979a6511a749af9db9bfc810393f63119970d7085fed1c4ea0db"
663dependencies = [
664 "pest",
665 "pest_meta",
666 "proc-macro2",
667 "quote",
668 "syn",
669]
670
671[[package]]
672name = "pest_meta"
673version = "2.4.0"
674source = "registry+https://github.com/rust-lang/crates.io-index"
675checksum = "4c8717927f9b79515e565a64fe46c38b8cd0427e64c40680b14a7365ab09ac8d"
676dependencies = [
677 "once_cell",
678 "pest",
679 "sha1",
680]
681
682[[package]]
479name = "pin-project-lite" 683name = "pin-project-lite"
480version = "0.2.9" 684version = "0.2.9"
481source = "registry+https://github.com/rust-lang/crates.io-index" 685source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -567,6 +771,27 @@ dependencies = [
567] 771]
568 772
569[[package]] 773[[package]]
774name = "ron"
775version = "0.7.1"
776source = "registry+https://github.com/rust-lang/crates.io-index"
777checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a"
778dependencies = [
779 "base64",
780 "bitflags",
781 "serde",
782]
783
784[[package]]
785name = "rust-ini"
786version = "0.18.0"
787source = "registry+https://github.com/rust-lang/crates.io-index"
788checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df"
789dependencies = [
790 "cfg-if",
791 "ordered-multimap",
792]
793
794[[package]]
570name = "ryu" 795name = "ryu"
571version = "1.0.11" 796version = "1.0.11"
572source = "registry+https://github.com/rust-lang/crates.io-index" 797source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -655,6 +880,17 @@ dependencies = [
655] 880]
656 881
657[[package]] 882[[package]]
883name = "sha1"
884version = "0.10.5"
885source = "registry+https://github.com/rust-lang/crates.io-index"
886checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3"
887dependencies = [
888 "cfg-if",
889 "cpufeatures",
890 "digest",
891]
892
893[[package]]
658name = "signal-hook-registry" 894name = "signal-hook-registry"
659version = "1.4.0" 895version = "1.4.0"
660source = "registry+https://github.com/rust-lang/crates.io-index" 896source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -689,6 +925,36 @@ dependencies = [
689] 925]
690 926
691[[package]] 927[[package]]
928name = "sqlite"
929version = "0.27.3"
930source = "registry+https://github.com/rust-lang/crates.io-index"
931checksum = "e66cb949f931ece6201d72bffad3f3601b94998a345793713dd13af70a77c185"
932dependencies = [
933 "libc",
934 "sqlite3-sys",
935]
936
937[[package]]
938name = "sqlite3-src"
939version = "0.4.0"
940source = "registry+https://github.com/rust-lang/crates.io-index"
941checksum = "d1815a7a02c996eb8e5c64f61fcb6fd9b12e593ce265c512c5853b2513635691"
942dependencies = [
943 "cc",
944 "pkg-config",
945]
946
947[[package]]
948name = "sqlite3-sys"
949version = "0.14.0"
950source = "registry+https://github.com/rust-lang/crates.io-index"
951checksum = "d47c99824fc55360ba00caf28de0b8a0458369b832e016a64c13af0ad9fbb9ee"
952dependencies = [
953 "libc",
954 "sqlite3-src",
955]
956
957[[package]]
692name = "syn" 958name = "syn"
693version = "1.0.103" 959version = "1.0.103"
694source = "registry+https://github.com/rust-lang/crates.io-index" 960source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -714,6 +980,26 @@ dependencies = [
714] 980]
715 981
716[[package]] 982[[package]]
983name = "thiserror"
984version = "1.0.37"
985source = "registry+https://github.com/rust-lang/crates.io-index"
986checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e"
987dependencies = [
988 "thiserror-impl",
989]
990
991[[package]]
992name = "thiserror-impl"
993version = "1.0.37"
994source = "registry+https://github.com/rust-lang/crates.io-index"
995checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb"
996dependencies = [
997 "proc-macro2",
998 "quote",
999 "syn",
1000]
1001
1002[[package]]
717name = "tinyvec" 1003name = "tinyvec"
718version = "1.6.0" 1004version = "1.6.0"
719source = "registry+https://github.com/rust-lang/crates.io-index" 1005source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -784,6 +1070,15 @@ dependencies = [
784] 1070]
785 1071
786[[package]] 1072[[package]]
1073name = "toml"
1074version = "0.5.9"
1075source = "registry+https://github.com/rust-lang/crates.io-index"
1076checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
1077dependencies = [
1078 "serde",
1079]
1080
1081[[package]]
787name = "tower-service" 1082name = "tower-service"
788version = "0.3.2" 1083version = "0.3.2"
789source = "registry+https://github.com/rust-lang/crates.io-index" 1084source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -816,6 +1111,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
816checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" 1111checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
817 1112
818[[package]] 1113[[package]]
1114name = "typenum"
1115version = "1.15.0"
1116source = "registry+https://github.com/rust-lang/crates.io-index"
1117checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
1118
1119[[package]]
1120name = "ucd-trie"
1121version = "0.1.5"
1122source = "registry+https://github.com/rust-lang/crates.io-index"
1123checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81"
1124
1125[[package]]
819name = "unicode-bidi" 1126name = "unicode-bidi"
820version = "0.3.8" 1127version = "0.3.8"
821source = "registry+https://github.com/rust-lang/crates.io-index" 1128source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -854,6 +1161,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
854checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" 1161checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
855 1162
856[[package]] 1163[[package]]
1164name = "version_check"
1165version = "0.9.4"
1166source = "registry+https://github.com/rust-lang/crates.io-index"
1167checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
1168
1169[[package]]
857name = "want" 1170name = "want"
858version = "0.3.0" 1171version = "0.3.0"
859source = "registry+https://github.com/rust-lang/crates.io-index" 1172source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1075,3 +1388,12 @@ checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
1075dependencies = [ 1388dependencies = [
1076 "winapi", 1389 "winapi",
1077] 1390]
1391
1392[[package]]
1393name = "yaml-rust"
1394version = "0.4.5"
1395source = "registry+https://github.com/rust-lang/crates.io-index"
1396checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
1397dependencies = [
1398 "linked-hash-map",
1399]
diff --git a/Cargo.toml b/Cargo.toml
index cc98e0d..bbc0456 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,3 +10,5 @@ reqwest = { version = "0.11", features = ["json"] }
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"
14sqlite = "0.27.3"
diff --git a/config.ini b/config.ini
new file mode 100644
index 0000000..0ea32d5
--- /dev/null
+++ b/config.ini
@@ -0,0 +1,3 @@
1[apis]
2;modrinth = "http://localhost:8080/"
3modrinth = "https://api.modrinth.com/v2/"
diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs
index ce9fdd4..3af5bbd 100644
--- a/src/apis/modrinth.rs
+++ b/src/apis/modrinth.rs
@@ -1,25 +1,6 @@
1use serde::{Deserialize, Serialize}; 1use serde::Deserialize;
2 2
3async fn get(path: String) -> Result<Vec<u8>, Box<dyn std::error::Error>> { 3use crate::Modloader;
4 dbg!(&path);
5 let api = String::from("https://api.modrinth.com/v2/");
6 //let api = String::from("localhost:8080/");
7 //let api = String::from("https://www.rust-lang.org/");
8 let url = format!(r#"{}{}"#, api, path);
9
10 println!("{}", &url);
11
12
13 let data = reqwest::get(r#"https://api.modrinth.com/v2/projects?ids=["kYuIpRLv","89Wsn8GD"]"#)
14 .await?
15 .bytes()
16 .await?
17 .to_vec();
18
19 //println!("body = {:?}", data);
20
21 Ok(data)
22}
23 4
24#[derive(Debug, Deserialize)] 5#[derive(Debug, Deserialize)]
25pub struct Project { 6pub struct Project {
@@ -86,19 +67,89 @@ pub enum Status {
86 processing, 67 processing,
87 unknown 68 unknown
88} 69}
89pub async fn project(name: &str) -> Project { 70
71#[derive(Debug, Deserialize)]
72pub struct Version {
73 pub name: String,
74 pub version_number: String,
75 pub changelog: Option<String>,
76 pub game_versions: Vec<String>,
77 pub version_type: VersionType,
78 pub loaders: Vec<String>,
79 pub featured: bool,
80 pub id: String,
81 pub project_id: String,
82 pub author_id: String,
83 pub date_published: String,
84 pub downloads: u32,
85 pub files: Vec<VersionFile>,
86}
87
88#[allow(non_camel_case_types)]
89#[derive(Debug, Deserialize)]
90pub enum VersionType {
91 release,
92 beta,
93 alpha
94}
95
96#[derive(Debug, Deserialize)]
97pub struct VersionFile {
98 pub hashes: Hash,
99 pub url: String,
100 pub filename: String,
101 pub primary: bool,
102 pub size: u32,
103}
104
105#[derive(Debug, Deserialize)]
106pub struct Hash {
107 pub sha512: String,
108 pub sha1: String,
109}
110
111async fn get(api: String, path: String) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
112 let url = format!(r#"{}{}"#, api, path);
113
114 dbg!(&url);
115
116 let data = reqwest::get(url)
117 .await?
118 .bytes()
119 .await?
120 .to_vec();
121
122 Ok(data)
123}
124
125
126pub async fn project(api: String, name: &str) -> Project {
90 let url = format!("project/{}", name); 127 let url = format!("project/{}", name);
91 let data = get(url); 128 let data = get(api, url);
92 129
93 serde_json::from_slice(&data.await.unwrap()).unwrap() 130 serde_json::from_slice(&data.await.unwrap()).unwrap()
94} 131}
95 132
96pub async fn projects(ids: Vec<&str>) -> Vec<Project> { 133pub async fn projects(api: String, ids: Vec<&str>) -> Vec<Project> {
97 let all = ids.join(r#"",""#); 134 let all = ids.join(r#"",""#);
98 let url = format!(r#"projects?ids=["{}"]"#, all); 135 let url = format!(r#"projects?ids=["{}"]"#, all);
99 println!("{}", url); 136 println!("{}", url);
100 137
101 let data = get(url); 138 let data = get(api, url);
102 139
103 serde_json::from_slice(&data.await.unwrap()).unwrap() 140 serde_json::from_slice(&data.await.unwrap()).unwrap()
104} 141}
142
143pub async fn versions(api: String, id: String, loader: Modloader, mc_version: String) -> Vec<Version> {
144
145 let loaderstr = match loader {
146 Modloader::Forge => String::from("forge"),
147 Modloader::Fabric => String::from("fabric"),
148 };
149
150 let url = format!(r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, id, loaderstr, mc_version);
151
152 let data = get(api, url);
153
154 serde_json::from_slice(&data.await.unwrap()).unwrap()
155}
diff --git a/src/commands/add.rs b/src/commands/add.rs
new file mode 100644
index 0000000..67f63de
--- /dev/null
+++ b/src/commands/add.rs
@@ -0,0 +1,34 @@
1use std::io::{Error, ErrorKind};
2
3use crate::{modrinth::{project, versions}, config::Cfg, db::insert_mod, Modloader};
4
5pub async fn add(config: Cfg, mc_mod: String) -> Result<(), Box<dyn std::error::Error>> {
6 println!("Adding");
7
8 let project = project(String::from(&config.apis.modrinth), &mc_mod).await;
9
10 dbg!(&project);
11
12 let loader = Modloader::Fabric;
13
14 if project.versions.is_empty() { panic!("This should never happen"); };
15
16 let current_version = get_current(config, String::from(&project.id)).await?;
17
18 match insert_mod(project.id, project.title, current_version, project.versions, loader, String::from("1.19.2")) {
19 Err(err) => { Err(Box::new(err)) },
20 Ok(()) => Ok(()),
21 }
22
23}
24
25async fn get_current(config: Cfg, id: String) -> Result<String, Box<dyn std::error::Error>> {
26 let available_versions = versions(config.apis.modrinth, id, Modloader::Fabric, String::from("1.19.2")).await;
27
28 match available_versions.len() {
29 0 => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_VERSIONS_AVAILABLE"))),
30 //TODO compare publish dates
31 1.. => Ok(available_versions[0].id.to_string()),
32 _ => panic!("available_versions should never be negative"),
33 }
34}
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
new file mode 100644
index 0000000..0cc183a
--- /dev/null
+++ b/src/commands/mod.rs
@@ -0,0 +1,3 @@
1pub mod add;
2
3pub use add::*;
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..a0dfbbe
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,23 @@
1use config::{Config, File, FileFormat};
2use serde::Deserialize;
3
4#[derive(Debug, Deserialize)]
5pub struct Cfg {
6 pub apis: Apis,
7}
8
9#[derive(Debug, Deserialize)]
10pub struct Apis {
11 pub modrinth: String,
12}
13
14impl Cfg {
15 pub fn init(path: &str) -> Self {
16 Config::builder()
17 .add_source(File::new(path, FileFormat::Ini))
18 .build()
19 .unwrap()
20 .try_deserialize()
21 .unwrap()
22 }
23}
diff --git a/src/db.rs b/src/db.rs
index e69de29..3d50b0f 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -0,0 +1,18 @@
1use crate::Modloader;
2
3pub fn insert_mod(id: String, name: String, current_version: String, old_versions: Vec<String>, mod_loader: Modloader, desired_mc_version: String) -> Result<(), sqlite::Error> {
4
5 let connection = sqlite::open("./data.db").unwrap();
6
7 let loader = match mod_loader {
8 Modloader::Fabric => "fabric",
9 Modloader::Forge => "forge",
10 };
11
12 let sql = format!("INSERT INTO mods VALUES ('{}', '{}', '{}', '{}', '{}', '{}')", id, name, current_version, old_versions.join("|"), loader, desired_mc_version);
13
14 dbg!(&sql);
15
16 connection.execute(sql)
17
18}
diff --git a/src/input.rs b/src/input.rs
new file mode 100644
index 0000000..689389e
--- /dev/null
+++ b/src/input.rs
@@ -0,0 +1,59 @@
1use std::io::{stdin, Error, ErrorKind};
2use crate::{add, config::Cfg};
3
4pub struct Input {
5 pub command: String,
6 pub args: Option<Vec<String>>,
7}
8
9impl Input {
10 pub fn from(string: String) -> Result<Self, Box<dyn std::error::Error>> {
11 let mut split: Vec<&str> = string.split(' ').collect();
12
13 let command: String;
14 let mut args: Option<Vec<String>> = None;
15
16 if split[0].is_empty() { split.remove(0); };
17
18 dbg!(&split);
19
20 match split.len() {
21 0 => { Err(Box::new(Error::new(ErrorKind::InvalidInput, "NO_ARGS"))) }
22 1 => Ok( Input { command: split[0].to_string(), args }),
23 2.. => {
24 command = split[0].to_string();
25 split.remove(0);
26 let mut str_args: Vec<String> = vec![];
27 for e in split {
28 str_args.push(e.to_string());
29 }
30 args = Some(str_args);
31 Ok(Input { command, args })
32 },
33 _ => { panic!("This should never happen") }
34 }
35
36
37 }
38}
39
40pub async fn get_input(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
41 let mut user_input = String::new();
42 stdin()
43 .read_line(&mut user_input)
44 .expect("ERROR");
45
46 dbg!(&user_input);
47
48 let input = Input::from(user_input.trim().to_string())?;
49
50 match input.command.as_str() {
51 "add" => {
52 if input.args == None { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))) };
53 if input.args.as_ref().unwrap().len() != 1 { return Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))) };
54 add(config, input.args.unwrap()[0].to_string()).await?;
55 Ok(())
56 },
57 _ => Err(Box::new(Error::new(ErrorKind::InvalidInput, "UNKNOWN_COMMAND"))),
58 }
59}
diff --git a/src/lib.rs b/src/lib.rs
index cbb761c..52b0646 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,13 @@
1pub mod update;
2pub mod apis; 1pub mod apis;
2pub mod config;
3pub mod commands;
4pub mod input;
5pub mod db;
3 6
4pub use apis::*; 7pub use apis::*;
8pub use commands::*;
9
10pub enum Modloader {
11 Fabric,
12 Forge
13}
diff --git a/src/main.rs b/src/main.rs
index 8d1a1bd..957e5aa 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,8 @@
1use modlist::modrinth::projects; 1use modlist::{config::Cfg, input::get_input};
2 2
3#[tokio::main] 3#[tokio::main]
4async fn main() { 4async fn main() {
5 //projects(vec!["kYuIpRLv", "89Wsn8GD"]); 5 let config = Cfg::init("config.ini");
6 println!("{:?}", projects(vec!["kYuIpRLv", "89Wsn8GD"]).await); 6 //TODO Error Handling
7 get_input(config).await.unwrap();
7} 8}
diff --git a/src/update.rs b/src/update.rs
deleted file mode 100644
index 2e70f43..0000000
--- a/src/update.rs
+++ /dev/null
@@ -1,8 +0,0 @@
1pub fn update_mods() {
2
3}
4
5fn get_version(link: String) {
6
7
8}