Struct Parser

Source
pub struct Parser<'a> { /* private fields */ }
๐Ÿ‘ŽDeprecated since 0.8.1: use interactions via twilight-http or twilight-gateway
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ยง

Sourceยง

impl<'a> Parser<'a>

Source

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

๐Ÿ‘ŽDeprecated since 0.8.1: use interactions via twilight-http or twilight-gateway

Creates a new parser from a given configuration.

Source

pub const fn config(&self) -> &CommandParserConfig<'a>

๐Ÿ‘ŽDeprecated since 0.8.1: use interactions via twilight-http or twilight-gateway

Returns an immutable reference to the configuration.

Source

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

๐Ÿ‘ŽDeprecated since 0.8.1: use interactions via twilight-http or twilight-gateway

Returns a mutable reference to the configuration.

Source

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

๐Ÿ‘ŽDeprecated since 0.8.1: use interactions via twilight-http or twilight-gateway

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.

Source

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

๐Ÿ‘ŽDeprecated since 0.8.1: use interactions via twilight-http or twilight-gateway

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ยง

Sourceยง

impl<'a> Clone for Parser<'a>

Sourceยง

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

Returns a duplicate of the value. Read more
1.0.0 ยท Sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Sourceยง

impl<'a> Debug for Parser<'a>

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

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

Sourceยง

fn from(config: T) -> Self

Converts to this type from the input type.

Auto Trait Implementationsยง

ยง

impl<'a> Freeze for Parser<'a>

ยง

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ยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

type Owned = T

The resulting type after obtaining ownership.
Sourceยง

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

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

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

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

The type returned in the event of a conversion error.
Sourceยง

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

Performs the conversion.