summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/commands/io.rs2
-rw-r--r--src/commands/list.rs32
-rw-r--r--src/commands/update.rs8
-rw-r--r--src/db.rs42
-rw-r--r--src/error.rs22
-rw-r--r--src/files.rs29
-rw-r--r--src/input.rs5
-rw-r--r--src/lib.rs9
8 files changed, 98 insertions, 51 deletions
diff --git a/src/commands/io.rs b/src/commands/io.rs
index 39f92d5..6c4a4d3 100644
--- a/src/commands/io.rs
+++ b/src/commands/io.rs
@@ -28,7 +28,7 @@ impl ExportList {
28 28
29 let mods = userlist_get_all_ids(config, list_id)?.join("|"); 29 let mods = userlist_get_all_ids(config, list_id)?.join("|");
30 30
31 Ok(Self { id: list.id, mods, launcher: list.modloader.stringify(), mc_version: list.mc_version, download_folder: dl_folder }) 31 Ok(Self { id: list.id, mods, launcher: list.modloader.to_string(), mc_version: list.mc_version, download_folder: dl_folder })
32 } 32 }
33} 33}
34 34
diff --git a/src/commands/list.rs b/src/commands/list.rs
index 585efe2..526b434 100644
--- a/src/commands/list.rs
+++ b/src/commands/list.rs
@@ -1,6 +1,6 @@
1use std::io::{Error, ErrorKind}; 1use std::io::{Error, ErrorKind};
2 2
3use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, Subcmd}}; 3use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, Subcmd}, cmd_update, error::{MLE, ErrorType, MLError}};
4 4
5#[derive(Debug, Clone, PartialEq, Eq)] 5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct List { 6pub struct List {
@@ -14,7 +14,10 @@ pub async fn list(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::E
14 14
15 match input.subcommand.ok_or("")? { 15 match input.subcommand.ok_or("")? {
16 Subcmd::Add => { 16 Subcmd::Add => {
17 add(config, input.args.ok_or("")?) 17 match add(config, input.args.ok_or("")?) {
18 Ok(..) => Ok(()),
19 Err(e) => Err(Box::new(e))
20 }
18 }, 21 },
19 Subcmd::Change => { 22 Subcmd::Change => {
20 change(config, input.args) 23 change(config, input.args)
@@ -23,7 +26,10 @@ pub async fn list(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::E
23 remove(config, input.args.ok_or("")?) 26 remove(config, input.args.ok_or("")?)
24 }, 27 },
25 Subcmd::Version => { 28 Subcmd::Version => {
26 version(config, input.args.ok_or("NO_VERSION")?) 29 match version(config, input.args.ok_or("NO_VERSION")?).await {
30 Ok(..) => Ok(()),
31 Err(e) => Err(Box::new(e))
32 }
27 } 33 }
28 _ => { 34 _ => {
29 Err(Box::new(Error::new(ErrorKind::InvalidInput, "WRONG_SUBCOMMAND"))) 35 Err(Box::new(Error::new(ErrorKind::InvalidInput, "WRONG_SUBCOMMAND")))
@@ -31,14 +37,14 @@ pub async fn list(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::E
31 } 37 }
32} 38}
33 39
34pub fn get_current_list(config: Cfg) -> Result<List, Box<dyn std::error::Error>> { 40pub fn get_current_list(config: Cfg) -> MLE<List> {
35 let id = config_get_current_list(config.clone())?; 41 let id = config_get_current_list(config.clone())?;
36 lists_get(config, id) 42 lists_get(config, id)
37} 43}
38 44
39fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { 45fn add(config: Cfg, args: Vec<String>) -> MLE<()> {
40 match args.len() { 46 match args.len() {
41 1 | 2 | 3 => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_FEW_ARGUMENTS"))), 47 1 | 2 | 3 => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_FEW_ARGUMENTS")),
42 4 => { 48 4 => {
43 let id = String::from(&args[0]); 49 let id = String::from(&args[0]);
44 let mc_version = String::from(&args[1]); 50 let mc_version = String::from(&args[1]);
@@ -46,7 +52,7 @@ fn add(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>>
46 let download_folder = String::from(&args[3]); 52 let download_folder = String::from(&args[3]);
47 lists_insert(config, id, mc_version, mod_loader, download_folder) 53 lists_insert(config, id, mc_version, mod_loader, download_folder)
48 }, 54 },
49 5.. => Err(Box::new(Error::new(ErrorKind::InvalidInput, "TOO_MANY_ARGUMENTS"))), 55 5.. => Err(MLError::new(ErrorType::ArgumentCountError, "TOO_MANY_ARGUMENTS")),
50 _ => panic!("list arguments should never be zero or lower"), 56 _ => panic!("list arguments should never be zero or lower"),
51 } 57 }
52} 58}
@@ -74,7 +80,15 @@ fn remove(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Erro
74 } 80 }
75} 81}
76 82
77fn version(config: Cfg, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> { 83///Changing the current lists version and updating it
78 lists_version(config.clone(), config_get_current_list(config.clone())?, String::from(&args[0])) 84/// #Arguments
85///
86/// * `config` - The current config
87/// * `args` - All args, to extract the new version
88async fn version(config: Cfg, args: Vec<String>) -> MLE<()> {
89 let current_list = lists_get(config.clone(), config_get_current_list(config.clone())?)?;
90
91 lists_version(config.clone(), String::from(&current_list.id), String::from(&args[0]))?;
79 //update the list & with -- args 92 //update the list & with -- args
93 cmd_update(config, vec![current_list], true, true, false).await
80} 94}
diff --git a/src/commands/update.rs b/src/commands/update.rs
index 11f283e..ca28130 100644
--- a/src/commands/update.rs
+++ b/src/commands/update.rs
@@ -1,8 +1,8 @@
1use std::io::{Error, ErrorKind}; 1use std::io::{Error, ErrorKind};
2 2
3use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, mods_change_versions}, List, input::Input, files::{delete_version, download_versions, disable_version}}; 3use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, mods_change_versions}, List, input::Input, files::{delete_version, download_versions, disable_version}, error::{MLE, MLError, ErrorType}};
4 4
5pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> { 5pub async fn update(config: Cfg, input: Input) -> MLE<()> {
6 6
7 let mut liststack: Vec<List> = vec![]; 7 let mut liststack: Vec<List> = vec![];
8 if input.all_lists { 8 if input.all_lists {
@@ -19,7 +19,7 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box<dyn std::error:
19 cmd_update(config, liststack, input.clean, input.direct_download, input.delete_old).await 19 cmd_update(config, liststack, input.clean, input.direct_download, input.delete_old).await
20} 20}
21 21
22pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_download: bool, delete_old: bool) -> Result<(), Box<dyn std::error::Error>> { 22pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_download: bool, delete_old: bool) -> MLE<()> {
23 for current_list in liststack { 23 for current_list in liststack {
24 let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?; 24 let mods = userlist_get_all_ids(config.clone(), current_list.clone().id)?;
25 25
@@ -37,7 +37,7 @@ pub async fn cmd_update(config: Cfg, liststack: Vec<List>, clean: bool, direct_d
37 let current_version = &versions[index]; 37 let current_version = &versions[index];
38 let p_id = String::from(&project.id); 38 let p_id = String::from(&project.id);
39 let v_id = &current_version.mod_id; 39 let v_id = &current_version.mod_id;
40 if &p_id != v_id { return Err(Box::new(Error::new(ErrorKind::Other, "SORTING_ERROR"))) }; 40 if &p_id != v_id { return Err(MLError::new(ErrorType::Other, "SORTING_ERROR")) };
41 41
42 //Getting current installed version for disable or delete 42 //Getting current installed version for disable or delete
43 let disable_version = userlist_get_current_version(config.clone(), String::from(&current_list.id), String::from(&project.id))?; 43 let disable_version = userlist_get_current_version(config.clone(), String::from(&current_list.id), String::from(&project.id))?;
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}
diff --git a/src/error.rs b/src/error.rs
index 192aa76..dbe7224 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -14,8 +14,12 @@ pub enum ErrorType {
14 ArgumentError, 14 ArgumentError,
15 ArgumentCountError, 15 ArgumentCountError,
16 ConfigError, 16 ConfigError,
17 DBError,
17 LibToml, 18 LibToml,
19 LibSql,
20 LibReq,
18 IoError, 21 IoError,
22 Other,
19} 23}
20 24
21impl std::error::Error for MLError { 25impl std::error::Error for MLError {
@@ -30,18 +34,34 @@ impl fmt::Display for MLError {
30 ErrorType::ArgumentError => write!(f, "Wrong argument"), 34 ErrorType::ArgumentError => write!(f, "Wrong argument"),
31 ErrorType::ArgumentCountError => write!(f, "Too many/too few arguments"), 35 ErrorType::ArgumentCountError => write!(f, "Too many/too few arguments"),
32 ErrorType::ConfigError => write!(f, "CONFIG"), 36 ErrorType::ConfigError => write!(f, "CONFIG"),
37 ErrorType::DBError => write!(f, "DATABASE"),
33 ErrorType::LibToml => write!(f, "TOML"), 38 ErrorType::LibToml => write!(f, "TOML"),
34 ErrorType::IoError => write!(f, "IO") 39 ErrorType::LibSql => write!(f, "SQL"),
40 ErrorType::LibReq => write!(f, "REQWEST"),
41 ErrorType::IoError => write!(f, "IO"),
42 ErrorType::Other => write!(f, "OTHER")
35 } 43 }
36 } 44 }
37} 45}
38 46
47impl From<reqwest::Error> for MLError {
48 fn from(error: reqwest::Error) -> Self {
49 Self { etype: ErrorType::LibReq, message: error.to_string() }
50 }
51}
52
39impl From<toml::de::Error> for MLError { 53impl From<toml::de::Error> for MLError {
40 fn from(error: toml::de::Error) -> Self { 54 fn from(error: toml::de::Error) -> Self {
41 Self { etype: ErrorType::LibToml, message: error.to_string() } 55 Self { etype: ErrorType::LibToml, message: error.to_string() }
42 } 56 }
43} 57}
44 58
59impl From<rusqlite::Error> for MLError {
60 fn from(error: rusqlite::Error) -> Self {
61 Self { etype: ErrorType::LibSql, message: error.to_string() }
62 }
63}
64
45impl From<toml::ser::Error> for MLError { 65impl From<toml::ser::Error> for MLError {
46 fn from(error: toml::ser::Error) -> Self { 66 fn from(error: toml::ser::Error) -> Self {
47 Self { etype: ErrorType::LibToml, message: error.to_string() } 67 Self { etype: ErrorType::LibToml, message: error.to_string() }
diff --git a/src/files.rs b/src/files.rs
index a3f838a..998ed32 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -2,16 +2,19 @@ use std::{fs::{File, read_dir, remove_file, rename}, io::Write, collections::Has
2use futures_util::StreamExt; 2use futures_util::StreamExt;
3use reqwest::Client; 3use reqwest::Client;
4 4
5use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg}; 5use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg, error::{MLE, MLError, ErrorType}};
6 6
7pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { 7pub async fn download_versions(current_list: List, versions: Vec<Version>) -> MLE<String> {
8 8
9 let dl_path = String::from(&current_list.download_folder); 9 let dl_path = String::from(&current_list.download_folder);
10 10
11 for ver in versions { 11 for ver in versions {
12 let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); 12 let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap();
13 let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); 13 let mut splitname: Vec<&str> = primary_file.filename.split('.').collect();
14 let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; 14 let extension = match splitname.pop().ok_or("") {
15 Ok(e) => e,
16 Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")),
17 };
15 let filename = format!("{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension); 18 let filename = format!("{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension);
16 download_file(primary_file.url, current_list.clone().download_folder, filename).await?; 19 download_file(primary_file.url, current_list.clone().download_folder, filename).await?;
17 } 20 }
@@ -19,14 +22,14 @@ pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Re
19 Ok(dl_path) 22 Ok(dl_path)
20} 23}
21 24
22async fn download_file(url: String, path: String, name: String) -> Result<(), Box<dyn std::error::Error>> { 25async fn download_file(url: String, path: String, name: String) -> MLE<()> {
23 println!("Downloading {}", url); 26 println!("Downloading {}", url);
24 let dl_path_file = format!("{}/{}", path, name); 27 let dl_path_file = format!("{}/{}", path, name);
25 let res = Client::new() 28 let res = Client::new()
26 .get(String::from(&url)) 29 .get(String::from(&url))
27 .send() 30 .send()
28 .await?; 31 .await?;
29 32
30 // download chunks 33 // download chunks
31 let mut file = File::create(&dl_path_file)?; 34 let mut file = File::create(&dl_path_file)?;
32 let mut stream = res.bytes_stream(); 35 let mut stream = res.bytes_stream();
@@ -39,7 +42,7 @@ async fn download_file(url: String, path: String, name: String) -> Result<(), Bo
39 Ok(()) 42 Ok(())
40} 43}
41 44
42pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> Result<(), Box<dyn std::error::Error>> { 45pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> MLE<()> {
43 println!("Disabling version {} for mod {}", versionid, mod_id); 46 println!("Disabling version {} for mod {}", versionid, mod_id);
44 let file = get_file_path(current_list.clone(), String::from(&versionid))?; 47 let file = get_file_path(current_list.clone(), String::from(&versionid))?;
45 let disabled = format!("{}.disabled", file); 48 let disabled = format!("{}.disabled", file);
@@ -51,7 +54,7 @@ pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_i
51 Ok(()) 54 Ok(())
52} 55}
53 56
54pub fn delete_version(list: List, version: String) -> Result<(), Box<dyn std::error::Error>> { 57pub fn delete_version(list: List, version: String) -> MLE<()> {
55 let file = get_file_path(list, version)?; 58 let file = get_file_path(list, version)?;
56 59
57 remove_file(file)?; 60 remove_file(file)?;
@@ -59,19 +62,25 @@ pub fn delete_version(list: List, version: String) -> Result<(), Box<dyn std::er
59 Ok(()) 62 Ok(())
60} 63}
61 64
62pub fn get_file_path(list: List, versionid: String) -> Result<String, Box<dyn std::error::Error>> { 65pub fn get_file_path(list: List, versionid: String) -> MLE<String> {
63 let mut names: HashMap<String, String> = HashMap::new(); 66 let mut names: HashMap<String, String> = HashMap::new();
64 for file in read_dir(list.download_folder)? { 67 for file in read_dir(list.download_folder)? {
65 let path = file?.path(); 68 let path = file?.path();
66 if path.is_file() { 69 if path.is_file() {
67 let pathstr = path.to_str().ok_or("BAH")?; 70 let pathstr = match path.to_str().ok_or("") {
71 Ok(s) => s,
72 Err(..) => return Err(MLError::new(ErrorType::Other, "INVALID_PATH"))
73 };
68 let namesplit: Vec<&str> = pathstr.split('.').collect(); 74 let namesplit: Vec<&str> = pathstr.split('.').collect();
69 let ver_id = namesplit[namesplit.len() - 2]; 75 let ver_id = namesplit[namesplit.len() - 2];
70 names.insert(String::from(ver_id), String::from(pathstr)); 76 names.insert(String::from(ver_id), String::from(pathstr));
71 } 77 }
72 }; 78 };
73 79
74 let filename = names.get(&versionid).ok_or("VERSION_NOT_FOUND_IN_FILES")?; 80 let filename = match names.get(&versionid).ok_or("") {
81 Ok(n) => n,
82 Err(..) => return Err(MLError::new(ErrorType::ArgumentError, "VERSION_NOT_FOUND_IN_FILES"))
83 };
75 84
76 Ok(filename.to_owned()) 85 Ok(filename.to_owned())
77} 86}
diff --git a/src/input.rs b/src/input.rs
index d048775..28a0b7b 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -148,7 +148,10 @@ pub async fn get_input(config: Cfg) -> Result<(), Box<dyn std::error::Error>> {
148 list(config, input).await 148 list(config, input).await
149 }, 149 },
150 Cmd::Update => { 150 Cmd::Update => {
151 update(config, input).await 151 match update(config, input).await {
152 Ok(..) => Ok(()),
153 Err(..) => Err(Box::new(MLError::new(ErrorType::Other, "UPDATE_ERR")))
154 }
152 }, 155 },
153 Cmd::Setup => { 156 Cmd::Setup => {
154 setup(config).await 157 setup(config).await
diff --git a/src/lib.rs b/src/lib.rs
index 17ad6b9..eb845d8 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,10 +6,11 @@ pub mod db;
6pub mod error; 6pub mod error;
7pub mod files; 7pub mod files;
8 8
9use std::{io::{Error, ErrorKind}, path::Path}; 9use std::path::Path;
10 10
11pub use apis::*; 11pub use apis::*;
12pub use commands::*; 12pub use commands::*;
13use error::{MLE, ErrorType, MLError};
13 14
14#[derive(Debug, Clone, PartialEq, Eq)] 15#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum Modloader { 16pub enum Modloader {
@@ -18,18 +19,18 @@ pub enum Modloader {
18} 19}
19 20
20impl Modloader { 21impl Modloader {
21 fn stringify(&self) -> String { 22 fn to_string(&self) -> String {
22 match self { 23 match self {
23 Modloader::Fabric => String::from("fabric"), 24 Modloader::Fabric => String::from("fabric"),
24 Modloader::Forge => String::from("forge"), 25 Modloader::Forge => String::from("forge"),
25 } 26 }
26 } 27 }
27 28
28 fn from(string: &str) -> Result<Modloader, Box<Error>> { 29 fn from(string: &str) -> MLE<Modloader> {
29 match string { 30 match string {
30 "forge" => Ok(Modloader::Forge), 31 "forge" => Ok(Modloader::Forge),
31 "fabric" => Ok(Modloader::Fabric), 32 "fabric" => Ok(Modloader::Fabric),
32 _ => Err(Box::new(Error::new(ErrorKind::InvalidData, "UNKNOWN_MODLOADER"))) 33 _ => Err(MLError::new(ErrorType::ArgumentError, "UNKNOWN_MODLOADER"))
33 } 34 }
34 } 35 }
35} 36}