From 896acee5ba000a8b7e2246148deb2aa5df420f82 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Thu, 27 May 2021 15:50:43 +0000 Subject: [PATCH] Avoid cloned command input with From instead of From<&str>. --- src/commands/basic_rolling.rs | 6 +++--- src/commands/cofd.rs | 6 +++--- src/commands/cthulhu.rs | 12 ++++++------ src/commands/management.rs | 24 ++++++++++++------------ src/commands/misc.rs | 6 +++--- src/commands/parser.rs | 3 +-- src/commands/variables.rs | 22 +++++++++++----------- 7 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/commands/basic_rolling.rs b/src/commands/basic_rolling.rs index 163b1a8..ea868bb 100644 --- a/src/commands/basic_rolling.rs +++ b/src/commands/basic_rolling.rs @@ -16,11 +16,11 @@ impl From for Box { } } -impl TryFrom<&str> for RollCommand { +impl TryFrom for RollCommand { type Error = BotError; - fn try_from(input: &str) -> Result { - let result = parse_element_expression(input); + fn try_from(input: String) -> Result { + let result = parse_element_expression(&input); match result { Ok((rest, expression)) if rest.len() == 0 => Ok(RollCommand(expression)), //"Legacy code boundary": translates Nom errors into BotErrors. diff --git a/src/commands/cofd.rs b/src/commands/cofd.rs index 592f064..0f8f794 100644 --- a/src/commands/cofd.rs +++ b/src/commands/cofd.rs @@ -21,11 +21,11 @@ impl From for Box { } } -impl TryFrom<&str> for PoolRollCommand { +impl TryFrom for PoolRollCommand { type Error = BotError; - fn try_from(input: &str) -> Result { - let pool = parse_dice_pool(input)?; + fn try_from(input: String) -> Result { + let pool = parse_dice_pool(&input)?; Ok(PoolRollCommand(pool)) } } diff --git a/src/commands/cthulhu.rs b/src/commands/cthulhu.rs index 9b69153..5600c70 100644 --- a/src/commands/cthulhu.rs +++ b/src/commands/cthulhu.rs @@ -17,11 +17,11 @@ impl From for Box { } } -impl TryFrom<&str> for CthRoll { +impl TryFrom for CthRoll { type Error = BotError; - fn try_from(input: &str) -> Result { - let roll = parse_regular_roll(input)?; + fn try_from(input: String) -> Result { + let roll = parse_regular_roll(&input)?; Ok(CthRoll(roll)) } } @@ -57,11 +57,11 @@ impl From for Box { } } -impl TryFrom<&str> for CthAdvanceRoll { +impl TryFrom for CthAdvanceRoll { type Error = BotError; - fn try_from(input: &str) -> Result { - let roll = parse_advancement_roll(input)?; + fn try_from(input: String) -> Result { + let roll = parse_advancement_roll(&input)?; Ok(CthAdvanceRoll(roll)) } } diff --git a/src/commands/management.rs b/src/commands/management.rs index 2c238bb..976ce76 100644 --- a/src/commands/management.rs +++ b/src/commands/management.rs @@ -15,10 +15,10 @@ impl From for Box { } } -impl TryFrom<&str> for RegisterCommand { +impl TryFrom for RegisterCommand { type Error = BotError; - fn try_from(_: &str) -> Result { + fn try_from(_: String) -> Result { Ok(RegisterCommand) } } @@ -62,11 +62,11 @@ impl From for Box { } } -impl TryFrom<&str> for UnlinkCommand { +impl TryFrom for UnlinkCommand { type Error = BotError; - fn try_from(value: &str) -> Result { - Ok(UnlinkCommand(value.to_owned())) + fn try_from(value: String) -> Result { + Ok(UnlinkCommand(value)) } } @@ -105,11 +105,11 @@ impl From for Box { } } -impl TryFrom<&str> for LinkCommand { +impl TryFrom for LinkCommand { type Error = BotError; - fn try_from(value: &str) -> Result { - Ok(LinkCommand(value.to_owned())) + fn try_from(value: String) -> Result { + Ok(LinkCommand(value)) } } @@ -150,10 +150,10 @@ impl From for Box { } } -impl TryFrom<&str> for CheckCommand { +impl TryFrom for CheckCommand { type Error = BotError; - fn try_from(_: &str) -> Result { + fn try_from(_: String) -> Result { Ok(CheckCommand) } } @@ -195,10 +195,10 @@ impl From for Box { } } -impl TryFrom<&str> for UnregisterCommand { +impl TryFrom for UnregisterCommand { type Error = BotError; - fn try_from(_: &str) -> Result { + fn try_from(_: String) -> Result { Ok(UnregisterCommand) } } diff --git a/src/commands/misc.rs b/src/commands/misc.rs index 51b3a65..709f5f1 100644 --- a/src/commands/misc.rs +++ b/src/commands/misc.rs @@ -13,11 +13,11 @@ impl From for Box { } } -impl TryFrom<&str> for HelpCommand { +impl TryFrom for HelpCommand { type Error = BotError; - fn try_from(input: &str) -> Result { - let topic = parse_help_topic(input); + fn try_from(input: String) -> Result { + let topic = parse_help_topic(&input); Ok(HelpCommand(topic)) } } diff --git a/src/commands/parser.rs b/src/commands/parser.rs index 7ea3a16..879bd0a 100644 --- a/src/commands/parser.rs +++ b/src/commands/parser.rs @@ -64,7 +64,7 @@ fn split_command(input: &str) -> Result<(String, String), CommandParsingError> { /// boilerplate. macro_rules! convert_to { ($type:ident, $input: expr) => { - $type::try_from($input.as_str()).map(Into::into) + $type::try_from($input).map(Into::into) }; } @@ -74,7 +74,6 @@ macro_rules! convert_to { pub fn parse_command(input: &str) -> Result, BotError> { match split_command(input) { Ok((cmd, cmd_input)) => match cmd.to_lowercase().as_ref() { - // "variables" => GetAllVariablesCommand::try_from(input).map(Into::into), "variables" => convert_to!(GetAllVariablesCommand, cmd_input), "get" => convert_to!(GetVariableCommand, cmd_input), "set" => convert_to!(SetVariableCommand, cmd_input), diff --git a/src/commands/variables.rs b/src/commands/variables.rs index 5971e5b..6b1a242 100644 --- a/src/commands/variables.rs +++ b/src/commands/variables.rs @@ -14,10 +14,10 @@ impl From for Box { } } -impl TryFrom<&str> for GetAllVariablesCommand { +impl TryFrom for GetAllVariablesCommand { type Error = BotError; - fn try_from(_: &str) -> Result { + fn try_from(_: String) -> Result { Ok(GetAllVariablesCommand) } } @@ -63,11 +63,11 @@ impl From for Box { } } -impl TryFrom<&str> for GetVariableCommand { +impl TryFrom for GetVariableCommand { type Error = BotError; - fn try_from(input: &str) -> Result { - Ok(GetVariableCommand(input.to_owned())) + fn try_from(input: String) -> Result { + Ok(GetVariableCommand(input)) } } @@ -107,11 +107,11 @@ impl From for Box { } } -impl TryFrom<&str> for SetVariableCommand { +impl TryFrom for SetVariableCommand { type Error = BotError; - fn try_from(input: &str) -> Result { - let (variable_name, value) = crate::parser::variables::parse_set_variable(input)?; + fn try_from(input: String) -> Result { + let (variable_name, value) = crate::parser::variables::parse_set_variable(&input)?; Ok(SetVariableCommand(variable_name, value)) } } @@ -148,11 +148,11 @@ impl From for Box { } } -impl TryFrom<&str> for DeleteVariableCommand { +impl TryFrom for DeleteVariableCommand { type Error = BotError; - fn try_from(input: &str) -> Result { - Ok(DeleteVariableCommand(input.to_owned())) + fn try_from(input: String) -> Result { + Ok(DeleteVariableCommand(input)) } }