document command module

master
rasul 5 years ago
parent bbc85c535a
commit 6d2a53e53f

@ -7,6 +7,13 @@ use crate::command::{CommandNew, CommandSet, Parse, ParserError};
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
/// Root level of command.
///
/// Every command must be implemented here, either directly or nested inside
/// a subcommand somewhere.
///
/// The parser matches against lowercase string representations of the enum
/// variants
#[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)]
pub enum Command { pub enum Command {
N, N,

@ -1,3 +1,22 @@
//! Mud commands
//!
//! This module contains the command parser and the commands themselves. A
//! generic interface is provided to make it easier to parse the input, map
//! it to a function, call that function, and provide help on commands.
//!
//! The command parsing is done in such a fashion that only enough characters
//! to unambiguously desginate a command are needed. That means "lo" will
//! match "look" as long as there's not a "log". If multiple matches are
//! found, the first one in alphabetical order is selected. This action may
//! change in the future.
//!
//! The help system isn't implemented yet.
//!
//! Each command is to be listed in the `Command` enum, which is the root
//! level of the commands. It and any subcommands must implement the `Parse`
//! trait, and if there are subcommands then certain trait methods must be
//! implemented for the subcommand parsing and matching to function properly.
mod command; mod command;
mod dispatch; mod dispatch;
mod new; mod new;

@ -7,10 +7,15 @@ use crate::command::Parse;
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
/// Create new things
#[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)]
pub enum CommandNew { pub enum CommandNew {
/// Create a new room
Room, Room,
/// Create a new zone
Zone, Zone,
Default, Default,
} }

@ -7,7 +7,9 @@ use crate::command::ParserError;
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
/// Command parser. Commands consist of the text from the beginning of a string, up to but not /// Command parser.
///
/// Commands consist of the text from the beginning of a string, up to but not
/// including the first space character or newline. If a space, that space is removed and the /// including the first space character or newline. If a space, that space is removed and the
/// rest of the string is put into args. If a newline, args will be empty. The command part of the /// rest of the string is put into args. If a newline, args will be empty. The command part of the
/// string is then parsed for a match with the variants of the enum. After that, control is passed /// string is then parsed for a match with the variants of the enum. After that, control is passed

@ -1,10 +1,15 @@
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
/// Errors that may be emitted by the command parser
#[derive(Debug)] #[derive(Debug)]
pub enum ParserError { pub enum ParserError {
/// No command provided
Empty, Empty,
/// Unknown command
Unknown, Unknown,
Default, Default,
} }

@ -6,12 +6,12 @@ use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send_error}; use crate::{try_option_send_error, try_send_error};
impl CommandSetPlayer { impl CommandSetPlayer {
/// Set the player's name
pub fn dispatch_name(&self, args: String, token: Token, db: &mut Db) -> SendQueue { pub fn dispatch_name(&self, args: String, token: Token, db: &mut Db) -> SendQueue {
let mut player = try_option_send_error!(token, db.get_connected_player(token)); let mut player = try_option_send_error!(token, db.get_connected_player(token));
let new_name = args.trim(); let new_name = args.trim();
if new_name.is_empty() { if new_name.is_empty() {
//return SendQueue::from_string(token, "Name can't be empty")
return SendQueue(vec![(token, "Name can't be empty".into(), true)].into()); return SendQueue(vec![(token, "Name can't be empty".into(), true)].into());
} }

@ -7,9 +7,12 @@ use crate::command::Parse;
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
/// Modify player properties
#[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)]
pub enum CommandSetPlayer { pub enum CommandSetPlayer {
/// Set the player's name
Name, Name,
Default, Default,
} }

@ -8,7 +8,7 @@ use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send, try_send_error}; use crate::{try_option_send_error, try_send, try_send_error};
impl CommandSetRoomDescription { impl CommandSetRoomDescription {
/// Set the specified line in the description to the specified text. /// Delete the specified line from the description.
pub fn dispatch_delete(&self, args: String, token: Token, db: &mut Db) -> SendQueue { pub fn dispatch_delete(&self, args: String, token: Token, db: &mut Db) -> SendQueue {
// make sure something was provided // make sure something was provided
if args.is_empty() { if args.is_empty() {

@ -7,10 +7,15 @@ use crate::command::Parse;
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
/// Modify the room description
#[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)]
pub enum CommandSetRoomDescription { pub enum CommandSetRoomDescription {
/// Delete a line
Delete, Delete,
/// Set a line
Set, Set,
Default, Default,
} }

@ -6,6 +6,7 @@ use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send_error}; use crate::{try_option_send_error, try_send_error};
impl CommandSetRoom { impl CommandSetRoom {
/// Change the room name
pub fn dispatch_name(&self, args: String, token: Token, db: &mut Db) -> SendQueue { pub fn dispatch_name(&self, args: String, token: Token, db: &mut Db) -> SendQueue {
let player = try_option_send_error!(token, db.get_connected_player(token)); let player = try_option_send_error!(token, db.get_connected_player(token));

@ -7,10 +7,15 @@ use crate::command::{CommandSetRoomDescription, Parse, ParserError};
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
/// Set room properties
#[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)]
pub enum CommandSetRoom { pub enum CommandSetRoom {
/// Change the room description
Description(CommandSetRoomDescription), Description(CommandSetRoomDescription),
/// Set the room name
Name, Name,
Default, Default,
} }

@ -7,11 +7,18 @@ use crate::command::{CommandSetPlayer, CommandSetRoom, CommandSetZone, Parse, Pa
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
/// Set various options
#[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)]
pub enum CommandSet { pub enum CommandSet {
/// Player options
Player(CommandSetPlayer), Player(CommandSetPlayer),
/// Room options
Room(CommandSetRoom), Room(CommandSetRoom),
/// Zone options
Zone(CommandSetZone), Zone(CommandSetZone),
Default, Default,
} }

@ -6,6 +6,7 @@ use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send_error}; use crate::{try_option_send_error, try_send_error};
impl CommandSetZone { impl CommandSetZone {
/// Change the zone name
pub fn dispatch_name(&self, args: String, token: Token, db: &mut Db) -> SendQueue { pub fn dispatch_name(&self, args: String, token: Token, db: &mut Db) -> SendQueue {
let player = try_option_send_error!(token, db.get_connected_player(token)); let player = try_option_send_error!(token, db.get_connected_player(token));

@ -7,9 +7,12 @@ use crate::command::Parse;
use crate::database::Db; use crate::database::Db;
use crate::queue::SendQueue; use crate::queue::SendQueue;
/// Change zone properties
#[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)]
pub enum CommandSetZone { pub enum CommandSetZone {
/// Change the name of the zone
Name, Name,
Default, Default,
} }

Loading…
Cancel
Save