improve error handling and add metadata

This commit is contained in:
Chance 2025-04-03 01:00:24 -04:00
parent 13893e96a9
commit 43fd5966b7
17 changed files with 1162 additions and 787 deletions

View file

@ -15,6 +15,7 @@ use rustyline::{
use tracing::{debug, error, info, warn};
use super::handler::COMMAND_MANAGER;
use crate::error::{Result, ZenyxError, ZenyxErrorKind};
struct CommandCompleter;
impl CommandCompleter {
@ -129,29 +130,38 @@ pub fn tokenize(command: &str) -> Vec<String> {
tokens
}
pub fn parse_command(input: &str) -> anyhow::Result<Vec<String>> {
let pattern = Regex::new(r"[;|\n]").unwrap();
pub fn parse_command(input: &str) -> Result<Vec<String>> {
let pattern = Regex::new(r"[;|\n]").map_err(|_| {
ZenyxError::builder(ZenyxErrorKind::CommandParsing)
.with_message("Failed to compile regex pattern")
.build()
})?;
let commands: Vec<String> = pattern.split(input).map(String::from).collect();
Ok(commands)
}
pub fn evaluate_command(input: &str) -> anyhow::Result<()> {
pub fn evaluate_command(input: &str) -> Result<()> {
if input.trim().is_empty() {
return Ok(());
}
let pattern = Regex::new(r"[;|\n]").unwrap();
let pattern = Regex::new(r"[;|\n]").map_err(|_| {
ZenyxError::builder(ZenyxErrorKind::CommandParsing)
.with_message("Failed to compile regex pattern")
.build()
})?;
let commands: Vec<&str> = pattern.split(input).collect();
for command in commands {
let command = command.trim();
if command.is_empty() {
println!("Empty command, skipping.");
error!("Empty command, skipping.");
continue;
}
let tokens = tokenize(command);
if tokens.is_empty() {
println!("Empty command, skipping.");
error!("Empty command, skipping.");
continue;
}
let cmd_name = &tokens[0];
@ -160,15 +170,20 @@ pub fn evaluate_command(input: &str) -> anyhow::Result<()> {
} else {
None
};
match COMMAND_MANAGER.read().execute(cmd_name, args) {
Ok(_) => continue,
Err(e) => return Err(e),
}
COMMAND_MANAGER
.read()
.execute(cmd_name, args)
.map_err(|e| {
ZenyxError::builder(ZenyxErrorKind::CommandExecution)
.with_message(format!("Failed to execute command: {cmd_name}"))
.with_context(format!("{e}"))
.build()
})?;
}
Ok(())
}
pub async fn handle_repl() -> anyhow::Result<()> {
pub async fn handle_repl() -> Result<()> {
let mut rl = Editor::<MyHelper, DefaultHistory>::new()?;
rl.set_helper(Some(MyHelper {
hinter: HistoryHinter::new(),
@ -196,7 +211,7 @@ pub async fn handle_repl() -> anyhow::Result<()> {
rl.add_history_entry(line.as_str())?;
match evaluate_command(line.as_str()) {
Ok(_) => continue,
Err(e) => println!("{e}"),
Err(e) => error!("{e}"),
}
}
Err(ReadlineError::Interrupted) => {