summaryrefslogtreecommitdiff
path: root/src/db.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2023-01-09 23:12:52 +0100
committerfxqnlr <[email protected]>2023-01-09 23:12:52 +0100
commit89193143f90e1b8cbb91445d14942fa39509acbb (patch)
tree4aeb60ae2aceffc2468589c615ead9dc7079a34d /src/db.rs
parent94d7656cce4ca751be545eeb2ff52bdea1f37fa0 (diff)
downloadmodlist-89193143f90e1b8cbb91445d14942fa39509acbb.tar
modlist-89193143f90e1b8cbb91445d14942fa39509acbb.tar.gz
modlist-89193143f90e1b8cbb91445d14942fa39509acbb.zip
implemented more Error (dumb)
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs42
1 files changed, 21 insertions, 21 deletions
diff --git a/src/db.rs b/src/db.rs
index d823018..c5a972f 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -2,7 +2,7 @@ use std::io::{Error, ErrorKind};
2 2
3use rusqlite::Connection; 3use rusqlite::Connection;
4 4
5use crate::{Modloader, config::Cfg, List, devdir}; 5use crate::{Modloader, config::Cfg, List, devdir, error::{MLE, MLError, ErrorType}};
6 6
7//mods 7//mods
8pub fn mods_insert(config: Cfg, id: String, name: String, versions: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { 8pub fn mods_insert(config: Cfg, id: String, name: String, versions: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
@@ -81,7 +81,7 @@ pub fn mods_get_name(config: Cfg, id: String) -> Result<String, Box<dyn std::err
81 } 81 }
82} 82}
83 83
84pub fn mods_change_versions(config: Cfg, versions: String, mod_id: String) -> Result<(), Box<dyn std::error::Error>> { 84pub fn mods_change_versions(config: Cfg, versions: String, mod_id: String) -> MLE<()> {
85 85
86 println!("Updating versions for {} with \n {}", mod_id, versions); 86 println!("Updating versions for {} with \n {}", mod_id, versions);
87 87
@@ -110,11 +110,11 @@ pub struct DBModlistVersions {
110 pub versions: String, 110 pub versions: String,
111} 111}
112 112
113pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> Result<Vec<DBModlistVersions>, Box<dyn std::error::Error>> { 113pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> MLE<Vec<DBModlistVersions>> {
114 let data = devdir(format!("{}/data.db", config.data).as_str()); 114 let data = devdir(format!("{}/data.db", config.data).as_str());
115 let connection = Connection::open(data)?; 115 let connection = Connection::open(data)?;
116 116
117 if mods.is_empty() { return Err(Box::new(Error::new(ErrorKind::Other, "MODS_NO_INPUT"))); } 117 if mods.is_empty() { return Err(MLError::new(ErrorType::ArgumentError, "MODS_NO_INPUT")); }
118 118
119 let mut wherestr = String::from("WHERE"); 119 let mut wherestr = String::from("WHERE");
120 for (i, id) in mods.iter().enumerate() { 120 for (i, id) in mods.iter().enumerate() {
@@ -137,7 +137,7 @@ pub fn mods_get_versions(config: Cfg, mods: Vec<String>) -> Result<Vec<DBModlist
137 }; 137 };
138 138
139 match versionmaps.is_empty() { 139 match versionmaps.is_empty() {
140 true => Err(Box::new(Error::new(ErrorKind::NotFound, "MODS_MODS_NOT_FOUND"))), 140 true => Err(MLError::new(ErrorType::DBError, "MODS_MODS_NOT_FOUND")),
141 false => Ok(versionmaps), 141 false => Ok(versionmaps),
142 } 142 }
143} 143}
@@ -155,7 +155,7 @@ pub fn userlist_insert(config: Cfg, list_id: String, mod_id: String, current_ver
155 Ok(()) 155 Ok(())
156} 156}
157 157
158pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> Result<Vec<String>, Box<dyn std::error::Error>> { 158pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> MLE<Vec<String>> {
159 let data = devdir(format!("{}/data.db", config.data).as_str()); 159 let data = devdir(format!("{}/data.db", config.data).as_str());
160 let connection = Connection::open(data).unwrap(); 160 let connection = Connection::open(data).unwrap();
161 161
@@ -171,7 +171,7 @@ pub fn userlist_get_all_ids(config: Cfg, list_id: String) -> Result<Vec<String>,
171 }; 171 };
172 172
173 match mod_ids.is_empty() { 173 match mod_ids.is_empty() {
174 true => Err(Box::new(std::io::Error::new(ErrorKind::NotFound, "NO_MODS"))), 174 true => Err(MLError::new(ErrorType::DBError, "NO_MODS")),
175 false => Ok(mod_ids), 175 false => Ok(mod_ids),
176 } 176 }
177} 177}
@@ -226,7 +226,7 @@ pub fn userlist_get_all_applicable_versions_with_mods(config: Cfg, list_id: Stri
226 Ok(versions) 226 Ok(versions)
227} 227}
228 228
229pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String) -> Result<String, Box<dyn std::error::Error>> { 229pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String) -> MLE<String> {
230 let data = devdir(format!("{}/data.db", config.data).as_str()); 230 let data = devdir(format!("{}/data.db", config.data).as_str());
231 let connection = Connection::open(data).unwrap(); 231 let connection = Connection::open(data).unwrap();
232 232
@@ -241,7 +241,7 @@ pub fn userlist_get_current_version(config: Cfg, list_id: String, mod_id: String
241 }; 241 };
242 242
243 match version.is_empty() { 243 match version.is_empty() {
244 true => Err(Box::new(Error::new(ErrorKind::NotFound, "MOD_NOT_FOUND"))), 244 true => Err(MLError::new(ErrorType::DBError, "MOD_NOT_FOUND")),
245 false => Ok(version), 245 false => Ok(version),
246 } 246 }
247} 247}
@@ -293,7 +293,7 @@ pub fn userlist_change_versions(config: Cfg, list_id: String, current_version: S
293 Ok(()) 293 Ok(())
294} 294}
295 295
296pub fn userlist_add_disabled_versions(config: Cfg, list_id: String, disabled_version: String, mod_id: String) -> Result<(), Box<dyn std::error::Error>> { 296pub fn userlist_add_disabled_versions(config: Cfg, list_id: String, disabled_version: String, mod_id: String) -> MLE<()> {
297 let data = devdir(format!("{}/data.db", config.data).as_str()); 297 let data = devdir(format!("{}/data.db", config.data).as_str());
298 let connection = Connection::open(data)?; 298 let connection = Connection::open(data)?;
299 299
@@ -307,7 +307,7 @@ pub fn userlist_add_disabled_versions(config: Cfg, list_id: String, disabled_ver
307 Ok(()) 307 Ok(())
308} 308}
309 309
310pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: String) -> Result<String, Box<dyn std::error::Error>> { 310pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: String) -> MLE<String> {
311 let data = devdir(format!("{}/data.db", config.data).as_str()); 311 let data = devdir(format!("{}/data.db", config.data).as_str());
312 let connection = Connection::open(data).unwrap(); 312 let connection = Connection::open(data).unwrap();
313 313
@@ -322,7 +322,7 @@ pub fn userlist_get_disabled_versions(config:Cfg, list_id: String, mod_id: Strin
322 }; 322 };
323 323
324 match version.is_empty() { 324 match version.is_empty() {
325 true => Err(Box::new(Error::new(ErrorKind::NotFound, "MOD_NOT_FOUND"))), 325 true => Err(MLError::new(ErrorType::DBError, "MOD_NOT_FOUND")),
326 false => Ok(version), 326 false => Ok(version),
327 } 327 }
328} 328}
@@ -349,13 +349,13 @@ pub fn userlist_get_all_downloads(config: Cfg, list_id: String) -> Result<Vec<St
349} 349}
350 350
351//lists 351//lists
352pub fn lists_insert(config: Cfg, id: String, mc_version: String, mod_loader: Modloader, download_folder: String) -> Result<(), Box<dyn std::error::Error>> { 352pub fn lists_insert(config: Cfg, id: String, mc_version: String, mod_loader: Modloader, download_folder: String) -> MLE<()> {
353 println!("Creating list {}", id); 353 println!("Creating list {}", id);
354 354
355 let data = devdir(format!("{}/data.db", config.data).as_str()); 355 let data = devdir(format!("{}/data.db", config.data).as_str());
356 let connection = Connection::open(data)?; 356 let connection = Connection::open(data)?;
357 357
358 connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.stringify(), download_folder])?; 358 connection.execute("INSERT INTO lists VALUES (?1, ?2, ?3, ?4)", [id.clone(), mc_version, mod_loader.to_string(), download_folder])?;
359 connection.execute(format!("CREATE TABLE {}( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT, 'disabled_versions' TEXT DEFAULT 'NONE' )", id).as_str(), [])?; 359 connection.execute(format!("CREATE TABLE {}( 'mod_id' TEXT, 'current_version' TEXT, 'applicable_versions' BLOB, 'current_download' TEXT, 'disabled_versions' TEXT DEFAULT 'NONE' )", id).as_str(), [])?;
360 360
361 Ok(()) 361 Ok(())
@@ -370,7 +370,7 @@ pub fn lists_remove(config: Cfg, id: String) -> Result<(), Box<dyn std::error::E
370 Ok(()) 370 Ok(())
371} 371}
372 372
373pub fn lists_get(config: Cfg, list_id: String) -> Result<List, Box<dyn std::error::Error>> { 373pub fn lists_get(config: Cfg, list_id: String) -> MLE<List> {
374 let data = devdir(format!("{}/data.db", config.data).as_str()); 374 let data = devdir(format!("{}/data.db", config.data).as_str());
375 let connection = Connection::open(data).unwrap(); 375 let connection = Connection::open(data).unwrap();
376 376
@@ -386,12 +386,12 @@ pub fn lists_get(config: Cfg, list_id: String) -> Result<List, Box<dyn std::erro
386 list = List { id: String::from(&list_id), mc_version: String::from(&li[0]), modloader: Modloader::from(&li[1])?, download_folder: String::from(&li[2]) }; 386 list = List { id: String::from(&list_id), mc_version: String::from(&li[0]), modloader: Modloader::from(&li[1])?, download_folder: String::from(&li[2]) };
387 }; 387 };
388 388
389 if list.id.is_empty() { return Err(Box::new(Error::new(ErrorKind::Other, "LIST_NOT_FOUND"))); } 389 if list.id.is_empty() { return Err(MLError::new(ErrorType::DBError, "LIST_NOT_FOUND")); }
390 390
391 Ok(list) 391 Ok(list)
392} 392}
393 393
394pub fn lists_version(config: Cfg, list_id: String, version: String) -> Result<(), Box<dyn std::error::Error>> { 394pub fn lists_version(config: Cfg, list_id: String, version: String) -> MLE<()> {
395 let data = devdir(format!("{}/data.db", config.data).as_str()); 395 let data = devdir(format!("{}/data.db", config.data).as_str());
396 let connection = Connection::open(data).unwrap(); 396 let connection = Connection::open(data).unwrap();
397 397
@@ -399,7 +399,7 @@ pub fn lists_version(config: Cfg, list_id: String, version: String) -> Result<()
399 Ok(()) 399 Ok(())
400} 400}
401 401
402pub fn lists_get_all_ids(config: Cfg) -> Result<Vec<String>, Box<dyn std::error::Error>> { 402pub fn lists_get_all_ids(config: Cfg) -> MLE<Vec<String>> {
403 let data = devdir(format!("{}/data.db", config.data).as_str()); 403 let data = devdir(format!("{}/data.db", config.data).as_str());
404 let connection = Connection::open(data).unwrap(); 404 let connection = Connection::open(data).unwrap();
405 405
@@ -414,7 +414,7 @@ pub fn lists_get_all_ids(config: Cfg) -> Result<Vec<String>, Box<dyn std::error:
414 }; 414 };
415 415
416 match list_ids.is_empty() { 416 match list_ids.is_empty() {
417 true => Err(Box::new(std::io::Error::new(ErrorKind::NotFound, "NO_LISTS"))), 417 true => Err(MLError::new(ErrorType::DBError, "NO_LISTS")),
418 false => Ok(list_ids), 418 false => Ok(list_ids),
419 } 419 }
420} 420}
@@ -428,7 +428,7 @@ pub fn config_change_current_list(config: Cfg, id: String) -> Result<(), Box<dyn
428 Ok(()) 428 Ok(())
429} 429}
430 430
431pub fn config_get_current_list(config: Cfg) -> Result<String, Box<dyn std::error::Error>> { 431pub fn config_get_current_list(config: Cfg) -> MLE<String> {
432 let data = devdir(format!("{}/data.db", config.data).as_str()); 432 let data = devdir(format!("{}/data.db", config.data).as_str());
433 let connection = Connection::open(data).unwrap(); 433 let connection = Connection::open(data).unwrap();
434 434
@@ -442,7 +442,7 @@ pub fn config_get_current_list(config: Cfg) -> Result<String, Box<dyn std::error
442 list_id = list?; 442 list_id = list?;
443 }; 443 };
444 444
445 if list_id.is_empty() { return Err(Box::new(Error::new(ErrorKind::Other, "NO_CURRENT_LIST"))); } 445 if list_id.is_empty() { return Err(MLError::new(ErrorType::DBError, "NO_CURRENT_LIST")); }
446 446
447 Ok(list_id) 447 Ok(list_id)
448} 448}