Struct Parser

Source
pub struct Parser {
    pub prefix: char,
    pub option_prefix: char,
}
Expand description

Used to parse a Command from a string.

§Command Syntax

For more information about prefixes look at the fields of this struct. In any examples in this documentation ! will be used as a prefix and - will be used as a option prefix.

A command that this can parse could look like this:

!foo arg1 "long arg 2" -opt -opt -key1:val1 -key2:"long val2"

A command consists of 4 different parts:

  • name: The name of the command is the first word after the prefix. In the example above that’s foo.
  • arguments: Arguments are simple strings passed to the command. They are either single words or strings with spaces enclosed by ". In the example the two arguments are arg1 and long arg 2.
  • options: Options are a set of words. They are prefixed with the option_prefix. The only option in the example is opt.
  • parameters: Parameters are key-value pairs. They are prefixed with the option_prefix and seperated by :. The value part of the pair can be a word or a string enclosed by ". In the example above key1s value is val1 and key2s value is long val2.

§Escaping

Since " is used to mark the borders of long arguments and values, it’s not normally possible to include them in the string of the argument.

You can escape a long argument or value using \:

  • \": produces "
  • \\: produces \

§Example

use std::collections::{HashMap, HashSet};
use command_parser::{Parser, Command};

let p = Parser::new('!', '-');
let command_string = r##"!foo arg1 "long arg 2" -opt -opt -key1:val1 -key2:"long val2""##;

let command = Command {
    prefix: '!',
    option_prefix: '-',
    name: "foo".to_string(),
    arguments: vec!["arg1".to_string(), "long arg 2".to_string()],
    options: HashSet::from(["opt".to_string()]),
    parameters: HashMap::from([
        ("key1".to_string(), "val1".to_string()),
        ("key2".to_string(), "long val2".to_string())
    ])
};

assert_eq!(p.parse(command_string).unwrap(), command);

Fields§

§prefix: char

Prefix of the command.

<prefix><name> ...

Should not be set to ' ' as most chats trim leading spaces.

§option_prefix: char

Prefix of options and parameters.

... <option_prefix><option> ... <option_prefix><param key>:<param value>

Should not be set to ' ' or '"' as it may not result in expected outcomes.

Implementations§

Source§

impl Parser

Source

pub fn new(prefix: char, option_prefix: char) -> Parser

Source

pub fn parse<'a>(&self, raw: &'a str) -> Result<Command, ParseError>

Trait Implementations§

Source§

impl Debug for Parser

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Parser

§

impl RefUnwindSafe for Parser

§

impl Send for Parser

§

impl Sync for Parser

§

impl Unpin for Parser

§

impl UnwindSafe for Parser

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> 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, 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.