Struct valence_command::graph::CommandGraphBuilder
source · pub struct CommandGraphBuilder<'a, T> { /* private fields */ }
Expand description
Ergonomic builder pattern for adding executables, literals and arguments to a command graph. See the derive macro for a more ergonomic way of doing this for a basic command with an enum or struct.
§Type Parameters
T
- the type that should be constructed by an executable when the command is executed
§Example
use std::collections::HashMap;
use petgraph::visit::{EdgeCount, NodeCount};
use valence_command::graph::{
CommandGraph, CommandGraphBuilder
};
use valence_command::{CommandRegistry};
use valence_command::parsers::CommandArg;
struct TestCommand {
test: i32,
}
let mut command_graph = CommandRegistry::default();
let mut executable_map = HashMap::new();
let mut parser_map = HashMap::new();
let mut modifier_map = HashMap::new();
let mut command_graph_builder = CommandGraphBuilder::<TestCommand>::new(&mut command_graph, &mut executable_map, &mut parser_map, &mut modifier_map);
// simple command
let simple_command = command_graph_builder
.root() // transition to the root node
.literal("test") // add a literal node then transition to it
.argument("test")
// a player needs one of these scopes to execute the command
// (note: if you want an admin scope you should use the link method on the scope registry.)
.with_scopes(vec!["test:admin", "command:test"])
.with_parser::<i32>()
// it is reasonably safe to unwrap here because we know that the argument is an integer
.with_executable(|args| TestCommand { test: i32::parse_arg(args).unwrap() })
.id();
// complex command (redirects back to the simple command)
command_graph_builder
.root()
.literal("test")
.literal("command")
.redirect_to(simple_command);
assert_eq!(command_graph.graph.graph.node_count(), 5); // root, test, command, <test>, test
// 5 edges, 2 for the simple command, 2 for the complex command and 1 for the redirect
assert_eq!(command_graph.graph.graph.edge_count(), 5);
in this example we can execute either of the following commands for the same result:
/test test 1
/test command test 1
the executables from these commands will both return a TestCommand
with
the value 1
Implementations§
source§impl<'a, T> CommandGraphBuilder<'a, T>
impl<'a, T> CommandGraphBuilder<'a, T>
sourcepub fn new(
registry: &'a mut CommandRegistry,
executables: &'a mut HashMap<NodeIndex, fn(_: &mut ParseInput<'_>) -> T>,
parsers: &'a mut HashMap<NodeIndex, fn(_: &mut ParseInput<'_>) -> bool>,
modifiers: &'a mut HashMap<NodeIndex, fn(_: String, _: &mut HashMap<ModifierValue, ModifierValue>)>,
) -> Self
pub fn new( registry: &'a mut CommandRegistry, executables: &'a mut HashMap<NodeIndex, fn(_: &mut ParseInput<'_>) -> T>, parsers: &'a mut HashMap<NodeIndex, fn(_: &mut ParseInput<'_>) -> bool>, modifiers: &'a mut HashMap<NodeIndex, fn(_: String, _: &mut HashMap<ModifierValue, ModifierValue>)>, ) -> Self
Creates a new command graph builder
§Arguments
- registry - the command registry to add the commands to
- executables - the map of node indices to executable parser functions
sourcepub fn root(&mut self) -> &mut Self
pub fn root(&mut self) -> &mut Self
Transitions to the root node. Use this to start building a new command from root.
sourcepub fn literal<S: Into<String>>(&mut self, literal: S) -> &mut Self
pub fn literal<S: Into<String>>(&mut self, literal: S) -> &mut Self
Creates a new literal node and transitions to it.
§Default Values
- executable -
false
- scopes -
Vec::new()
sourcepub fn argument<A: Into<String>>(&mut self, argument: A) -> &mut Self
pub fn argument<A: Into<String>>(&mut self, argument: A) -> &mut Self
Creates a new argument node and transitions to it.
§Default Values
- executable -
false
- scopes -
Vec::new()
- parser -
StringArg::SingleWord
- suggestion -
None
sourcepub fn redirect_to(&mut self, node: NodeIndex) -> &mut Self
pub fn redirect_to(&mut self, node: NodeIndex) -> &mut Self
Creates a new redirect edge from the current node to the node specified. For info on what a redirect edge is, see the module level documentation.
§Example
use std::collections::HashMap;
use valence_command::graph::CommandGraphBuilder;
use valence_command::CommandRegistry;
struct TestCommand;
let mut command_graph = CommandRegistry::default();
let mut executable_map = HashMap::new();
let mut parser_map = HashMap::new();
let mut modifier_map = HashMap::new();
let mut command_graph_builder = CommandGraphBuilder::<TestCommand>::new(
&mut command_graph,
&mut executable_map,
&mut parser_map,
&mut modifier_map,
);
let simple_command = command_graph_builder
.root() // transition to the root node
.literal("test") // add a literal node then transition to it
.id(); // get the id of the literal node
command_graph_builder
.root() // transition to the root node
.literal("test") // add a literal node then transition to it
.literal("command") // add a literal node then transition to it
.redirect_to(simple_command); // redirect to the simple command
sourcepub fn with_executable(
&mut self,
executable: fn(_: &mut ParseInput<'_>) -> T,
) -> &mut Self
pub fn with_executable( &mut self, executable: fn(_: &mut ParseInput<'_>) -> T, ) -> &mut Self
Sets up the executable function for the current node. This function will
be called when the command is executed and should parse the args and
return the T
type.
§Arguments
- executable - the executable function to add
§Example
have a look at the example for CommandGraphBuilder
sourcepub fn with_modifier(
&mut self,
modifier: fn(_: String, _: &mut HashMap<ModifierValue, ModifierValue>),
) -> &mut Self
pub fn with_modifier( &mut self, modifier: fn(_: String, _: &mut HashMap<ModifierValue, ModifierValue>), ) -> &mut Self
Adds a modifier to the current node
§Arguments
- modifier - the modifier function to add
§Example
use std::collections::HashMap;
use valence_command::graph::CommandGraphBuilder;
use valence_command::CommandRegistry;
struct TestCommand;
let mut command_graph = CommandRegistry::default();
let mut executable_map = HashMap::new();
let mut parser_map = HashMap::new();
let mut modifier_map = HashMap::new();
let mut command_graph_builder =
CommandGraphBuilder::<TestCommand>::new(&mut command_graph, &mut executable_map, &mut parser_map, &mut modifier_map);
command_graph_builder
.root() // transition to the root node
.literal("test") // add a literal node then transition to it
.with_modifier(|_, modifiers| {
modifiers.insert("test".into(), "test".into()); // this will trigger when the node is passed
})
.literal("command") // add a literal node then transition to it
.with_executable(|_| TestCommand);
sourcepub fn with_scopes<S: Into<String>>(&mut self, scopes: Vec<S>) -> &mut Self
pub fn with_scopes<S: Into<String>>(&mut self, scopes: Vec<S>) -> &mut Self
sourcepub fn apply_scopes(&mut self, registry: &mut CommandScopeRegistry) -> &mut Self
pub fn apply_scopes(&mut self, registry: &mut CommandScopeRegistry) -> &mut Self
sourcepub fn with_parser<P: CommandArg>(&mut self) -> &mut Self
pub fn with_parser<P: CommandArg>(&mut self) -> &mut Self
Sets the parser for the current node. This will decide how the argument is parsed client side and will be used to check the argument before it is passed to the executable. The node should be an argument node or nothing will happen.
§Type Parameters
P
- the parser to use for the current node (must beCommandArg
)
Auto Trait Implementations§
impl<'a, T> Freeze for CommandGraphBuilder<'a, T>
impl<'a, T> RefUnwindSafe for CommandGraphBuilder<'a, T>
impl<'a, T> Send for CommandGraphBuilder<'a, T>
impl<'a, T> Sync for CommandGraphBuilder<'a, T>
impl<'a, T> Unpin for CommandGraphBuilder<'a, T>
impl<'a, T> !UnwindSafe for CommandGraphBuilder<'a, T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.