pub trait Command {
type Input;
type Output;
type InputParseError;
type ExecutionError;
// Required methods
fn parse(
&self,
input: &str,
repl_context: &ReplContext<'_>,
) -> Result<Self::Input, Self::InputParseError>;
fn execute(
&self,
input: Self::Input,
repl_context: &mut ReplContext<'_>,
) -> Result<Self::Output, Self::ExecutionError>;
fn print_output(&self, output: Self::Output, repl_context: &ReplContext<'_>);
fn print_input_parse_error(
&self,
error: Self::InputParseError,
repl_context: &ReplContext<'_>,
);
fn print_execution_error(
&self,
error: Self::ExecutionError,
repl_context: &ReplContext<'_>,
);
// Provided method
fn name(&self) -> String { ... }
}Expand description
A Command implementation will do the following:
- Parse user input from a string that follows the command which is of the pattern
:command-name inputand gets a structured Input - The Input will soon require a Display for documentation purpose
- The Input will be executed to get a structured Output or an ExecutionError
- Print results or errors to the user
To register new commands to the REPL from any client (Ex: golem-cli),
create a struct that implements the Command trait and register it using let mut registry = CommandRegistry::default(),
and registry.register(MyCommand);, and pass it the config when bootstrapping the REPL.
Required Associated Types§
Sourcetype Input
type Input
The structured input type resulting from parsing the raw REPL string.
If using Clap, this should be a type that derives clap::Parser, and the error
is typically clap::Error.
Sourcetype InputParseError
type InputParseError
Error type returned when parsing the user input fails.
Sourcetype ExecutionError
type ExecutionError
Error type returned when command execution fails.
Required Methods§
Sourcefn parse(
&self,
input: &str,
repl_context: &ReplContext<'_>,
) -> Result<Self::Input, Self::InputParseError>
fn parse( &self, input: &str, repl_context: &ReplContext<'_>, ) -> Result<Self::Input, Self::InputParseError>
Parses user input into a structured Input type.
Parse implementation can internally make use of Clap or any other parsing library
If using Clap, using helpers like parse_with_clap is recommended to handle shell-style splitting
and all you need is derived clap:Parser trait on the Input type.
§Parameters
prompt_input: The raw string entered by the user after the command name in the REPL. For example, if the user types:my-command foo bar, thenprompt_inputwill be"foo bar".repl_context: An immutable projection of internal ReplState. This gives access to printer, current session of rib script etc
Sourcefn execute(
&self,
input: Self::Input,
repl_context: &mut ReplContext<'_>,
) -> Result<Self::Output, Self::ExecutionError>
fn execute( &self, input: Self::Input, repl_context: &mut ReplContext<'_>, ) -> Result<Self::Output, Self::ExecutionError>
Executes the command with the parsed input and REPL context.
§Parameters
input: The structured input previously returned byparse.repl_context: An immutable projection of internal ReplState
Sourcefn print_output(&self, output: Self::Output, repl_context: &ReplContext<'_>)
fn print_output(&self, output: Self::Output, repl_context: &ReplContext<'_>)
Prints the output produced by the command after successful execution.
§Parameters
output: The result returned byexecuteif it completed successfully.repl_context: An immutable projection of internal ReplState
Sourcefn print_input_parse_error(
&self,
error: Self::InputParseError,
repl_context: &ReplContext<'_>,
)
fn print_input_parse_error( &self, error: Self::InputParseError, repl_context: &ReplContext<'_>, )
Prints an error that occurred during input parsing.
§Parameters
error: The error returned byparsewhen the user input is invalid.repl_context: An immutable projection of internal ReplState
Sourcefn print_execution_error(
&self,
error: Self::ExecutionError,
repl_context: &ReplContext<'_>,
)
fn print_execution_error( &self, error: Self::ExecutionError, repl_context: &ReplContext<'_>, )
Prints an error that occurred during command execution.
§Parameters
error: The error returned byexecutewhen something goes wrong during execution.repl_context: An immutable projection of internal ReplState