From 5192314c42ad6e9b692c4a6bd8f2ca0d99169646 Mon Sep 17 00:00:00 2001 From: darcy Date: Thu, 19 Dec 2024 22:08:39 +1100 Subject: [PATCH] refactor: use static strings for command name in error --- src/debugger/command.rs | 57 +++++++++++++++++++++-------------------- src/debugger/parse.rs | 3 ++- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/debugger/command.rs b/src/debugger/command.rs index 3cd04a8..81f3061 100644 --- a/src/debugger/command.rs +++ b/src/debugger/command.rs @@ -104,10 +104,10 @@ pub enum Error { name: String, }, MissingSubcommand { - name: String, + name: &'static str, }, InvalidSubcommand { - name: String, + name: &'static str, subname: String, }, InvalidArgument { @@ -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) + } } } } diff --git a/src/debugger/parse.rs b/src/debugger/parse.rs index a4418ce..37621af 100644 --- a/src/debugger/parse.rs +++ b/src/debugger/parse.rs @@ -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 }); };