Expand description
Minecraft command parsing crate inspired by Brigadier
and nom. This crate tries to combine the easy syntax from
Brigadier with the performant parsing concepts of nom.
§Creating a parser
Using the builder pattern, different parsers can be chained together to
create a logical tree a command can be propagated through. This is done by
using the appropiate parser functions in the root of the crate. Custom
parsers can be implemented using CommandArgument and most other traits.
It is recommended to look at parser structs in parsers
for inspiration.
§Example
let parser = literal("foo")
.then(
integer_i32("bar")
.build_exec(|ctx: (), bar| {
println!("Bar is {}", bar);
Ok::<(), Infallible>(())
})
).build_exec(|ctx: ()| {
println!("Called foo with no arguments");
Ok::<(), Infallible>(())
});This code creates a new parser that can parse commands in the forms of foo
and foo <bar> and can be represented in a tree like this:
+-----------+ +---------+
| i32 (bar) +-----> | Execute |
+-----+-----+ +---------+
^
|
+---+------+
+-----------+ | (foo) | +---------+
| lit (foo) +-->| Then +----> | Execute |
+-----------+ +----------+ +---------+The parser first expects a literal string “foo” as denoted by the
literal("foo"). After this literal value, an optional integer can be
provided. Note that the second argument is optional due to the Execute
attached to the Then that branches to that argument.
Unlike Mojang’s brigadier, arguments are not collected in a Context
object. They are instead fed directly into the provided closures. A generic
context however is provided so dependents can pass data to the closures
after parsing (ctx in the example).
§Command help
A HelpArgument is provided to easily integrate a command into a help
system. This is done by calling help() on a command parser like this:
let parser = literal("foo")
.then(
integer_i32("bar")
.build_exec(|ctx: (), bar| {
println!("Bar is {}", bar);
Ok::<(), Infallible>(())
})
).build_exec(|ctx| {
println!("Called foo with no arguments");
Ok::<(), Infallible>(())
})
.help("Short description of foo")
.build_exec(|ctx: (), usages: UsagePrint<_>| {
println!("'foo help' was called");
Ok::<(), Infallible>(())
});The parser can now return foo and Short description of foo when queried
using HelpUsage, this is useful for collecting a list of commands. This
also automatically chains a HelpArgument
for foo help. The usages variable is an iterator over all the different
syntaxes this parser understands (UsagePrint). In this example, that
would be:
foofoo <bar>
There will be as many syntaxes as action points (build_exec
orbuild_propagate) defined. Note that foo help is ignored.
Modules§
- parsers
- Collection of builtin parsers for several basic argument types.
Structs§
- Chain
- A chain of two
MultipleUsageiterators. - Command
Error - Error returned when parsing a command.
- Help
Entry - Name and description of a command.
- Prefix
- Prefixes every usage returned by the contained
MultipleUsageiterator with the containedSingleUsage. - Usage
Print - Wrapper of
MultipleUsageiterator.
Enums§
- CmdError
Kind - Error kinds of
CommandError.
Traits§
- Argument
Marker Default Impl - Default argument marker trait.
- Build
Execute - Command logic builder type (without propagation).
- Build
Propagate - Command logic builder type (with propagation).
- Child
Usage - Implemented on
MultipleUsagethat want to return a single “root” usage in the parser tree. - Command
Argument - A parser for a command argument type.
- Command
Parser - Parser trait combination of
ExecuteandHelpUsage. - Execute
- Command parser execution entrypoint.
- Help
Usage - Type that returns a
HelpEntry. - Into
Multiple Usage - Type that can build a
MultipleUsage. - Multiple
Usage - One or more usages that can be iterated through.
- Propagate
- Command parser propagation entrypoint.
- Single
Usage - A single usage able to write itself to a
Write. - Task
Logic - Command logic definition trait (with arguments).
- Task
Logic NoArgs - Command logic definition trait (without arguments).
- Then
- Chaining extension trait for argument type parsers (
CommandArgument). - Then
Help - Type that can produce a usage list and help command.
Functions§
- boolean
- Create a boolean parser
- float_
32 - Create a f32 argument parser.
- float_
64 - Create a f64 argument parser.
- integer_
i8 - Create a i8 argument parser.
- integer_
i16 - Create a i16 argument parser.
- integer_
i32 - Create a i32 argument parser.
- integer_
i64 - Create a i64 argument parser.
- integer_
u8 - Create a u8 argument parser.
- integer_
u16 - Create a u16 argument parser.
- integer_
u32 - Create a u32 argument parser.
- integer_
u64 - Create a u64 argument parser.
- literal
- Create a new literal parser
- prefix
- Returns a new
Prefix.