mod options;
use crate::{
nodes::{Array, Object, OffsetRange},
ASTKind, ASTNode,
};
pub use options::CommandOptions;
use std::{
fmt,
fmt::{Display, Formatter},
hash::{Hash, Hasher},
};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum CommandKind {
Inline,
Normal,
SmartLink,
CodeBlock,
OpenClose,
SelfClose,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Command {
pub cmd: String,
pub kind: CommandKind,
pub args: Array,
pub kvs: Object,
}
impl Display for Command {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let a = self.args.values().map(|v| format!("{}", v));
let kv = self.kvs.iter().map(|(k, v)| format!("{} = {}", k, v));
write!(f, "\\{}({})", self.cmd, a.chain(kv).collect::<Vec<_>>().join(", "))
}
}
impl Hash for Command {
fn hash<H: Hasher>(&self, state: &mut H) {
self.cmd.hash(state);
todo!()
}
}
impl Command {
#[inline]
pub fn is(&self, rhs: impl AsRef<str>) -> bool {
self.cmd.as_str() == rhs.as_ref()
}
#[inline]
pub fn into_node(self, range: Option<OffsetRange>) -> ASTNode {
ASTNode { value: ASTKind::Command(box self), range }
}
}
impl Command {
#[inline]
pub fn command_line(cmd: String, _: String) -> Command {
Self { cmd, kind: CommandKind::Inline, args: Default::default(), kvs: Default::default() }
}
}
impl ASTKind {
#[inline]
pub fn command_line(cmd: impl Into<String>, content: impl Into<String>, r: Option<OffsetRange>) -> ASTNode {
Command::command_line(cmd.into(), content.into()).into_node(r)
}
}