Avoid cloned command input with From<String> instead of From<&str>.
continuous-integration/drone/push Build is passing Details

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;
fn try_from(input: &str) -> Result<Self, Self::Error> {
let result = parse_element_expression(input);
fn try_from(input: String) -> Result<Self, Self::Error> {
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.

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;
fn try_from(input: &str) -> Result<Self, Self::Error> {
let pool = parse_dice_pool(input)?;
fn try_from(input: String) -> Result<Self, Self::Error> {
let pool = parse_dice_pool(&input)?;
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;
fn try_from(input: &str) -> Result<Self, Self::Error> {
let roll = parse_regular_roll(input)?;
fn try_from(input: String) -> Result<Self, Self::Error> {
let roll = parse_regular_roll(&input)?;
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;
fn try_from(input: &str) -> Result<Self, Self::Error> {
let roll = parse_advancement_roll(input)?;
fn try_from(input: String) -> Result<Self, Self::Error> {
let roll = parse_advancement_roll(&input)?;
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;
fn try_from(_: &str) -> Result<Self, Self::Error> {
fn try_from(_: String) -> Result<Self, Self::Error> {
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;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(UnlinkCommand(value.to_owned()))
fn try_from(value: String) -> Result<Self, Self::Error> {
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;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(LinkCommand(value.to_owned()))
fn try_from(value: String) -> Result<Self, Self::Error> {
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;
fn try_from(_: &str) -> Result<Self, Self::Error> {
fn try_from(_: String) -> Result<Self, Self::Error> {
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;
fn try_from(_: &str) -> Result<Self, Self::Error> {
fn try_from(_: String) -> Result<Self, Self::Error> {
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;
fn try_from(input: &str) -> Result<Self, Self::Error> {
let topic = parse_help_topic(input);
fn try_from(input: String) -> Result<Self, Self::Error> {
let topic = parse_help_topic(&input);
Ok(HelpCommand(topic))
}
}

View File

@ -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<Box<dyn Command>, 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),

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;
fn try_from(_: &str) -> Result<Self, Self::Error> {
fn try_from(_: String) -> Result<Self, Self::Error> {
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;
fn try_from(input: &str) -> Result<Self, Self::Error> {
Ok(GetVariableCommand(input.to_owned()))
fn try_from(input: String) -> Result<Self, Self::Error> {
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;
fn try_from(input: &str) -> Result<Self, Self::Error> {
let (variable_name, value) = crate::parser::variables::parse_set_variable(input)?;
fn try_from(input: String) -> Result<Self, Self::Error> {
let (variable_name, value) = crate::parser::variables::parse_set_variable(&input)?;
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;
fn try_from(input: &str) -> Result<Self, Self::Error> {
Ok(DeleteVariableCommand(input.to_owned()))
fn try_from(input: String) -> Result<Self, Self::Error> {
Ok(DeleteVariableCommand(input))
}
}