removed unwraps and added a method to Results called .unwrap_or_exit(error_message, error_code)

This tries to unwrap and if it can't then it outputs the error message with the error!() macro
(log library) and exits with the error code.  This is to be used instead of expect and is for
fatal errors
This commit is contained in:
2026-05-15 19:19:17 +01:00
parent a5a45fa1f0
commit 20174e697c
9 changed files with 69 additions and 32 deletions
+15 -7
View File
@@ -12,6 +12,7 @@ use crate::
info,
debug,
warn,
UnwrapOrExit,
};
mod strings;
@@ -93,10 +94,10 @@ pub fn token_parse(
"or" =>
{
info!("OR command, jumping over");
index += match strings::closing_char(&tokens[index..], '{','}') // TODO eh
index += match strings::closing_char(&tokens[index..], '{','}')
{
Ok(index) => index,
Err(()) => return Err(String::from("Unable to find closing brace to OR command")),
Some(index) => index,
None => return Err(String::from("No closing brace")),
};
continue
},
@@ -130,12 +131,16 @@ fn choice_parse
let mut choices: Vec<String> = Vec::new();
let mut choice_indeces: Vec<usize> = Vec::new();
// Ensure the index is valid (the index is not beyond the vector)
// Get the initial choice
let (choice_string, counter) = strings::extract_quoted(&tokens[sum_index..])
.ok_or_else(|| "No choice string".to_string())?;
sum_index += counter;
choices.push(choice_string);
choice_indeces.push(sum_index+1);
sum_index += strings::closing_char(&tokens[sum_index..], '{','}').unwrap() + 1; //TODO eh
sum_index += strings::closing_char(&tokens[sum_index..], '{','}')
.ok_or_else(|| "No closing brace".to_string())? + 1;
// Find all the alternate choices labelled with OR
// Fill out the choices vector with all the choice strings
while tokens[sum_index].to_lowercase() == "or"
{
let (choice_string, counter) = strings::extract_quoted(&tokens[sum_index+1..])
@@ -143,14 +148,17 @@ fn choice_parse
sum_index += counter;
choices.push(choice_string);
choice_indeces.push(sum_index+2);
sum_index += strings::closing_char(&tokens[sum_index..], '{','}').unwrap() + 1; //TODO eh
sum_index += strings::closing_char(&tokens[sum_index..], '{','}')
.ok_or_else(|| "No closing brace".to_string())? + 1;
}
debug!("{choices:?}");
let mut data = data_to_send.lock().unwrap();
// Send the choices to the Client via the API
let mut data = data_to_send.lock().unwrap_or_exit("Data to send Mutex was poisoned",2);
data.action_type = String::from("choice");
data.content = String::new();
data.character = String::new();
data.choices = choices; //TODO
data.choices = choices;
drop(data);
// Return the choice indeces
Ok((sum_index + 1, choice_indeces))
}