Batteries-included implementation of MXP (MUD eXtension Protocol).
Description
MXP (MUD eXtension Protocol) is an open communication protocol for MUD servers and clients. The mxp library is a lightweight but robust implementation of the protocol in its entirety. This library is geared toward client implementations, but it can also be used for server-side syntax handling.
By default, mxp processes all tags described in the MXP standard (above). To restrict which
elements your client supports, send a SupportResponse to the MUD server.
Examples
Simple parsing
mxp can be used to parse MXP strings directly:
use ;
assert_eq!;
However, this approach lacks the most important aspect of MXP parsing: custom entities and
elements. It also has no way to protect secure tags during OPEN line modes. (It's also
inefficient, because it uses owned strings rather than borrowed string slices.) The intended way
to use this library is with state management via mxp::State and mxp::ModeState.
State management
mxp provides state management via mxp::State: the central hub of MXP logic.
mxp::State stores custom [Element]s, custom [Entity]s, and user-defined [LineTag]s.
In this approach, rather than using FromStr to parse tags with owned strings,
Tag::parse is used to deserialize tags in-place using borrowed string slices.
Furthermore, mxp::ModeState can be used to handle line modes, as well as retrieving custom
elements from user-defined line tags. Rather than being parsed from XML tags like everything
else in MXP, modes are set by ANSI escape sequences. For example, to set the MXP mode to 20,
server would send <ESC>[20z. As such, it is up to the client to recognize MXP mode changes and
apply them with [ModeState::set] and [ModeState::revert].
use Cow;
// Alternatively:
// - `use mxp::node;` for prefixed names, e.g. `node::TagOpen`
// - `use mxp::node::{Tag as TagNode, TagOpen as TagOpenNode};`
use ;
let mut mxp_state = with_globals;
let mut mode = new;
mode.set;
let secure = mode.use_secure;
assert!; // Mode must be secure in order to define elements
handle_element.unwrap;
let secure = mode.use_secure;
assert!; // SECURE_ONCE reverts back to OPEN mode after use
handle_element.unwrap; // prints "----\n"
Server-side usage
All of the types exported by mxp can be serialized to MXP syntax with their Display
implementation.
use EntityKeyword;
let entity = EntityDefinition ;
assert_eq!;
For advanced tag building, see TagBuilder.
Memory allocation
Tag::parse allocates memory if it parses a custom element definition (as
[node::ElementDefinition]), which needs to use owned strings because custom elements are
stored long-term in state. Otherwise, it only allocates memory to parse arguments passed to
an opening tag (as [node::TagOpen]), as described in the next paragraph.
[Arguments] parsing allocates a Vec<&'a str> and HashMap<&'a str, &'a str> for positional
and named arguments respectively. Both use generous size guesses (based on the number of spaces
in the string) in order to prevent reallocations. [Arguments] are ephemeral structs that drop
as soon as they are done being used to decode tags.
Tag decoding (via [AtomicTag::decode] and [Element::decode]) uses Cows because
attributes may contain entities, in which case they must be decoded to owned strings in order to
replace entities with their definitions (e.g. replacing "<" with "<"). If the MXP string
does not contain entities, no allocations are performed.