Skip to main content

CommandSet

Struct CommandSet 

Source
pub struct CommandSet { /* private fields */ }
Expand description

Compiled command set built from a table of (pattern, handler) pairs.

Implementations§

Source§

impl CommandSet

Source

pub fn from_table(table: &[(&str, Handler)]) -> Result<Self, ParseError>

Build a command set from a static table of (pattern, handler) pairs.

Examples found in repository?
examples/demo.rs (line 14)
13fn main() {
14    let set = CommandSet::from_table(TABLE).expect("bad command table");
15    let stdin = std::io::stdin();
16    let mut line = String::new();
17    while stdin.read_line(&mut line).is_ok_and(|n| n > 0) {
18        match set.parse(line.trim()) {
19            Ok(cmds) => {
20                for cmd in &cmds {
21                    set.dispatch(cmd);
22                }
23            }
24            Err(e) => eprintln!("ERROR: {e}"),
25        }
26        line.clear();
27    }
28}
Source

pub fn parse(&self, line: &str) -> Result<Vec<Command>, ParseError>

Parse an input line (which may contain multiple ;-separated commands) into a list of Commands.

Examples found in repository?
examples/demo.rs (line 18)
13fn main() {
14    let set = CommandSet::from_table(TABLE).expect("bad command table");
15    let stdin = std::io::stdin();
16    let mut line = String::new();
17    while stdin.read_line(&mut line).is_ok_and(|n| n > 0) {
18        match set.parse(line.trim()) {
19            Ok(cmds) => {
20                for cmd in &cmds {
21                    set.dispatch(cmd);
22                }
23            }
24            Err(e) => eprintln!("ERROR: {e}"),
25        }
26        line.clear();
27    }
28}
Source

pub fn dispatch(&self, cmd: &Command)

Dispatch a parsed command to its handler.

Examples found in repository?
examples/demo.rs (line 21)
13fn main() {
14    let set = CommandSet::from_table(TABLE).expect("bad command table");
15    let stdin = std::io::stdin();
16    let mut line = String::new();
17    while stdin.read_line(&mut line).is_ok_and(|n| n > 0) {
18        match set.parse(line.trim()) {
19            Ok(cmds) => {
20                for cmd in &cmds {
21                    set.dispatch(cmd);
22                }
23            }
24            Err(e) => eprintln!("ERROR: {e}"),
25        }
26        line.clear();
27    }
28}

Auto Trait Implementations§

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.