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
|
use std::io::{Error, ErrorKind};
use rusqlite::Connection;
use crate::{
config::Cfg,
error::{ErrorType, MLError, MLE},
List, Modloader,
};
//MODS
pub fn mods_insert(config: Cfg, id: &str, slug: &str, name: &str) -> MLE<()> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
connection.execute(
"INSERT INTO mods (id, slug, title) VALUES (?1, ?2, ?3)",
[id, slug, name.replace('\'', "").as_str()],
)?;
Ok(())
}
pub fn mods_get_all_ids(config: Cfg) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data).unwrap();
let mut mods: Vec<String> = Vec::new();
let mut stmt = connection.prepare("SELECT id FROM mods")?;
let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
for id in id_iter {
mods.push(id?);
}
match mods.is_empty() {
true => Err(Box::new(Error::new(ErrorKind::NotFound, "NO_MODS_ALL"))),
false => Ok(mods),
}
}
///Get mod id based on the slug or name
///# Arguments
///
///* `data` - file directory of the database
///* `slug` - Slug or Name of a mod
///
///# Failure
///
///Will return `MLError` when no mod id is found
pub fn mods_get_id(data: &str, slug: &str) -> MLE<String> {
let data = format!("{}/data.db", data);
let connection = Connection::open(data)?;
let mut mod_id = String::new();
//get from slug
let mut stmt = connection.prepare("SELECT id FROM mods WHERE slug = ?")?;
let id_iter = stmt.query_map([slug], |row| row.get::<usize, String>(0))?;
for id in id_iter {
mod_id = id?;
}
//get from id if no slug found
if mod_id.is_empty() {
let mut stmt = connection.prepare("SELECT id FROM mods WHERE id = ?")?;
let id_iter = stmt.query_map([slug], |row| row.get::<usize, String>(0))?;
for id in id_iter {
mod_id = id?;
}
}
//get from title if no id found from slug
if mod_id.is_empty() {
let mut stmt = connection.prepare("SELECT id FROM mods WHERE title = ?")?;
let id_iter = stmt.query_map([slug], |row| row.get::<usize, String>(0))?;
for id in id_iter {
mod_id = id?;
}
}
if mod_id.is_empty() {
return Err(MLError::new(ErrorType::DBError, "GI_MOD_NOT_FOUND"));
};
Ok(mod_id)
}
pub struct ModInfo {
pub slug: String,
pub title: String,
}
pub fn mods_get_info(config: &Cfg, id: &str) -> MLE<ModInfo> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
let mut mod_info: Option<ModInfo> = None;
let mut stmt = connection.prepare("SELECT title, slug FROM mods WHERE id = ?")?;
let name_iter = stmt.query_map([id], |row| {
Ok(vec![
row.get::<usize, String>(0)?,
row.get::<usize, String>(1)?,
])
})?;
for info in name_iter {
let i = info?;
mod_info = Some(ModInfo {
title: String::from(&i[0]),
slug: String::from(&i[1]),
});
}
match mod_info.is_none() {
true => Err(MLError::new(ErrorType::DBError, "GN_MOD_NOT_FOUND")),
false => Ok(mod_info.unwrap()),
}
}
pub fn mods_remove(config: Cfg, id: String) -> MLE<()> {
println!("Removing mod {} from database", id);
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
connection.execute("DELETE FROM mods WHERE id = ?", [id])?;
Ok(())
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DBModlistVersions {
pub mod_id: String,
pub versions: String,
}
pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> MLE<Vec<DBModlistVersions>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
if mods.is_empty() {
return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_INPUT"));
}
let mut wherestr = String::from("WHERE");
for (i, id) in mods.iter().enumerate() {
let mut or = " OR";
if i == mods.len() - 1 {
or = ""
};
wherestr = format!("{} id = '{}'{}", wherestr, id, or);
}
let mut versionmaps: Vec<DBModlistVersions> = Vec::new();
let mut stmt = connection
.prepare(format!("SELECT id, versions, title FROM mods {}", wherestr).as_str())?;
let id_iter = stmt.query_map([], |row| {
Ok(vec![
row.get::<usize, String>(0)?,
row.get::<usize, String>(1)?,
row.get::<usize, String>(2)?,
])
})?;
for ver in id_iter {
let version = ver?;
println!(
"\t({}) Get versions from the database",
String::from(&version[2])
);
//println!("Found versions {} for mod {}", version[1], version[0]);
versionmaps.push(DBModlistVersions {
mod_id: String::from(&version[0]),
versions: String::from(&version[1]),
})
}
match versionmaps.is_empty() {
true => Err(MLError::new(ErrorType::DBError, "MODS_MODS_NOT_FOUND")),
false => Ok(versionmaps),
}
}
//userlist
pub fn userlist_insert(
config: Cfg,
list_id: &str,
mod_id: &str,
current_version: &str,
applicable_versions: Vec<String>,
current_link: &str,
set_version: bool,
) -> MLE<()> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
let sv = match set_version {
true => "1",
false => "0",
};
connection.execute(
format!(
"INSERT INTO {} VALUES (?1, ?2, ?3, ?4, 'NONE', ?5)",
list_id
)
.as_str(),
[
mod_id,
current_version,
applicable_versions.join("|").as_str(),
current_link,
sv,
],
)?;
Ok(())
}
pub fn userlist_get_all_ids(config: Cfg, list_id: &str) -> MLE<Vec<String>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data).unwrap();
let mut mod_ids: Vec<String> = Vec::new();
let mut stmt = connection.prepare(format!("SELECT mod_id FROM {}", list_id).as_str())?;
let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
for id in id_iter {
//println!("Found id {:?}", id.as_ref().unwrap());
mod_ids.push(id?)
}
match mod_ids.is_empty() {
true => Err(MLError::new(ErrorType::DBError, "NO_MODS_USERLIST")),
false => Ok(mod_ids),
}
}
pub fn userlist_remove(config: Cfg, list_id: &str, mod_id: &str) -> MLE<()> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
connection.execute(
format!("DELETE FROM {} WHERE mod_id = ?", list_id).as_str(),
[mod_id],
)?;
Ok(())
}
pub fn userlist_get_applicable_versions(
config: Cfg,
list_id: String,
mod_id: String,
) -> MLE<String> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data).unwrap();
let mut version: String = String::new();
let mut stmt = connection.prepare(
format!(
"SELECT applicable_versions FROM {} WHERE mod_id = ?",
list_id
)
.as_str(),
)?;
let ver_iter = stmt.query_map([mod_id], |row| row.get::<usize, String>(0))?;
for ver in ver_iter {
version = ver?;
}
match version.is_empty() {
true => Err(MLError::new(ErrorType::DBError, "GAV_MOD_NOT_FOUND")),
false => Ok(version),
}
}
pub fn userlist_get_all_applicable_versions_with_mods(
config: Cfg,
list_id: String,
) -> MLE<Vec<(String, String)>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
let mut versions: Vec<(String, String)> = Vec::new();
let mut stmt = connection
.prepare(format!("SELECT mod_id, applicable_versions FROM {}", list_id).as_str())?;
let id_iter = stmt.query_map([], |row| {
Ok(vec![
row.get::<usize, String>(0)?,
row.get::<usize, String>(1)?,
])
})?;
for ver in id_iter {
let out = ver?;
versions.push((out[0].to_owned(), out[1].to_owned()));
}
if versions.is_empty() {
return Err(MLError::new(ErrorType::DBError, "NO_MODS_ON_LIST"));
};
Ok(versions)
}
pub fn userlist_get_current_version(config: Cfg, list_id: &str, mod_id: &str) -> MLE<String> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data).unwrap();
let mut version: String = String::new();
let mut stmt = connection
.prepare(format!("SELECT current_version FROM {} WHERE mod_id = ?", list_id).as_str())?;
let ver_iter = stmt.query_map([&mod_id], |row| row.get::<usize, String>(0))?;
for ver in ver_iter {
version = ver?;
}
match version.is_empty() {
true => Err(MLError::new(ErrorType::DBError, "GCV_MOD_NOT_FOUND")),
false => Ok(version),
}
}
pub fn userlist_get_all_current_version_ids(
config: Cfg,
list_id: String,
) -> MLE<Vec<String>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
let mut versions: Vec<String> = Vec::new();
let mut stmt =
connection.prepare(format!("SELECT current_version FROM {}", list_id).as_str())?;
let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
for id in id_iter {
versions.push(id?);
}
if versions.is_empty() {
return Err(MLError::new(
ErrorType::DBError,
"NO_MODS_ON_LIST",
));
};
Ok(versions)
}
pub fn userlist_get_all_current_versions_with_mods(
config: Cfg,
list_id: String,
) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
let mut versions: Vec<(String, String)> = Vec::new();
let mut stmt =
connection.prepare(format!("SELECT mod_id, current_version FROM {}", list_id).as_str())?;
let id_iter = stmt.query_map([], |row| {
Ok(vec![
row.get::<usize, String>(0)?,
row.get::<usize, String>(1)?,
])
})?;
for ver in id_iter {
let out = ver?;
versions.push((out[0].to_owned(), out[1].to_owned()));
}
if versions.is_empty() {
return Err(Box::new(std::io::Error::new(
ErrorKind::Other,
"NO_MODS_ON_LIST",
)));
};
Ok(versions)
}
pub fn userlist_get_set_version(config: Cfg, list_id: &str, mod_id: &str) -> MLE<bool> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data).unwrap();
let mut set_version: bool = false;
let mut stmt = connection
.prepare(format!("SELECT set_version FROM {} WHERE mod_id = ?", list_id).as_str())?;
let ver_iter = stmt.query_map([&mod_id], |row| row.get::<usize, bool>(0))?;
for ver in ver_iter {
set_version = ver?;
}
Ok(set_version)
}
pub fn userlist_change_versions(
config: Cfg,
list_id: String,
current_version: String,
versions: String,
link: String,
mod_id: String,
) -> MLE<()> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
connection.execute(format!("UPDATE {} SET current_version = ?1, applicable_versions = ?2, current_download = ?3 WHERE mod_id = ?4", list_id).as_str(), [current_version, versions, link, mod_id])?;
Ok(())
}
pub fn userlist_add_disabled_versions(
config: Cfg,
list_id: String,
disabled_version: String,
mod_id: String,
) -> MLE<()> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
let currently_disabled_versions =
userlist_get_disabled_versions(config, String::from(&list_id), String::from(&mod_id))?;
let disabled_versions = match currently_disabled_versions == "NONE" {
true => disabled_version,
false => format!("{}|{}", currently_disabled_versions, disabled_version),
};
connection.execute(
format!(
"UPDATE {} SET disabled_versions = ?1 WHERE mod_id = ?2",
list_id
)
.as_str(),
[disabled_versions, mod_id],
)?;
Ok(())
}
pub fn userlist_get_disabled_versions(config: Cfg, list_id: String, mod_id: String) -> MLE<String> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data).unwrap();
let mut version: String = String::new();
let mut stmt = connection
.prepare(format!("SELECT disabled_versions FROM {} WHERE mod_id = ?", list_id).as_str())?;
let ver_iter = stmt.query_map([mod_id], |row| row.get::<usize, String>(0))?;
for ver in ver_iter {
version = ver?;
}
match version.is_empty() {
true => Err(MLError::new(ErrorType::DBError, "GDV_MOD_NOT_FOUND")),
false => Ok(version),
}
}
pub fn userlist_get_all_downloads(
config: Cfg,
list_id: String,
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data).unwrap();
let mut links: Vec<String> = Vec::new();
let mut stmt =
connection.prepare(format!("SELECT current_download FROM {}", list_id).as_str())?;
let link_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
for link in link_iter {
let l = link?;
println!("Found link {}", String::from(&l));
links.push(l)
}
if links.is_empty() {
return Err(Box::new(std::io::Error::new(
ErrorKind::Other,
"NO_MODS_ON_LIST",
)));
};
Ok(links)
}
//lists
///Inserts into lists table and creates new table
pub fn lists_insert(
config: Cfg,
id: String,
mc_version: String,
mod_loader: Modloader,
download_folder: String,
) -> MLE<()> {
println!("Creating list {}", id);
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
connection.execute(
"INSERT INTO lists VALUES (?1, ?2, ?3, ?4)",
[
id.clone(),
mc_version,
mod_loader.to_string(),
download_folder,
],
)?;
connection.execute(format!("CREATE TABLE {}( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT, 'disabled_versions' TEXT DEFAULT 'NONE', 'set_version' INTEGER, CONSTRAINT {}_PK PRIMARY KEY (mod_id) )", id, id).as_str(), [])?;
Ok(())
}
pub fn lists_remove(config: Cfg, id: String) -> MLE<()> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
connection.execute("DELETE FROM lists WHERE id = ?", [&id])?;
connection.execute(format!("DROP TABLE {}", id).as_str(), [])?;
Ok(())
}
pub fn lists_get(config: Cfg, list_id: String) -> MLE<List> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data).unwrap();
let mut list = List {
id: String::new(),
mc_version: String::new(),
modloader: Modloader::Fabric,
download_folder: String::new(),
};
let mut stmt = connection
.prepare("SELECT mc_version, modloader, download_folder FROM lists WHERE id = ?")?;
let list_iter = stmt.query_map([&list_id], |row| {
Ok(vec![
row.get::<usize, String>(0)?,
row.get::<usize, String>(1)?,
row.get::<usize, String>(2)?,
])
})?;
for l in list_iter {
let li = l?;
list = List {
id: String::from(&list_id),
mc_version: String::from(&li[0]),
modloader: Modloader::from(&li[1])?,
download_folder: String::from(&li[2]),
};
}
if list.id.is_empty() {
return Err(MLError::new(ErrorType::DBError, "LIST_NOT_FOUND"));
}
Ok(list)
}
pub fn lists_version(config: Cfg, list_id: &str, version: &str) -> MLE<()> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data).unwrap();
connection.execute(
"UPDATE lists SET mc_version = ? WHERE id = ?",
[version, list_id],
)?;
Ok(())
}
pub fn lists_get_all_ids(config: Cfg) -> MLE<Vec<String>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data).unwrap();
let mut list_ids: Vec<String> = Vec::new();
let mut stmt = connection.prepare("SELECT id FROM lists")?;
let id_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
for id in id_iter {
list_ids.push(id?)
}
match list_ids.is_empty() {
true => Err(MLError::new(ErrorType::DBError, "NO_LISTS")),
false => Ok(list_ids),
}
}
//config
pub fn config_change_current_list(config: Cfg, id: String) -> MLE<()> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
connection.execute(
"UPDATE user_config SET value = ? WHERE id = 'current_list'",
[id],
)?;
Ok(())
}
pub fn config_get_current_list(config: Cfg) -> MLE<String> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data).unwrap();
let mut list_id = String::new();
let mut stmt = connection.prepare("SELECT value FROM user_config WHERE id = 'current_list'")?;
let list_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
for list in list_iter {
list_id = list?;
}
if list_id.is_empty() {
return Err(MLError::new(ErrorType::DBError, "NO_CURRENT_LIST"));
}
Ok(list_id)
}
//SETUP(UPDATES)
pub fn s_userlist_update_download(
config: Cfg,
list_id: String,
mod_id: String,
link: String,
) -> Result<(), Box<dyn std::error::Error>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
connection.execute(
format!(
"UPDATE {} SET current_download = ?1 WHERE mod_id = ?2",
list_id
)
.as_str(),
[link, mod_id],
)?;
Ok(())
}
pub fn s_config_create_version(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
connection.execute(
"INSERT INTO 'user_config' VALUES ( 'db_version', '0.2' )",
(),
)?;
Ok(())
}
pub fn s_config_update_version(config: Cfg, ver: String) -> Result<(), Box<dyn std::error::Error>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
connection.execute(
"UPDATE user_config SET value = ? WHERE id = 'db_version'",
[ver],
)?;
Ok(())
}
pub fn s_config_get_version(config: Cfg) -> Result<String, Box<dyn std::error::Error>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
let mut version: String = String::new();
let mut stmt = connection.prepare("SELECT value FROM user_config WHERE id = 'db_version'")?;
let ver_iter = stmt.query_map([], |row| row.get::<usize, String>(0))?;
for ver in ver_iter {
version = ver?;
}
if version.is_empty() {
return Err(Box::new(std::io::Error::new(
ErrorKind::Other,
"NO_DBVERSION",
)));
};
Ok(version)
}
pub fn s_insert_column(
config: Cfg,
table: String,
column: String,
c_type: String,
default: Option<String>,
) -> Result<(), Box<dyn std::error::Error>> {
let data = format!("{}/data.db", config.data);
let connection = Connection::open(data)?;
let mut sql = format!("ALTER TABLE {} ADD '{}' {}", table, column, c_type);
if default.is_some() {
sql = format!("{} DEFAULT {}", sql, default.unwrap());
}
connection.execute(sql.as_str(), ())?;
Ok(())
}
pub fn db_setup(path: &str) -> MLE<()> {
let connection = Connection::open(path)?;
connection.execute_batch(
"CREATE TABLE 'user_config' ( 'id' TEXT, 'value' TEXT );
CREATE TABLE 'mods' ( 'id' TEXT, 'slug' TEXT, 'title' TEXT, CONSTRAINT mods_PK PRIMARY KEY (id) );
CREATE TABLE 'lists' ( 'id' TEXT, 'mc_version' TEXT, 'modloader' TEXT, 'download_folder' TEXT );
INSERT INTO 'user_config' VALUES ( 'db_version', '0.5' );
INSERT INTO 'user_config' VALUES ( 'current_list', '...' )",
)?;
Ok(())
}
|