From 6d2a53e53f3ce118b4290ae602613d75c1e2d602 Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 17:23:43 -0500 Subject: [PATCH] document command module --- src/command/command.rs | 7 +++++++ src/command/mod.rs | 19 +++++++++++++++++++ src/command/new/new.rs | 5 +++++ src/command/parse.rs | 4 +++- src/command/parser_error.rs | 5 +++++ src/command/set/player/name.rs | 2 +- src/command/set/player/player.rs | 3 +++ src/command/set/room/description/delete.rs | 2 +- .../set/room/description/description.rs | 5 +++++ src/command/set/room/name.rs | 1 + src/command/set/room/room.rs | 5 +++++ src/command/set/set.rs | 7 +++++++ src/command/set/zone/name.rs | 1 + src/command/set/zone/zone.rs | 3 +++ 14 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/command/command.rs b/src/command/command.rs index aef70fd..bc565dc 100644 --- a/src/command/command.rs +++ b/src/command/command.rs @@ -7,6 +7,13 @@ use crate::command::{CommandNew, CommandSet, Parse, ParserError}; use crate::database::Db; 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)] pub enum Command { N, diff --git a/src/command/mod.rs b/src/command/mod.rs index e280632..4f836f9 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -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 dispatch; mod new; diff --git a/src/command/new/new.rs b/src/command/new/new.rs index 46fbc88..570733f 100644 --- a/src/command/new/new.rs +++ b/src/command/new/new.rs @@ -7,10 +7,15 @@ use crate::command::Parse; use crate::database::Db; use crate::queue::SendQueue; +/// Create new things #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] pub enum CommandNew { + /// Create a new room Room, + + /// Create a new zone Zone, + Default, } diff --git a/src/command/parse.rs b/src/command/parse.rs index 8246e66..591696a 100644 --- a/src/command/parse.rs +++ b/src/command/parse.rs @@ -7,7 +7,9 @@ use crate::command::ParserError; use crate::database::Db; 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 /// 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 diff --git a/src/command/parser_error.rs b/src/command/parser_error.rs index b202ed5..1d57d6f 100644 --- a/src/command/parser_error.rs +++ b/src/command/parser_error.rs @@ -1,10 +1,15 @@ use std::error::Error; use std::fmt; +/// Errors that may be emitted by the command parser #[derive(Debug)] pub enum ParserError { + /// No command provided Empty, + + /// Unknown command Unknown, + Default, } diff --git a/src/command/set/player/name.rs b/src/command/set/player/name.rs index 0e98900..79dd240 100644 --- a/src/command/set/player/name.rs +++ b/src/command/set/player/name.rs @@ -6,12 +6,12 @@ use crate::queue::SendQueue; use crate::{try_option_send_error, try_send_error}; impl CommandSetPlayer { + /// Set the player's name 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 new_name = args.trim(); 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()); } diff --git a/src/command/set/player/player.rs b/src/command/set/player/player.rs index 62708d2..6797d55 100644 --- a/src/command/set/player/player.rs +++ b/src/command/set/player/player.rs @@ -7,9 +7,12 @@ use crate::command::Parse; use crate::database::Db; use crate::queue::SendQueue; +/// Modify player properties #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] pub enum CommandSetPlayer { + /// Set the player's name Name, + Default, } diff --git a/src/command/set/room/description/delete.rs b/src/command/set/room/description/delete.rs index e00fdba..e385c5d 100644 --- a/src/command/set/room/description/delete.rs +++ b/src/command/set/room/description/delete.rs @@ -8,7 +8,7 @@ use crate::queue::SendQueue; use crate::{try_option_send_error, try_send, try_send_error}; 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 { // make sure something was provided if args.is_empty() { diff --git a/src/command/set/room/description/description.rs b/src/command/set/room/description/description.rs index 95d1fbf..570c078 100644 --- a/src/command/set/room/description/description.rs +++ b/src/command/set/room/description/description.rs @@ -7,10 +7,15 @@ use crate::command::Parse; use crate::database::Db; use crate::queue::SendQueue; +/// Modify the room description #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] pub enum CommandSetRoomDescription { + /// Delete a line Delete, + + /// Set a line Set, + Default, } diff --git a/src/command/set/room/name.rs b/src/command/set/room/name.rs index 9c8932d..2b83e4a 100644 --- a/src/command/set/room/name.rs +++ b/src/command/set/room/name.rs @@ -6,6 +6,7 @@ use crate::queue::SendQueue; use crate::{try_option_send_error, try_send_error}; impl CommandSetRoom { + /// Change the room name 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)); diff --git a/src/command/set/room/room.rs b/src/command/set/room/room.rs index 948c638..c2f269b 100644 --- a/src/command/set/room/room.rs +++ b/src/command/set/room/room.rs @@ -7,10 +7,15 @@ use crate::command::{CommandSetRoomDescription, Parse, ParserError}; use crate::database::Db; use crate::queue::SendQueue; +/// Set room properties #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] pub enum CommandSetRoom { + /// Change the room description Description(CommandSetRoomDescription), + + /// Set the room name Name, + Default, } diff --git a/src/command/set/set.rs b/src/command/set/set.rs index 43e78d4..9c0a91b 100644 --- a/src/command/set/set.rs +++ b/src/command/set/set.rs @@ -7,11 +7,18 @@ use crate::command::{CommandSetPlayer, CommandSetRoom, CommandSetZone, Parse, Pa use crate::database::Db; use crate::queue::SendQueue; +/// Set various options #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] pub enum CommandSet { + /// Player options Player(CommandSetPlayer), + + /// Room options Room(CommandSetRoom), + + /// Zone options Zone(CommandSetZone), + Default, } diff --git a/src/command/set/zone/name.rs b/src/command/set/zone/name.rs index c228654..f790497 100644 --- a/src/command/set/zone/name.rs +++ b/src/command/set/zone/name.rs @@ -6,6 +6,7 @@ use crate::queue::SendQueue; use crate::{try_option_send_error, try_send_error}; impl CommandSetZone { + /// Change the zone name 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)); diff --git a/src/command/set/zone/zone.rs b/src/command/set/zone/zone.rs index bace83b..a34b6a7 100644 --- a/src/command/set/zone/zone.rs +++ b/src/command/set/zone/zone.rs @@ -7,9 +7,12 @@ use crate::command::Parse; use crate::database::Db; use crate::queue::SendQueue; +/// Change zone properties #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] pub enum CommandSetZone { + /// Change the name of the zone Name, + Default, }