69 lines
1.4 KiB
Rust
69 lines
1.4 KiB
Rust
use std::collections::HashMap;
|
|
use std::fmt;
|
|
use std::str::FromStr;
|
|
|
|
use pest::{error::Error as PestError, Parser};
|
|
|
|
#[derive(Parser)]
|
|
#[grammar = "message.pest"]
|
|
pub struct MessageParser;
|
|
|
|
/// Represents a general message, which is the basic unit of communication
|
|
pub struct Message {
|
|
/// The command, usually a verb, stating the purpose of the message.
|
|
pub command: String,
|
|
|
|
pub positional: Vec<String>,
|
|
pub named: HashMap<String, String>,
|
|
pub content: String,
|
|
}
|
|
|
|
#[derive(Debug, Error)]
|
|
pub enum ParseError {
|
|
#[error("parse error: {0}")]
|
|
Pest(#[from] PestError<Rule>),
|
|
}
|
|
|
|
impl FromStr for Message {
|
|
type Err = ParseError;
|
|
fn from_str(line: &str) -> Result<Self, Self::Err> {
|
|
let parsed = MessageParser::parse(Rule::line, line)?;
|
|
|
|
Ok(Message {
|
|
command: String::new(),
|
|
positional: vec![],
|
|
named: HashMap::new(),
|
|
content: String::new(),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Message {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{cmd}", cmd = self.command)
|
|
}
|
|
}
|
|
|
|
pub enum Command<'a> {
|
|
Auth(),
|
|
Dispatch(&'a str),
|
|
}
|
|
|
|
pub struct MessageCodec;
|
|
|
|
impl Decoder for MessageCodec {
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use std::str::FromStr;
|
|
|
|
use super::Message;
|
|
|
|
#[test]
|
|
fn basic() {
|
|
let message = Message::from_str("").unwrap();
|
|
}
|
|
}
|