From fc7e6df283d4a7a0f18c1b114387da7b62a9860c Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Fri, 1 Dec 2023 19:21:13 -0500 Subject: [PATCH] Add better error handling for dataset rename command --- src/database.rs | 4 ++++ src/main.rs | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/database.rs b/src/database.rs index 7b44e34..9241fa2 100644 --- a/src/database.rs +++ b/src/database.rs @@ -132,6 +132,10 @@ pub fn delete_dataset(connection: &Connection, dataset: &str) -> sqlite::Result< } pub fn rename_dataset(connection: &Connection, old: &str, new: &str) -> sqlite::Result<()> { + if old == new { + return Ok(()); + } + let query = format!( r#"UPDATE datasets SET name = '{1}' WHERE name = '{0}'; ALTER TABLE "{0}_players" RENAME TO "{1}_players"; diff --git a/src/main.rs b/src/main.rs index 8174e1f..efc3b08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -472,7 +472,19 @@ fn dataset_rename(old: Option, new: Option) { let connection = open_datasets(&config_dir).unwrap_or_else(|_| error("Could not open datasets file", 2)); - rename_dataset(&connection, &old, &new).unwrap(); + + match rename_dataset(&connection, &old, &new) { + Ok(()) => (), + Err(sqlite::Error { + code: Some(1), + message: _, + }) => error(&format!("Dataset {:?} does not exist", &old), 1), + Err(sqlite::Error { + code: Some(19), + message: _, + }) => error(&format!("Dataset {:?} already exists", &new), 1), + Err(_) => error("Unknown error occurred", 2), + }; } // Players