improve error handling and add metadata

This commit is contained in:
Chance 2025-04-03 01:00:24 -04:00 committed by BitSyndicate
parent 2d4736f12e
commit 42f9c669c8
17 changed files with 1162 additions and 787 deletions

View file

@ -1,11 +1,12 @@
use std::collections::HashMap;
use colored::Colorize;
use lazy_static::lazy_static;
use parking_lot::RwLock;
lazy_static! {
pub static ref COMMAND_MANAGER: RwLock<CommandManager> = RwLock::new(CommandManager::init());
}
use std::sync::LazyLock;
use crate::error::{ZenyxError, ZenyxErrorKind};
pub static COMMAND_MANAGER: LazyLock<RwLock<CommandManager>> = LazyLock::new(|| { RwLock::new(CommandManager::init()) });
#[macro_export]
macro_rules! commands {
[$($command:ty),*] => [
@ -110,24 +111,28 @@ impl CommandManager {
&self,
command: &str,
args: Option<Vec<String>>,
) -> Result<(), anyhow::Error> {
) -> Result<(), ZenyxError> {
if let Some(command) = self.commands.get(command) {
command.execute(args)?;
Ok(())
} else {
let corrected_cmd = check_similarity(command);
if corrected_cmd.is_some() {
println!("Command: {} was not found. Did you mean {}?",command.red().bold(),corrected_cmd
.expect("A command was editied during execution, something has gone seriously wrong").green().bold().italic());
if let Some(corrected_cmd) = corrected_cmd {
println!(
"Command: {} was not found. Did you mean {}?",
command.red().bold(),
corrected_cmd.green().bold().italic()
);
}
Err(anyhow::anyhow!("Command '{}' not found.", command))
Err(ZenyxError::builder(ZenyxErrorKind::CommandExecution)
.with_message(format!("Command '{}' not found.", command))
.build())
}
}
pub fn execute(&self, command: &str, args: Option<Vec<String>>) -> Result<(), anyhow::Error> {
pub fn execute(&self, command: &str, args: Option<Vec<String>>) -> Result<(), ZenyxError> {
match self.aliases.get(command) {
Some(command) => self.execute(command, args),
// check to see if we are using an alias or the command just doesnt exist
None => {
self.execute_command(command, args)?;
Ok(())
@ -149,7 +154,7 @@ impl CommandManager {
}
pub trait Command: Send + Sync {
fn execute(&self, args: Option<Vec<String>>) -> Result<(), anyhow::Error>;
fn execute(&self, args: Option<Vec<String>>) -> Result<(), ZenyxError>;
fn undo(&self);
fn redo(&self);
fn get_description(&self) -> String;