Avoid cloned command input with From<String> instead of From<&str>.

This commit is contained in:
projectmoon 2021-05-27 15:50:43 +00:00
parent d70df44d2a
commit 896acee5ba
7 changed files with 39 additions and 40 deletions

View File

@ -16,11 +16,11 @@ impl From<RollCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for RollCommand { impl TryFrom<String> for RollCommand {
type Error = BotError; type Error = BotError;
fn try_from(input: &str) -> Result<Self, Self::Error> { fn try_from(input: String) -> Result<Self, Self::Error> {
let result = parse_element_expression(input); let result = parse_element_expression(&input);
match result { match result {
Ok((rest, expression)) if rest.len() == 0 => Ok(RollCommand(expression)), Ok((rest, expression)) if rest.len() == 0 => Ok(RollCommand(expression)),
//"Legacy code boundary": translates Nom errors into BotErrors. //"Legacy code boundary": translates Nom errors into BotErrors.

View File

@ -21,11 +21,11 @@ impl From<PoolRollCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for PoolRollCommand { impl TryFrom<String> for PoolRollCommand {
type Error = BotError; type Error = BotError;
fn try_from(input: &str) -> Result<Self, Self::Error> { fn try_from(input: String) -> Result<Self, Self::Error> {
let pool = parse_dice_pool(input)?; let pool = parse_dice_pool(&input)?;
Ok(PoolRollCommand(pool)) Ok(PoolRollCommand(pool))
} }
} }

View File

@ -17,11 +17,11 @@ impl From<CthRoll> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for CthRoll { impl TryFrom<String> for CthRoll {
type Error = BotError; type Error = BotError;
fn try_from(input: &str) -> Result<Self, Self::Error> { fn try_from(input: String) -> Result<Self, Self::Error> {
let roll = parse_regular_roll(input)?; let roll = parse_regular_roll(&input)?;
Ok(CthRoll(roll)) Ok(CthRoll(roll))
} }
} }
@ -57,11 +57,11 @@ impl From<CthAdvanceRoll> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for CthAdvanceRoll { impl TryFrom<String> for CthAdvanceRoll {
type Error = BotError; type Error = BotError;
fn try_from(input: &str) -> Result<Self, Self::Error> { fn try_from(input: String) -> Result<Self, Self::Error> {
let roll = parse_advancement_roll(input)?; let roll = parse_advancement_roll(&input)?;
Ok(CthAdvanceRoll(roll)) Ok(CthAdvanceRoll(roll))
} }
} }

View File

@ -15,10 +15,10 @@ impl From<RegisterCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for RegisterCommand { impl TryFrom<String> for RegisterCommand {
type Error = BotError; type Error = BotError;
fn try_from(_: &str) -> Result<Self, Self::Error> { fn try_from(_: String) -> Result<Self, Self::Error> {
Ok(RegisterCommand) Ok(RegisterCommand)
} }
} }
@ -62,11 +62,11 @@ impl From<UnlinkCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for UnlinkCommand { impl TryFrom<String> for UnlinkCommand {
type Error = BotError; type Error = BotError;
fn try_from(value: &str) -> Result<Self, Self::Error> { fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(UnlinkCommand(value.to_owned())) Ok(UnlinkCommand(value))
} }
} }
@ -105,11 +105,11 @@ impl From<LinkCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for LinkCommand { impl TryFrom<String> for LinkCommand {
type Error = BotError; type Error = BotError;
fn try_from(value: &str) -> Result<Self, Self::Error> { fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(LinkCommand(value.to_owned())) Ok(LinkCommand(value))
} }
} }
@ -150,10 +150,10 @@ impl From<CheckCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for CheckCommand { impl TryFrom<String> for CheckCommand {
type Error = BotError; type Error = BotError;
fn try_from(_: &str) -> Result<Self, Self::Error> { fn try_from(_: String) -> Result<Self, Self::Error> {
Ok(CheckCommand) Ok(CheckCommand)
} }
} }
@ -195,10 +195,10 @@ impl From<UnregisterCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for UnregisterCommand { impl TryFrom<String> for UnregisterCommand {
type Error = BotError; type Error = BotError;
fn try_from(_: &str) -> Result<Self, Self::Error> { fn try_from(_: String) -> Result<Self, Self::Error> {
Ok(UnregisterCommand) Ok(UnregisterCommand)
} }
} }

View File

@ -13,11 +13,11 @@ impl From<HelpCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for HelpCommand { impl TryFrom<String> for HelpCommand {
type Error = BotError; type Error = BotError;
fn try_from(input: &str) -> Result<Self, Self::Error> { fn try_from(input: String) -> Result<Self, Self::Error> {
let topic = parse_help_topic(input); let topic = parse_help_topic(&input);
Ok(HelpCommand(topic)) Ok(HelpCommand(topic))
} }
} }

View File

@ -64,7 +64,7 @@ fn split_command(input: &str) -> Result<(String, String), CommandParsingError> {
/// boilerplate. /// boilerplate.
macro_rules! convert_to { macro_rules! convert_to {
($type:ident, $input: expr) => { ($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<Box<dyn Command>, BotError> { pub fn parse_command(input: &str) -> Result<Box<dyn Command>, BotError> {
match split_command(input) { match split_command(input) {
Ok((cmd, cmd_input)) => match cmd.to_lowercase().as_ref() { Ok((cmd, cmd_input)) => match cmd.to_lowercase().as_ref() {
// "variables" => GetAllVariablesCommand::try_from(input).map(Into::into),
"variables" => convert_to!(GetAllVariablesCommand, cmd_input), "variables" => convert_to!(GetAllVariablesCommand, cmd_input),
"get" => convert_to!(GetVariableCommand, cmd_input), "get" => convert_to!(GetVariableCommand, cmd_input),
"set" => convert_to!(SetVariableCommand, cmd_input), "set" => convert_to!(SetVariableCommand, cmd_input),

View File

@ -14,10 +14,10 @@ impl From<GetAllVariablesCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for GetAllVariablesCommand { impl TryFrom<String> for GetAllVariablesCommand {
type Error = BotError; type Error = BotError;
fn try_from(_: &str) -> Result<Self, Self::Error> { fn try_from(_: String) -> Result<Self, Self::Error> {
Ok(GetAllVariablesCommand) Ok(GetAllVariablesCommand)
} }
} }
@ -63,11 +63,11 @@ impl From<GetVariableCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for GetVariableCommand { impl TryFrom<String> for GetVariableCommand {
type Error = BotError; type Error = BotError;
fn try_from(input: &str) -> Result<Self, Self::Error> { fn try_from(input: String) -> Result<Self, Self::Error> {
Ok(GetVariableCommand(input.to_owned())) Ok(GetVariableCommand(input))
} }
} }
@ -107,11 +107,11 @@ impl From<SetVariableCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for SetVariableCommand { impl TryFrom<String> for SetVariableCommand {
type Error = BotError; type Error = BotError;
fn try_from(input: &str) -> Result<Self, Self::Error> { fn try_from(input: String) -> Result<Self, Self::Error> {
let (variable_name, value) = crate::parser::variables::parse_set_variable(input)?; let (variable_name, value) = crate::parser::variables::parse_set_variable(&input)?;
Ok(SetVariableCommand(variable_name, value)) Ok(SetVariableCommand(variable_name, value))
} }
} }
@ -148,11 +148,11 @@ impl From<DeleteVariableCommand> for Box<dyn Command> {
} }
} }
impl TryFrom<&str> for DeleteVariableCommand { impl TryFrom<String> for DeleteVariableCommand {
type Error = BotError; type Error = BotError;
fn try_from(input: &str) -> Result<Self, Self::Error> { fn try_from(input: String) -> Result<Self, Self::Error> {
Ok(DeleteVariableCommand(input.to_owned())) Ok(DeleteVariableCommand(input))
} }
} }