Skip to content

Commit

Permalink
refactor: use static strings for command name in error
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy committed Dec 19, 2024
1 parent 74176cf commit 5192314
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
57 changes: 29 additions & 28 deletions src/debugger/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ pub enum Error {
name: String,
},
MissingSubcommand {
name: String,
name: &'static str,
},
InvalidSubcommand {
name: String,
name: &'static str,
subname: String,
},
InvalidArgument {
Expand Down Expand Up @@ -141,33 +141,34 @@ impl fmt::Display for Error {
Self::MissingSubcommand { name } => {
write!(f, "Missing subcommand for `{}`.", name)
}
Self::InvalidArgument { name, error } => match error {
ArgumentError::MissingArgument { argument } => {
write!(f, "Missing argument `{}` for command `{}`.", argument, name)
}
ArgumentError::TooManyArguments {} => {
write!(f, "Too many arguments for command `{}`.", name)
}
ArgumentError::WrongArgumentType { argument } => {
write!(
f,
"Invalid type for argument `{}` for command `{}`.",
argument, name
)
}
ArgumentError::MalformedArgument {} => {
write!(f, "Malformed argument for command `{}`.", name)
}
ArgumentError::MalformedInteger {} => {
write!(f, "Malformed integer argument for command `{}`.", name)
}
ArgumentError::MalformedLabel {} => {
write!(f, "Malformed label argument for command `{}`.", name)
}
ArgumentError::IntegerTooLarge {} => {
write!(f, "Integer argument too large for command `{}`.", name)

Self::InvalidArgument { name, error } => {
match error {
ArgumentError::MissingArgument { argument } => {
write!(f, "Missing argument `{}`", argument)?;
}
ArgumentError::TooManyArguments {} => {
write!(f, "Too many arguments")?;
}
ArgumentError::WrongArgumentType { argument } => {
write!(f, "Invalid type for argument `{}`", argument)?;
}
ArgumentError::MalformedArgument {} => {
write!(f, "Malformed argument")?;
}
ArgumentError::MalformedInteger {} => {
write!(f, "Malformed integer argument")?;
}
ArgumentError::MalformedLabel {} => {
write!(f, "Malformed label argument")?;
}
ArgumentError::IntegerTooLarge {} => {
write!(f, "Integer argument too large")?;
}
}
},

write!(f, " for command `{}`", name)
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/debugger/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ impl<'a> CommandIter<'a> {

// This could be written a bit nicer. But it doesn't seem necessary.
if matches(name, &["break", "b"]) {
let name = name.to_string();
let name = "break";

let Some(subname) = self.next_command_name_part() else {
return Err(Error::MissingSubcommand { name });
};
Expand Down

0 comments on commit 5192314

Please sign in to comment.