Struct twilight_command_parser::Parser[][src]

pub struct Parser<'a> { /* fields omitted */ }
Expand description

A struct to parse prefixes, commands, and arguments out of messages.

While parsing, the parser takes into account the configuration that it was configured with. This configuration is mutable during runtime via the Parser::config_mut method.

After parsing, you’re given an optional Command: a struct representing a command and its relevant information. Refer to its documentation for more information.

Examples

Using a parser configured with the commands "echo" and "ping" and the prefix "!", parse the message “!echo foo bar baz”:

use twilight_command_parser::{Command, CommandParserConfig, Parser};

let mut config = CommandParserConfig::new();
config.add_command("echo", false);
config.add_command("ping", false);
config.add_prefix("!");

let parser = Parser::new(config);

if let Some(command) = parser.parse("!echo foo bar baz") {
    match command {
        Command { name: "echo", arguments, .. } => {
            let content = arguments.as_str();

            println!("Got a request to echo `{}`", content);
        },
        Command { name: "ping", .. } => {
            println!("Got a ping request");
        },
        _ => {},
    }
}

Implementations

impl<'a> Parser<'a>[src]

pub fn new(config: impl Into<CommandParserConfig<'a>>) -> Self[src]

Creates a new parser from a given configuration.

pub fn config(&self) -> &CommandParserConfig<'a>[src]

Returns an immutable reference to the configuration.

pub fn config_mut(&mut self) -> &mut CommandParserConfig<'a>[src]

Returns a mutable reference to the configuration.

pub fn parse(&'a self, buf: &'a str) -> Option<Command<'a>>[src]

Parses a command out of a buffer.

If a configured prefix and command are in the buffer, then some Command is returned with them and a lazy iterator of the argument list.

If a matching prefix or command weren’t found, then None is returned.

Refer to the struct-level documentation on how to use this.

pub fn parse_with_prefix(
    &'a self,
    prefix: &'a str,
    buf: &'a str
) -> Option<Command<'a>>
[src]

Parse a command out of a buffer with a specific prefix.

Instead of using the list of set prefixes, give a specific prefix to parse the message, this can be used to have a kind of dynamic prefixes.

Example

let mut config = CommandParserConfig::new();
config.add_prefix("!");
config.add_command("echo", false);

let parser = Parser::new(config);

let command = parser.parse_with_prefix("=", "=echo foo")?;

assert_eq!("=", command.prefix);
assert_eq!("echo", command.name);

Trait Implementations

impl<'a> Clone for Parser<'a>[src]

fn clone(&self) -> Parser<'a>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<'a> Debug for Parser<'a>[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl<'a, T: Into<CommandParserConfig<'a>>> From<T> for Parser<'a>[src]

fn from(config: T) -> Self[src]

Performs the conversion.

Auto Trait Implementations

impl<'a> RefUnwindSafe for Parser<'a>

impl<'a> Send for Parser<'a>

impl<'a> Sync for Parser<'a>

impl<'a> Unpin for Parser<'a>

impl<'a> UnwindSafe for Parser<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.