Skip to content

Commit

Permalink
rework subcommand detection error
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacherr committed Jul 6, 2024
1 parent 3055ddf commit a701dd6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions assyst-core/src/command/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub struct ArgsExhausted(pub Label);
#[derive(Debug)]
pub enum TagParseError {
ArgsExhausted(ArgsExhausted),
SubcommandArgsExhausted(String),
ParseIntError(ParseIntError),
ParseFloatError(ParseFloatError),
ParseToMillisError(ParseToMillisError),
Expand Down Expand Up @@ -134,6 +135,7 @@ impl Display for TagParseError {
TagParseError::ArgsExhausted(ArgsExhausted(None)) => {
f.write_str("an argument is required but none were found")
},
TagParseError::SubcommandArgsExhausted(_) => f.write_str("no valid subcommand was given"),
TagParseError::ParseIntError(err) => write!(f, "failed to parse an argument as a whole number: {err}"),
TagParseError::ParseFloatError(err) => write!(f, "failed to parse an argument as a decimal number: {err}"),
TagParseError::ParseToMillisError(err) => write!(f, "failed to parse an argument as time: {err}"),
Expand Down
8 changes: 4 additions & 4 deletions assyst-core/src/command/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ macro_rules! define_commandgroup {
#![allow(unreachable_code)]
match crate::command::group::execute_subcommand_raw_message(ctxt.fork(), Self::SUBCOMMANDS).await {
Ok(res) => Ok(res),
Err(crate::command::ExecutionError::Parse(crate::command::errors::TagParseError::InvalidSubcommand(_))
| crate::command::ExecutionError::Parse(crate::command::errors::TagParseError::ArgsExhausted(_))) => {
Err(crate::command::ExecutionError::Parse(crate::command::errors::TagParseError::InvalidSubcommand(s))
| crate::command::ExecutionError::Parse(crate::command::errors::TagParseError::SubcommandArgsExhausted(s))) => {
// No subcommand was found, call either the default if provided, or error out
$(
return [<$default _command>].execute_raw_message(ctxt).await;
)?
return Err(crate::command::ExecutionError::Parse(crate::command::errors::TagParseError::InvalidSubcommand("unknown".to_owned())));
return Err(crate::command::ExecutionError::Parse(crate::command::errors::TagParseError::InvalidSubcommand(s)));
},
Err(err) => Err(err)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ pub async fn execute_subcommand_raw_message(
commands: &[(&str, TCommand)],
) -> Result<(), ExecutionError> {
// todo: come up with better names for this?
let subcommand = ctxt.next_word(None).map_err(|err| ExecutionError::Parse(err.into()))?;
let subcommand = ctxt.next_word(None).map_err(|_| ExecutionError::Parse(TagParseError::SubcommandArgsExhausted("unknown".to_owned())))?;

let command = find_subcommand(subcommand, commands).ok_or(ExecutionError::Parse(
TagParseError::InvalidSubcommand(subcommand.to_owned()),
Expand Down

0 comments on commit a701dd6

Please sign in to comment.